簡體   English   中英

在 Spring Boot 上即時修改 @JMSListener 目標

[英]Modify @JMSListener destination on-the-fly on Spring Boot

我開發了一個@JMSListener,它從 Java 屬性中獲取目的地並且工作得很好。

但是現在我需要能夠在運行時更改隊列的“目的地”而不必重置整個應用程序,即使我在運行時修改屬性,隊列“目的地”也不會改變。

這是我們如何實現@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();

我使用組件偵聽器線程解決了這個問題。 使用 TaskExecutor 和 ApplicationContext 進行管理。 您可以在運行時創建。 我還在努力。 我也會嘗試 Gary Russell 的建議。 對不起英語。 隨意糾正。

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) {
...
}

我希望它對你有幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM