消息侦听器中的JMS连接池 [英] JMS Connection Pooling in Message Listener

查看:146
本文介绍了消息侦听器中的JMS连接池的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我正在开发一个独立的Java应用程序,该应用程序连接到Websphere MQ以发送和接收消息.

Currently i'm working on a standalone Java apps that connects to a Websphere MQ to send and receive messages.

流处于异步模式,我们使用MessageListener类实现了该模式,以便在队列准备就绪时从队列中检索消息.用侦听器初始化使用者的代码如下:

The flow is in asynchronous mode, which we implemented using MessageListener class to retrieve the messages from the queue when they are ready. The code to initialize the consumer with the listener is as follow:

if(connection == null)
        connection = getJmsConnection();

    try {
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        if (isTopic) {
            destination = session.createTopic(destinationName);
        } else {
            destination = session.createQueue(destinationName);
        }
        consumer = session.createConsumer(destination);
        consumer.setMessageListener(listener);
    } catch (JMSException e) {
        e.printStackTrace();
    }

getJmsConnection()方法将从使用Apache Commons Pool库实现的池中返回连接.

The getJmsConnection() method will return a connection from a pool, implemented using Apache Commons Pool library.

我的问题是,只要程序正在运行,从池分配给侦听器的连接是否将处于活动状态并绑定到该侦听器?还是该连接被间歇性使用,并且可以被其他进程重用?我们的想法是让发送和接收过程重新使用来自池的连接,但是我不确定MessageListener如何处理为其分配的连接.

My question is, will the connection assign to the listener from the pool be active and tied to that listener as long as the program is running? Or the connection is used intermittently and can be reuse by other processes? Our idea is to have the sending and receiving process to reuse the connection from the pool, but i'm not sure how the MessageListener deal with the connection they are assigned with.

谢谢.

推荐答案

此处的关键对象是会话而不是连接;该会话位于将通过消息消耗(异步或其他方式)在此处执行主要工作的会话上.

The key object here is the session rather than the connection; the session is on the one that will be doing the primary work here with the message consumption (async or otherwise).

建议尝试尽可能广泛地共享连接.临时目标在连接级别范围内.因此,使用池化是一个好主意.完全有可能共享该连接.

It is advisable to try and share out the connection as widely as possible. Temporary destinations are scoped on the connection level. So the use of pooling is a good idea; it will be perfectly possible to share that connection around.

但是我也要说,合并会议可能是值得考虑的.使用此处的代码,每次通过该代码都会创建一个新的会话,这意味着将创建到WebSphere MQ队列管理器的新连接.目前尚不清楚它的范围是什么,但是如果迅速关闭它可能会成为瓶颈.

However I would also say that it might be worth considering pooling the sessions. With the code here a new session will be created, each time through that code, that will mean a new connection to the WebSphere MQ queue manager will be created. It's not clear what the scope of that will be, but if that is closed quickly it could become a bottleneck.

这篇关于消息侦听器中的JMS连接池的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆