简体   繁体   English

如何停止和启动 jms 侦听器

[英]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.

相关问题 如何手动启动Spring JMS侦听器容器 - How to start Spring JMS listener container manually Spring - JMS,在JMS activemq服务器启动/停止几次后,侦听器服务器抛出java.io.EOFException,然后不连接到运行JMS - Spring - JMS , after couple of start/stop of JMS activemq server, listener server throws java.io.EOFException and then does not connect to running JMS 未使用 jmsListenerEndpointRegistry.start() 启动 JMS 侦听器 - Not starting the JMS listener using jmsListenerEndpointRegistry.start() 如何在 java 的动作监听器中启动/恢复和停止/暂停线程 - How to start/resume and stop/pause a thread inside the action listener in java 如何从 Spring JMS 侦听器中的 JMS 消息获取自定义属性 - How to get custom properties from JMS message in a Spring JMS Listener 如何知道侦听器是否在JMS中获取消息? - How to know if a listener gets the message in JMS? 如何在测试环境中禁用JMS Spring侦听器 - How to disable JMS spring listener in test evnvironment 当单击停止按钮onclick侦听器时,如何停止执行start button onclick侦听器。 请输入验证码 - How to stop implementation of start button onclick listener when click on stop button onclick listener. please go through code 如何线程池 Spring JMS 侦听器 - How to thread pool a Spring JMS listener 如何通知JMS侦听器“无对象”消息 - How to notify JMS listener for 'no objects' message
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM