我有一个使用ActiveMQConnectionFactory.createConnection()获取ActiveMQ连接的类。 然后,它创建并拥有会话,目标和在该目标上侦听的消费者(队列)。
我在消费者上调用receive()或receive(millis)来将我的消息从队列中取出。在某些情况下,我必须杀死(中断)调用receive方法的线程。我尝试在此之后关闭会话和连接。不幸的是,当调用close并且关联的“ActiveMQ传输”线程(以及与代理的相关连接)保持活动时,我经常遇到异常。我得到的例外是
org.myorg.awsiface.communication.MessagingException: Failed to close JMS connection
at
org.myorg.aws.communication.transport.JMSMessageTransport.cleanUp(JMSMessageTransport.java:253)
at
org.myorg.aws.communication.protocol.ContextFinishedProtocol.cleanUp(ContextFinishedProtocol.java:94)
at org.myorg.myservice.job.Job.run(Job.java:206)
Caused by: javax.jms.JMSException: java.io.InterruptedIOExceptio)
at org.apache.activemq.util.JMSExceptionSupport.create(JMSExceptionSupport.java:62)
at org.apache.activemq.ActiveMQConnection.doAsyncSendPacket(ActiveMQConnection.java:1227)
at org.apache.activemq.ActiveMQConnection.asyncSendPacket(ActiveMQConnection.java:1219)
at org.apache.activemq.ActiveMQSession.asyncSendPacket(ActiveMQSession.java:1799)
at org.apache.activemq.ActiveMQMessageConsumer.doClose(ActiveMQMessageConsumer.java:636)
at org.apache.activemq.ActiveMQMessageConsumer.close(ActiveMQMessageConsumer.java:627)
at org.myorg.aws.communication.transport.JMSMessageTransport.cleanUp(JMSMessageTransport.java:232)
... 2 more
Caused by: java.io.InterruptedIOException
at org.apache.activemq.transport.WireFormatNegotiator.oneway(WireFormatNegotiator.java:102)
at org.apache.activemq.transport.MutexTransport.oneway(MutexTransport.java:40)
at org.apache.activemq.transport.ResponseCorrelator.oneway(ResponseCorrelator.java:60)
at org.apache.activemq.ActiveMQConnection.doAsyncSendPacket(ActiveMQConnection.java:1225)
... 7 more
我尝试过以下连接网址 故障转移:// TCP://broker.ip.address:61616 TCP://broker.ip.address:61616 ?故障转移:// TCP://broker.ip.address:61616跟踪=真安培; closeAsync =假
此外,我尝试使用MessageConsumer :: receiveNoWait()调用并执行我自己的Thread.sleep,但每当我调用以下内容关闭连接时,我总是会遇到上述异常
try {
// I added the following two lines to see if the taskRunner was handling the threads - still no luck
TaskRunnerFactory taskRunner = ((ActiveMQConnection)connection).getSessionTaskRunner();
taskRunner.shutdown();
if (producer != null) {
producer.close();
}
if (consumer != null) {
consumer.close();
}
session.close();
if (connection instanceof ActiveMQConnection) {
ActiveMQConnection amqConnection = (ActiveMQConnection) connection;
amqConnection.stop();
}
connection.stop();
connection.close();
}
catch (ConnectionClosedException e) {
// NOOP - this is ok
}
catch (Exception e) {
throw new MessagingException("Failed to close JMS connection", e, log);
}
答案 0 :(得分:1)
我不确定为什么关闭不适合你。但是你可以通过使用异步消息监听器来完成你想要的任务。
Connection connection = connectionFactory.createConnection();
connection.start();
Session session =
connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination queue = session.createQueue("queueName");
Consumer consumer = session.createConsumer( queue );
consumer.setMessageListener( new MessageListener()
{
public void onMessage( Message message )
{
// whatever was after the receive() method call
}
} );
然后没有必要被打断。当你完成后,关闭事情:
consumer.close();
session.close();
queue.close();
connection.close();
答案 1 :(得分:1)
一种解决方案可能是在TCP ActiveMQ传输线程上设置守护进程标志,
例如tcp://URI:PORT?daemon=true
查看ActiveMQ文档:http://activemq.apache.org/tcp-transport-reference.html