保持 JMS 连接/会话/消费者始终打开是一种不好的做法吗?
代码草案示例:
// app startup code
ConnectionFactory cf = (ConnectionFactory)jndiContext.lookup(CF_JNDI_NAME);
Connection connection = cf.createConnection(user,pass);
Session session = connection.createSession(true,Session.TRANSACTIONAL);
MessageConsumer consumer = session.createConsumer(new Queue(queueName));
consumer.setMessageListener(new MyListener());
connection.start();
connection.setExceptionListener(new MyExceptionHandler()); // handle connection error
// ... Message are processed on MyListener asynchronously ...
// app shutdown code
consumer.close();
session.close();
connection.close();
有什么建议可以改进这种 JMS 使用模式吗?
同意。这里有一些关于如何有效使用 JMS 的好技巧 http://activemq.apache.org/how-do-i-use-jms-efficiently.html其中包括保持连接/会话/生产者/消费者。
您可能还想检查使用交易的建议 http://activemq.apache.org/should-i-use-transactions.html如果您有兴趣最大限度地提高性能,也可以这样做。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)