[英]How to stop and start jms listener
I'm using Spring and I have a JMS queue to send messages from client to server.我正在使用 Spring 并且我有一个 JMS 队列来将消息从客户端发送到服务器。 I'd like to stop the messages from being sent when the server is down, and resend them when it's back up.
我想在服务器关闭时停止发送消息,并在它备份时重新发送它们。
I know it was asked before but I can't make it work.我知道之前有人问过它,但我无法让它工作。 I created a JmsListener and gave it an ID, but I cannot get it's container in order to stop\\start it.
我创建了一个 JmsListener 并给了它一个 ID,但我无法获取它的容器以停止\\启动它。
@Resource(name="testId")
private AbstractJmsListeningContainer _probeUpdatesListenerContainer;
public void testSendJms() {
_jmsTemplate.convertAndSend("queue", "working");
}
@JmsListener(destination="queue", id="testId")
public void testJms(String s) {
System.out.println("Received JMS: " + s);
}
The container bean is never created.永远不会创建容器 bean。 I also tried getting it from the context or using @Autowired and @Qualifier("testId") with no luck.
我也尝试从上下文中获取它或使用 @Autowired 和 @Qualifier("testId") 没有运气。
How can I get the container?我怎样才能得到容器?
You need @EnableJms
on one of your configuration classes.您需要在您的配置类之一上使用
@EnableJms
。
You need a jmsListenerContainerFactory
bean.您需要一个
jmsListenerContainerFactory
bean。
You can stop and start the containers using the JmsListenerEndpointRegistry
bean.您可以使用
JmsListenerEndpointRegistry
bean 停止和启动容器。
See the Spring documentation .请参阅Spring 文档。
如果在你的项目中使用了CachingConnectionFactory
,则需要在 stop 和 restart 之间调用resetConnection()
方法,否则旧的物理连接将保持打开状态,并且在重启时会被重用。
I used JmsListenerEndpointRegistry.我使用了 JmsListenerEndpointRegistry。 Here's my example.
这是我的例子。 I hope this will help.
我希望这将有所帮助。
Bean configuration in JmsConfiguration.java. JmsConfiguration.java 中的 Bean 配置。 I changed default autostart option.
我更改了默认的自动启动选项。
@Bean(name="someQueueScheduled")
public DefaultJmsListenerContainerFactory odsContractScheduledQueueContainerFactory() {
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory(someActiveMQ);
Map<String, Class<?>> typeIds = new HashMap<>();
typeIds.put(SomeDTO);
factory.setMessageConverter(messageConverter(Collections.unmodifiableMap(typeIds)));
factory.setPubSubDomain(false);
factory.setConnectionFactory(cf);
factory.setAutoStartup(false);
return factory;
}
Invoke in SomeFacade.java在 SomeFacade.java 中调用
public class SomeFacade {
@Autowired
JmsListenerEndpointRegistry someUpdateListener;
public void stopSomeUpdateListener() {
MessageListenerContainer container = someUpdateListener.getListenerContainer("someUpdateListener");
container.stop();
}
public void startSomeUpdateListener() {
MessageListenerContainer container = someUpdateListener.getListenerContainer("someUpdateListener");
container.start();
}
}
JmsListener implementation in SomeService.java SomeService.java 中的 JmsListener 实现
public class SomeService {
@JmsListener(id = "someUpdateListener",
destination = "${some.someQueueName}",
containerFactory ="someQueueScheduled")
public void pullUpdateSomething(SomeDTO someDTO) {
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.