ActiveMQ如何处理关闭的会话 [英] ActiveMQ How to handle closed session

查看:761
本文介绍了ActiveMQ如何处理关闭的会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ActiveMQ排队电子邮件,使用者读取队列并发送电子邮件.

I am using ActiveMQ to enqueue email messages, the consumer reads the queue and sends out emails.

在启动时,我注册了一个生产者并将其永久缓存.

At startup, I register a producer and cache it forever.

PooledConnectionFactory factory = new PooledConnectionFactory(new ActiveMQConnectionFactory(jmsBrokerUserName, jmsBrokerPassword, activeMQBrokerURL));
Connection connection = factory.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue(queueName);
MessageProducer producer = session.createProducer(destination);

有时,由于连接关闭,生产者无法使消息入队.

From time to time, the producer fails to enqueue the message as the connection gets closed.

Caused by: javax.jms.IllegalStateException: The Session is closed
    at org.apache.activemq.ActiveMQSession.checkClosed(ActiveMQSession.java:767) ~[activemq-client-5.10.0.jar:5.10.0]
    at org.apache.activemq.ActiveMQSession.configureMessage(ActiveMQSession.java:755) ~[activemq-client-5.10.0.jar:5.10.0]
    at org.apache.activemq.ActiveMQSession.createTextMessage(ActiveMQSession.java:438) ~[activemq-client-5.10.0.jar:5.10.0]
    at org.apache.activemq.jms.pool.PooledSession.createTextMessage(PooledSession.java:242) ~[activemq-jms-pool-5.10.0.jar:5.10.0]

有人可以让我知道处理封闭会议的最佳方法是什么?我应该重新注册我的制作人吗?还是有办法重新打开会话?

Can somebody please let me know what is the best way to handle closed sessions? Should I re-register my producer? Or is there a way to reopen session?

推荐答案

使用JMS时,您不应真正缓存JMS会话(以及任何挂起的内容,例如生产者).原因是JMS会话是JMS中的工作单元,因此应该是一个短暂的对象.在Java EE世界中,例如JMS Session也可能加入了全局事务,因此需要正确确定范围.

When using JMS you shouldn't really cache the JMS Session (and anything hanging of that such as a Producer). The reason being is that the JMS Session is the unit of work within JMS and so should be a short lived object. In the Java EE world that JMS Session might also be enlisted with a global transaction for example and so needs to be scoped correctly.

JMS Session实例不能被多个线程同时使用,因此最好的建议是在您正在使用的每个线程上创建JMS Session,并在完成该工作单元后关闭jms会话(在事务中发送消息或消息集合)

The JMS Session instance can not be used concurrently by multiple threads, so the best advise is to create the JMS Session on each thread you are using and close the jms session when you have finished that unit of work (sending a message or a collection of messages in a transaction)

这篇关于ActiveMQ如何处理关闭的会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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