简体   繁体   English

Java JMS ActiveMQ - 关闭用于使用 MessageListener 创建消费者的 session

[英]Java JMS ActiveMQ - Close the session used to create a consumer with MessageListener

I have an application in JAVA that uses ActiveMQ.我在 JAVA 中有一个使用 ActiveMQ 的应用程序。 Following the suggestions of using a specific "session" for each consumer (to avoid problem with session concurrency from different threads) I am creating a new session before creating each new consumer.按照为每个消费者使用特定“会话”的建议(以避免来自不同线程的 session 并发问题),我在创建每个新消费者之前创建一个新的 session。 These consumers are using a MessageListener to gather their messages in an asynchronous way.这些消费者使用 MessageListener 以异步方式收集他们的消息。

When a consumer closes I also want to close the session that was used (and explicitly created) to create this consumer, but I don´t have access to the session that was used to create the specific consumer I am going to close (this method does not exists in the "MessageConsumer" object of the consumer).当消费者关闭时,我还想关闭用于(并明确创建)创建此消费者的 session,但我无权访问用于创建我要关闭的特定消费者的 session(此方法消费者的“MessageConsumer”object 中不存在)。 I don´t want to keep all these sessions opened because the consumers are connecting and disconnecting dynamically and the final number of opened (and unused) sessions will be very high.我不想让所有这些会话保持打开状态,因为消费者正在动态连接和断开连接,并且打开(和未使用的)会话的最终数量将非常高。

Is there a way to have access to the "session" that was used to create an specific "consumer" (MessageConsumer object) that uses a MessageListener?有没有办法访问用于创建使用 MessageListener 的特定“消费者”(MessageConsumer 对象)的“会话”?

Any other way to close the session when its consumer that uses a MessageListerner closes?当使用 MessageListerner 的消费者关闭时,还有其他方法可以关闭 session 吗?

Could another possibility be a kind of automatic purge of "sessions" that do not have any resource associated with them (consumer, producer, etc.)?另一种可能性是自动清除没有任何相关资源(消费者、生产者等)的“会话”吗?

Here is a snippet of my code:这是我的代码片段:

    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageConsumer consumer = session.createConsumer(session.createQueue(queueName));
    consumer.setMessageListener(new CustomMessageListener(locWSInstance));

You can pass the session and consumer down to your CustomerMessageListener and it can call the close during its shutdown lifecycle of your CustomMessageListener.您可以将 session 和使用者传递给您的 CustomerMessageListener,它可以在您的 CustomMessageListener 的关闭生命周期内调用关闭。 Otherwise, you'd need some sort of Manager/Tracker class to keep up with all the objects to prevent resource / memory leaks.否则,您需要某种管理器/跟踪器 class 来跟上所有对象以防止资源/ memory 泄漏。 This is no different than JDBC, With stateful communication comes performance and benefits.这与 JDBC 没有什么不同,状态通信带来了性能和优势。 but you have to track your clean-up as a trade-off.但你必须跟踪你的清理工作作为权衡。

  1. Use a PooledConnectionFactory使用 PooledConnectionFactory

  2. Be sure to unit test the lifecycle logic.确保对生命周期逻辑进行单元测试。 This is a resource leak / memory leak waiting to happen.这是等待发生的资源泄漏/memory 泄漏。

  3. Do proper try { } catch {} finally {} logic around each object to ensure the close gets called on all of them, and you do not have a leak on.close() methods throwing an exception and exiting the code path before all.close() are called (see below)围绕每个object 执行正确的 try { } catch {} finally {} 逻辑,以确保对所有这些都调用关闭,并且您没有泄漏 on.close() 方法首先抛出异常并退出代码路径。 close() 被调用(见下文)

     public void deactivate() { if(consumer.= null) { try { consumer;close(); } catch (JMSException e) { // do some logging } finally { consumer = null. } } if(session;= null) { try { session;close(); } catch (JMSException e) { // do some logging } finally { session = null; } } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MessageListener和JMS中的Consumer之间有什么区别? - What is the difference between a MessageListener and a Consumer in JMS? 具有ActiveMQ经纪人网络的JMS消费者 - JMS consumer with ActiveMQ network of brokers 使用ActiveMQ和Spring的JMS Standalone消费者 - JMS Standalone consumer with ActiveMQ and Spring Jms-ActiveMQ TOPIC慢消费者 - Jms-ActiveMQ TOPIC slow consumer 来自远程 ActiveMQ Artemis 的 Wildfly JMS Consumer - Wildfly JMS Consumer from remote ActiveMQ Artemis 为什么在JMS MessageListener中使用的实体管理器不参与JTA事务? - Why is an entity manager used in a JMS MessageListener not taking part in the JTA transaction? Java JMS MessageListener onMessage在某些PC上运行,但在其他PC上不运行 - Java JMS MessageListener onMessage running on some PCs but not others 线程“main”中的异常 java.lang.NoClassDefFoundError: javax/jms/MessageListener - Exception in thread "main" java.lang.NoClassDefFoundError: javax/jms/MessageListener 运行时没有onMessage事件(Oracle队列上的Java JMS MessageListener) - No onMessage events on Runtime (Java JMS MessageListener on Oracle Queue) 发生异常后,我们如何从系统中正确关闭和清理 JMS 连接、Session、消费者和生产者? - How can we close and clean properly JMS Connection, Session, Consumer and Producer from system after Exception?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM