简体   繁体   English

如何配置和启用 JMS 侦听器以根据某些条件使用消息?

[英]How to configure and enable a JMS Listener to consume messages based on some condition?

I have two listeners( fooMessages and barMessages ) in my application, and both of them are connected to same queue.我的应用程序中有两个侦听器( fooMessagesbarMessages ),它们都连接到同一个队列。 At one point of time only one listener will consume the message from the queue based on some condition.在某个时间点,只有一个侦听器会根据某种条件使用队列中的消息。 In application.yml file if fooEnabled is set to true then fooMessages listener should consume the messages and if fooEnabled is set to false then barMessages listener should consume the messages.application.yml文件中,如果fooEnabled设置为true ,则fooMessages侦听器应使用消息,如果fooEnabled设置为false ,则barMessages侦听器应使用消息。

Listeners:听众:

@JMSListener(destination="${queueName}", selector = "${selectorName}")
public void fooMessages(Message message) {
   // foo logic
}
    
@JMSListener(destination="${queueName}", selector = "${selectorName}")
public void barMessages(Message message) {
   // bar logic
}

application.yml file: application.yml .yml 文件:

queueName: myqueue
selectorName: "priority=medium"
fooEnable: true

How can I configure the listener to handle this scenario?如何配置侦听器来处理这种情况?

  1. Give each listener an id给每个听众一个id
  2. Configure the container factory to not automatically start the listeners https://docs.spring.io/spring-boot/docs/current/reference/html/application-properties.html#application-properties.integration.spring.jms.listener.auto-startup Configure the container factory to not automatically start the listeners https://docs.spring.io/spring-boot/docs/current/reference/html/application-properties.html#application-properties.integration.spring.jms.listener.自动启动
  3. Start the container(s) manually using the JmsListenerEndpointRegistry bean - registry.getListenerContainer("fooListener").start();使用JmsListenerEndpointRegistry bean 手动启动容器 - registry.getListenerContainer("fooListener").start();
@Bean
ApplicationRunner runner(@Value("${fooEnabled}") boolean fooEnabled, 
        @Value("${barEnabled}") boolean barEnabled, JmsListenerEndpointRegistry registry) {
    return args -> {
        if (fooEnabled) ...
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 仅当某些条件为真时才使用来自 Kafka 的消息 - Consume messages from Kafka only if some condition true 如何使用 Spring JMS 使用来自 websphere 应用程序服务器的 JMS 消息 - How to consume JMS messages from the websphere application server using Spring JMS Camel JMS:连接到主题时如何配置自定义消息侦听器 - Camel JMS : how to configure custom message listener when connecting to a topic 使用服务或侦听器在ActiveMQ中使用消息 - Consume Messages in ActiveMQ with a service or a listener 如何在运行时启用/禁用JMS侦听器? 我可以创建一个可以在运行时启用/禁用侦听器的API吗? - How to enable/disable JMS listener on runtime? Can I create one api which enables/disables listener on runtime? 如何在Jboss 6中配置JMS? - How to configure JMS in Jboss 6? 在Spring JMS消息中配置JSON输出 - Configure JSON output in Spring JMS messages ActiveMQ:如何根据指定的时间段使用队列中的消息 - ActiveMQ:How to consume the Messages on a queue based on a specified time period 如何在Java EE Web应用程序web.xml中配置JMS侦听器? - How to configure JMS Listener in Java EE web application web.xml? 为什么我的JMS客户端不使用主题中的消息? - Why does my JMS client not consume messages from the Topic?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM