【问题标题】:Possibility of handling multiple queue using multiple jms listeners使用多个 jms 侦听器处理多个队列的可能性
【发布时间】:2021-10-09 04:40:43
【问题描述】:

我有以下 Java 进程正在侦听 ActiveMQ 的 MyMessageQueue 队列。我可以看到使用此语句 System.out.println("Message Retrieved is:" +message); 检索到的消息 在processBrokerQueues 方法内。

@Component
public class MessageConsumer {

    @Autowired
    private JavaMailSender javaMailSender;

    // one instance, reuse
    private final CloseableHttpClient httpClient = HttpClients.createDefault();

    private static Connection connection;

    // URL of the JMS server. DEFAULT_BROKER_URL will just mean that JMS server is on localhost
    private static String url = ActiveMQConnection.DEFAULT_BROKER_URL;
    private static String subject = "MyMessageQueue"; //Queue Name
    // default broker URL is : tcp://localhost:61616"
    private static ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
    private static final Logger logger = LoggerFactory.getLogger(MessageConsumer.class);
    private static final DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

    // Working Code with JMS 2.0
    @JmsListener(destination = "MyMessageQueue")
    public void processBrokerQueues(String message) throws DaoException, JMSException {


        System.out.println("Message Retrieved is:" + message);


    }

    private void createConnection() throws JMSException {
        // Getting JMS connection from the server
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
        connection = connectionFactory.createConnection();
    }

    private void close() throws IOException {
        httpClient.close();
    }

}

我想知道,如果我想听 20-30 个不同的队列,添加多个具有不同队列名称的 @JmsListener(destination = "MyMessageQueue") 是否正确,例如 MessageQueue1, MessageQueue2 ....... 等等,直到 MessageQueue30

【问题讨论】:

  • 这能回答你的问题吗? Adding Dynamic Number of Listeners(Spring JMS)
  • @JustinBertram 不确定。我想知道是否必须切换到主题才能做到这一点?
  • @JmsListener 可以配置为队列或主题。没关系。答案只是碰巧使用了主题。
  • @JustinBertram 好的,我会测试一下。还有一个问题,在生产者方面,我可能会向 10 个或更多不同的队列发送 10 个或更多不同的消息。在这种情况下,我需要配置 @JmsListner 来监听 10 个或更多队列。想问制作方也没有Topic相关要求吗?谢谢!
  • 我不明白你在问什么。 “生产者方面的主题相关要求”是什么意思?这是 JMS。您可以使用队列主题。没有什么要求您使用主题。

标签: java activemq spring-jms


【解决方案1】:

您可以使用通配符主题名称。 一个简单的JUnit测试如下...

public EmbeddedActiveMQBroker embeddedBroker = new EmbeddedActiveMQBroker();
@Autowired JmsTemplate jmsTemplate;

@Test
void testListener() throws Exception{
    jmsTemplate.convertAndSend("MyMessageQueue.1", "Test");

}

@JmsListener(destination = "MyMessageQueue.>")
public void processBrokerQueues(ActiveMQTextMessage msg) throws Exception {
    System.out.println("Message Retrieved is:" + msg.getText() + " TopicName:" + msg.getDestination().getPhysicalName());
}

相关依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-activemq</artifactId>
    <version>2.5.3</version>
</dependency>
<dependency>
    <groupId>org.apache.activemq.tooling</groupId>
    <artifactId>activemq-junit</artifactId>
    <version>5.16.2</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.13</version>
    <scope>test</scope>
</dependency>

【讨论】:

猜你喜欢
  • 2016-12-31
  • 2015-12-05
  • 1970-01-01
  • 1970-01-01
  • 2013-06-16
  • 2018-04-08
  • 2013-09-24
  • 1970-01-01
  • 2019-03-14
相关资源
最近更新 更多