如何使用jms.Connection.setExceptionListner释放资源并重新连接?

发布于 2024-10-01 03:11:51 字数 1066 浏览 0 评论 0原文


假设我每个 JVM 有一个 Connection (作为单例实现),作为文档的派生,该文档建议使用一个 Connection 因为它是一个重对象。

来自文档:

...连接是一个相对的 重量级的物体。大多数客户都会 完成所有的消息传递 连接... JMS 客户端 通常会创建一个连接,一个或 更多会话和大量消息 生产者和消费者。

我正在尝试决定如何处理我的 SessionsProducersConsumers 相对于 ExceptionListener位于连接级别。
据我所知,当抛出 JMSException 时,它们不再可用是非常合理的,但我不确定触发上述侦听器后应该做什么。
我的 Session 保存在 ThreadLocal 中,它也保存在单例中。
我可以使用它在侦听器中调用 MySessionSingleton.closeSession() ,但这只会关闭绑定到抛出异常的线程的 Session ,而不是所有其他会话
此外,这不考虑生产者\消费者及其重新连接。
我看到使用但我不愿意模仿的一个可能的解决方案是为每个生产者\消费者建立一个连接和一个会话,这样我就可以控制上述所有内容。

如有任何想法,将不胜感激,
伊泰
澄清:
我当前的实现是由一位前程序员实现的,这就是我上面提到的正在使用的实现,它给我带来的最大问题是我需要多个生产者和消费者来使用与我相同的 Session需要 JTA 事务,并且我认为(可能是错误的)我需要那些生产者\消费者来共享会话。
这种联系是该决定的衍生。
因此,基本上即使我保持每个连接一个会话的关系,当一个会话有多个生产者\消费者时,我仍然会遇到上述问题。

Assuming I have one Connection per JVM (implemented as a singleton) as a derivative of the docs which recommend having one Connection as it's a heavy object.

From the docs:

... a connection is a relatively
heavyweight object. Most clients will
do all their messaging with a single
connection... A JMS client
typically creates a connection, one or
more sessions, and a number of message
producers and consumers.

I'm trying to decide what to do with my Sessions, Producers and Consumers with respect to the ExceptionListener which is at the Connection level.
To the best of my understanding it is very reasonable they are no longer usable, when a JMSException is thrown, but I'm not sure what should be done once the above listener is triggered.
My Sessions are kept in a ThreadLocal<Session> which is also kept in a singleton.
I can use this to call MySessionSingleton.closeSession() in the listener but this will only close the Session which is bound to the thread in which the Exception was thrown and not all other Sessions.
In addition this does not take care of the Producers\Consumers and their reconnect.
A possible solution which I saw used and I'm reluctant to imitate is to have a Connection and a Session for every Producer\Consumer and so I can control all of the above.

Would appreciate any thoughts,
Ittai
Clarification:
My current implementation, by a former programmer, is the one I refer to above as being used and the biggest problem it poses for me is that I need several producers and consumers to use the same Session as I have a need for JTA transactions and I think (might be wrong) that I need those Producers\Consumers to share the session.
The connection was a derivative of that decision.
So basically even if I keep the relationship of one session per connection I still have the above problem when one session has multiple Producers\Consumers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不回头走下去 2024-10-08 03:11:51

也许这是一个天真的问题,因为我对 JMS 没有太多的实际经验,但这不是调用 connection.close() 的最简单方法吗?

JMS API 表示“无需关闭已关闭连接的会话、生产者和消费者”。

另外,可以将全局标志设置为消息处理程序循环等的信号。

Maybe this is a naive question as I have not much real world experience with JMS, but wouldn't it be the easiest way to call connection.close()?

The JMS API says that "There is no need to close the sessions, producers, and consumers of a closed connection."

Aditionally a global flag could be set as a signal for message handler loops etc.

半枫 2024-10-08 03:11:51

我仍然不明白为什么你只需要一个 Connection 对象:-)。我会让应用程序服务器(我假设您正在其中运行)负责为您管理连接资源。

即使用 ConnectionFactory 来获取连接并创建会话以及消费者和生产者。

大致是这样的:

public void notify(String errorOccurrence)
{
    Connection connection = null;

    try
    {
        connection = connectionFactory.createConnection();

        Session session = connection.createSession(true, 
                          javax.jms.Session.AUTO_ACKNOWLEDGE);

        TextMessage message = session.createTextMessage(errorOccurrence);

        MessageProducer messageProducer = 
                            session.createProducer(errorOccurrenceChannel);

        messageProducer.send(message);
    }
    catch (JMSException e)
    {
        handleJmsExcption(e);
    }
    finally
    {
        if (connection != null)
        {
            try
            {
                connection.close();
            }
            catch (JMSException e)
            {
                handleJmsExcption(e);
            }
        }
    }
}

I still don't see why you need to only have one Connection object :-). I'd let the application server (I assume you're running in one) take care of managing the Connection resources for you.

i.e. Use a ConnectionFactory in order to get your Connections and create your session and consumers and producers off that.

Something roughly like:

public void notify(String errorOccurrence)
{
    Connection connection = null;

    try
    {
        connection = connectionFactory.createConnection();

        Session session = connection.createSession(true, 
                          javax.jms.Session.AUTO_ACKNOWLEDGE);

        TextMessage message = session.createTextMessage(errorOccurrence);

        MessageProducer messageProducer = 
                            session.createProducer(errorOccurrenceChannel);

        messageProducer.send(message);
    }
    catch (JMSException e)
    {
        handleJmsExcption(e);
    }
    finally
    {
        if (connection != null)
        {
            try
            {
                connection.close();
            }
            catch (JMSException e)
            {
                handleJmsExcption(e);
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文