我正在设置一个MessageListener并期望程序仍然会运行(因为监听线程仍处于活动状态),但是在"完成后#34;在屏幕上打印,程序终止。知道为什么会这样,以及如何解决它?
MessageListener messageListener = new DelegationReceiver();
Properties p = PropertiesLoader.getProperties("jms.properties");
InitialContext cntxt = new InitialContext();
cntxt.addToEnvironment(Context.INITIAL_CONTEXT_FACTORY, p.getProperty("namingFactory"));
cntxt.addToEnvironment(Context.URL_PKG_PREFIXES, p.getProperty("urlPackages"));
cntxt.addToEnvironment(Context.PROVIDER_URL,p.getProperty("providerURL"));
cntxt.addToEnvironment(Context.SECURITY_PRINCIPAL,"default");
cntxt.addToEnvironment(Context.SECURITY_CREDENTIALS,"password");
ConnectionFactory tfact = (ConnectionFactory) cntxt.lookup(p.getProperty("topicFactory"));
Topic topic = (Topic) cntxt.lookup(p.getProperty("myTopic"));
Connection connection = tfact.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer mc = session.createConsumer(topic);
mc.setMessageListener(messageListener);
connection.start();
System.out.println("Finished");