使用 Spring 启动和停止 JMS 侦听器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11407838/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 05:14:32  来源:igfitidea点击:

Start and Stop JMS Listener using Spring

springjmsspring-jms

提问by JOKe

So question is how to temporary stop and start a jms listener created using spring using the fallowing way :

所以问题是如何临时停止和启动使用 spring 创建的 jms 侦听器,使用休闲方式:

<amq:connectionFactory id="exampleJmsFactory" brokerURL="tcp://${jms.broker.url}" />

<jms:listener-container concurrency="1" connection-factory="exampleJmsFactory"  destination-type="queue" message-converter="exampleMessageConverter">
        <jms:listener destination="incoming.example.client.queue" ref="exampleProductsMessageConsumer" method="consume"/>
</jms:listener-container>


<bean id="exampleProductsMessageConsumer" class="com.unic.example.jms.receive.JmsExampleProductsMessageConsumer" scope="tenant"/>

So basically what is the problem. We do have an init/update mechanism that the client can run in any time and durring this init/update I want to stop consuming of ANY messages because the system is unusable in this time and if a message came it will be lost.

所以基本上是什么问题。我们确实有一个 init/update 机制,客户端可以在任何时间运行,在这个 init/update 期间,我想停止使用任何消息,因为系统在这段时间内无法使用,如果消息来了,它将丢失。

So how I can stop the listener or the listener container or the whole connection using the API. I found that a class AbstractJmsListeningContainer have stop/start but how I can get it ? I mean none of this jms: listener and listener-containers have a name or anything like that.

那么我如何使用 API 停止侦听器或侦听器容器或整个连接。我发现一个类 AbstractJmsListeningContainer 有停止/启动但是我怎么能得到它?我的意思是这些 jms: listener 和 listener-containers 都没有名字或类似的东西。

回答by gkamal

You can assign an id to the listener-container. Then get a reference to it, either by calling getBean or getting it injected. This will give you a AbstractJmsListeningContainer on which you can call start / stop.

您可以为侦听器容器分配一个 id。然后通过调用 getBean 或注入它来获取对它的引用。这将为您提供一个 AbstractJmsListeningContainer,您可以在其上调用 start/stop。

回答by JOKe

Yes thats do the trick.

是的,这就是诀窍。

<jms:listener-container concurrency="1" connection-factory="exampleJmsFactory"  destination-type="queue" message-converter="exampleMessageConverter">
        <jms:listener id="exampleProductsMessageListener" destination="incoming.example.client.queue" ref="exampleProductsMessageConsumer" method="consume"/>
</jms:listener-container>



DefaultMessageListenerContainer exampleProductsMessageListener= Registry.getApplicationContext().getBean("exampleProductsMessageListener", DefaultMessageListenerContainer.class);
exampleProductsMessageListener.stop();

回答by Anurag Kumar

You can also get a hold of the messageListenerContainer, and invoke stop() on it:

您还可以获取 messageListenerContainer,并在其上调用 stop():

@javax.annotation.Resource //autowire by name
 private AbstractJmsListeningContainer myMessageListenerContainer;

 myMessageListenerContainer.stop();

 I'm using the more verbose setup of this container:
 <bean id="myMessageListenerContainer" class="org.springframework.jms.listener.DefaultMes sageListenerContainer">
 <property name="connectionFactory" ref="jmsConnectionFactory"/>
 <property name="destination" ref="myQueue"/>
 <property name="messageListener" ref="myListener"/>
 <property name="autoStartup" value="true" />
 </bean>

Here you see that you can set autoStartup to false if you don't want the listenerContainer to automatically start.

在这里您可以看到,如果您不希望 listenerContainer 自动启动,则可以将 autoStartup 设置为 false。