java 长期存在的 JMS 会话。保持 JMS 连接/JMS 会话总是打开一个不好的做法吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/274051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Long lived JMS sessions. Is Keeping JMS connections / JMS sessions always opened a bad practice?
提问by Jo?o
Is keeping JMS connections / sessions / consumer always open a bad practice?
保持 JMS 连接/会话/消费者总是打开一个不好的做法吗?
Code draft example:
代码草稿示例:
// 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();
Any suggestions to improve this pattern of JMS usage?
有什么建议可以改进这种 JMS 使用模式?
采纳答案by John Meagher
That is a very common and acceptable practice when dealing with long lived connections. For many JMS servers it is in fact preferable to creating a new connection each time it is needed.
在处理长期连接时,这是一种非常普遍且可接受的做法。对于许多 JMS 服务器来说,实际上每次需要时都创建一个新连接更可取。
回答by James Strachan
Agreed. Here are some good tips on how to use JMS efficientlywhich includes keeping around connections/sessions/producers/consumers.
同意。这里有一些关于如何有效使用 JMS 的好技巧,包括保持连接/会话/生产者/消费者。
You might also want to check the recommendation on using transactionstoo if you are interested in maximising performance.
回答by Shashi
The choice of keeping connection/session/producer/consumer open for long or not should be based on the frequency at which producer/consumer sends/receives messages.
选择是否保持连接/会话/生产者/消费者长时间打开应该基于生产者/消费者发送/接收消息的频率。
If a producer sends or a consumer receives messages frequently then the connections/sessions/producer/consumer should be kept open. On the other hand if messages send/receive is infrequent then it is not good keeping these JMS objects open will consume system resources like sockets.
如果生产者发送或消费者频繁接收消息,则连接/会话/生产者/消费者应保持打开状态。另一方面,如果消息发送/接收不频繁,那么保持这些 JMS 对象打开将消耗系统资源(如套接字)并不好。
回答by John M
In our app, we will have connections/sessions/consumers/producers open for months at a time. We've had to work with our vendor (BEA) to make that work reliably. But any troubles with that is a bug the vendor needs to fix.
在我们的应用程序中,我们将一次打开数月的连接/会话/消费者/生产者。我们不得不与我们的供应商 (BEA) 合作以使其可靠地工作。但任何与此有关的问题都是供应商需要修复的错误。
回答by eparvan
FYI, there is no need to close the sessions, producers, and consumers of a closed connection( javax.jms.Connection). The code below should be enough to release the resources:
仅供参考,没有必要关闭关闭连接(javax.jms.Connection)的会话、生产者和消费者。下面的代码应该足以释放资源:
try {
this.connection.close();
} catch (JMSException e) {
//
}

