在Spring Boot上动态修改@JMSListener目的地



我开发了一个@JMSListener,它从Java属性中获取目的地,并且运行良好。

但现在我需要能够在运行时更改队列的"目的地",而不必重置整个应用程序,即使我在运行时修改了Properties,队列的"目标地"也不会更改。

以下是我们如何实现@JMSListener:


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.support.converter.MessageConverter;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
@Component("b2b.CCRReceiver")
@Slf4j
public class CCRReceiver {
//SOME_VARIABLES
@Transactional
@JmsListener(destination = "${tibco.configuration.queues.upsert}", containerFactory = "jmsFactory", concurrency = "${jms.concurrency}")
public void receiveMessage(Message message) {
//DO_SOME_STUFF
}
}

正如您所看到的,我第一次从值表达式中获得目的地,它工作得很好,但后来我不知道如何访问JMSListener并更改它的目的地。

这能做到吗?有办法改变目的地吗?

或者我将不得不以允许我这样做的其他方式来实现这个JMS侦听器?

这应该有效:

  • 给侦听器一个id属性

  • 自动连接JmsListenerEndpointRegistry(或以其他方式获取其参考(

  • registry.getListenerContainer("myListener").stop();

  • registry.getListenerContainer("myListener").shutdown();

  • ((AbstractMessageListenerContainer) registry.getListenerContainer("myListener")).setDestinationName("newOne")

  • registry.getListenerContainer("myListener").initialize();

  • registry.getListenerContainer("myListener").start();

我使用组件Listener Thread解决了这个问题。使用TaskExecutor和ApplicationContext进行管理。您可以在运行时创建。我还在努力。我也会试试加里·拉塞尔的建议。对不起英语。请随意更正。

applicationContext.getBean(ExampleListenerJMS.class);
... 
taskExecutor.execute(exampleListenerJMS);

类监听器"实现Runnable,MessageListener";通过获得自定义连接管理器的实现(activemq服务器不同(。

@Component
@Scope("application")
public class ExampleListenerJMS implements Runnable, MessageListener {
private EspecificManagerJMS jms = new EspecificManagerJMS();
@Override
public void run() {
customAndChekingActions();
}
protected void customAndChekingActions() {
...
try {
Destination destination = jms.getSession().createQueue(queue);
MessageConsumer consumer = jms.getSession().createConsumer(destination);
consumer.setMessageListener(this);
...
} catch (JMSException e) {
e.printStackTrace();
...
}
}
@Override
public void onMessage(Message message) {
...
}

我希望它能帮助你

最新更新