HornetQ 的 JMS Connection/ConnectionFactory 参考
我正在开发一个较旧的应用程序,并将其 JMS 系统从 JBoss Messaging 升级到 HornetQ,在此过程中我遇到了一些问题,这些问题似乎与该应用程序如何使用和管理 JMS 连接有关。这是我第一次大规模接触 JMS(除了简单的玩具用法),所以我想知道当前的习惯用法是……正确、愚蠢还是完全错误?
以下是当前系统的工作原理。
static QueueConnection connection;
static boolean isConnected;
static void sendSomeMessage(Object sendMe) {
if(!isConnected) connect();
}
static void connect() {
// jndi lookup for connection factory
connection = factory.createQueueConnection();
// lambdas in java pseudo code, woot!
connection.onException => disconnect();
connection.start();
isConnected = true;
}
static void disconnect() {
connection.close()
isConnected = false;
}
其要点是,对于发送的每条消息都会一遍又一遍地使用连接,直到发生错误为止,当发生错误时,连接最终会被关闭并重新创建。
我见过的每个示例总是为每条消息创建一个新的连接工厂和一个新的连接,但这些示例不是大型系统示例,它们只是一个“操作方法”示例。
保留对 JMS 连接的单个托管引用是否是可接受的习惯用法,是否应该缓存连接工厂?是否应该为每条新消息重新创建它们?
对我来说重用连接工厂但每次都使用新的连接有意义吗?
I'm working on an older application and upgrade it's JMS system from JBoss Messaging to HornetQ, in the process I've run into a few gotchas that seem to be related to how this application uses and manages JMS connections. This is my first large scale exposure to JMS (besides simple toy usage) so I'm wondering if the current idiom is... correct, silly, or dead wrong?
Here is how the current system works.
static QueueConnection connection;
static boolean isConnected;
static void sendSomeMessage(Object sendMe) {
if(!isConnected) connect();
}
static void connect() {
// jndi lookup for connection factory
connection = factory.createQueueConnection();
// lambdas in java pseudo code, woot!
connection.onException => disconnect();
connection.start();
isConnected = true;
}
static void disconnect() {
connection.close()
isConnected = false;
}
The gist of this is that the connection is used over and over for every single message that is sent until an error occurs, when an error occurs the connection finally gets closed and recreated.
Every example that I've seen always creates a new connection factory and a new connection for every message but these examples are not big system examples they are one off "how-to" examples.
Is keeping a single managed reference to a JMS Connection an acceptable idiom, should the connection factory be cached? Should they both be recreated for every new message?
It makes sense to me to reuse the connection factory but use a fresh connection every time?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
消息系统应该是异步的...
因此,您应该在应用程序的整个生命周期中保持连接打开。
目前,JMS 对于您必须创建的对象有点冗长,因此您必须创建连接和会话。
所以你应该这样做:
connection = cf.createConnection(...);
会话=连接.createSession(...);
生产者 = session.createProducer(...);
会话和生产者应始终在线程内使用。因为一个会话代表一个线程的使用情况。 (只要它是同步的,您就可以在多个线程中重用它)
Message systems are supposed to be asynchronous...
So, you should keep a connection open for the entire life of your application.
JMS is currently a bit verbose on objects you have to create, so you have to create a connection and a session.
So you should do this:
connection = cf.createConnection(...);
session = connection.createSession(...);
producer = session.createProducer(...);
The session and producer should always be used within a thread. As a session represents a thread usage. (You may reuse it within multiple threads as long as it's synchronized)