是否可以使用Spring Boot和JmsTemplate在ActiveMQ主题上强制执行消息顺序?

问题描述 投票:0回答:1

在使用Spring Boot,ActiveMQ和JmsTemplate时,我注意到消息顺序似乎并不总是保留。在阅读ActiveMQ时,提供“消息组”作为在发送到主题时保留消息顺序的潜在解决方案。有没有办法用JmsTemplate做到这一点?

添加注意:我开始认为JmsTemplate很适合“推出”,但是有太多问题。

下面发布的示例代码和控制台输出...

@RestController
public class EmptyControllerSB {

    @Autowired
    MsgSender msgSender;

    @RequestMapping(method = RequestMethod.GET, value = { "/v1/msgqueue" })
    public String getAccount() {
        msgSender.sendJmsMessageA();
        msgSender.sendJmsMessageB();
        return "Do nothing...successfully!";
    }
}

@Component
public class MsgSender {

    @Autowired
    JmsTemplate jmsTemplate;

    void sendJmsMessageA() {
        jmsTemplate.convertAndSend(new ActiveMQTopic("VirtualTopic.TEST-TOPIC"), "message A");
    }

    void sendJmsMessageB() {
        jmsTemplate.convertAndSend(new ActiveMQTopic("VirtualTopic.TEST-TOPIC"), "message B");
    }
}

@Component
public class MsgReceiver {

    private final String consumerOne = "Consumer.myConsumer1.VirtualTopic.TEST-TOPIC";
    private final String consumerTwo = "Consumer.myConsumer2.VirtualTopic.TEST-TOPIC";

    @JmsListener(destination = consumerOne )
    public void receiveMessage1(String strMessage) {
        System.out.println("Received on #1a -> " + strMessage);
    }

    @JmsListener(destination = consumerOne )
    public void receiveMessage2(String strMessage) {
        System.out.println("Received on #1b -> " + strMessage);
    }

    @JmsListener(destination = consumerTwo )
    public void receiveMessage3(String strMessage) {
        System.out.println("Received on #2 -> " + strMessage);
    }
}

这是控制台输出(注意第一顺序的输出顺序)......

\Intel\Intel(R) Management Engine Components\DAL;C:\WINDOWS\System32\OpenSSH\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files (x86)\gnupg\bin;C:\Users\LesR\AppData\Local\Microsoft\WindowsApps;c:\Gradle\gradle-5.0\bin;;C:\Program Files\JetBrains\IntelliJ IDEA 2018.3\bin;;.]
2019-04-03 09:23:08.408  INFO 13936 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-04-03 09:23:08.408  INFO 13936 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 672 ms
2019-04-03 09:23:08.705  INFO 13936 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-04-03 09:23:08.845  INFO 13936 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2019-04-03 09:23:08.877  INFO 13936 --- [           main] mil.navy.msgqueue.MsgqueueApplication    : Started MsgqueueApplication in 1.391 seconds (JVM running for 1.857)
2019-04-03 09:23:14.949  INFO 13936 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2019-04-03 09:23:14.949  INFO 13936 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2019-04-03 09:23:14.952  INFO 13936 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 3 ms
Received on #2 -> message A
Received on #1a -> message B
Received on #1b -> message A
Received on #2 -> message B

<HIT DO-NOTHING ENDPOINT AGAIN>
Received on #1b -> message A
Received on #2 -> message A
Received on #1a -> message B
Received on #2 -> message B
spring spring-boot activemq jmstemplate
1个回答
0
投票

BLUF - 将“?consumer.exclusive = true”添加到JmsListener注释的目标声明中。

似乎解决方案并不复杂,特别是如果放弃ActiveMQ的“消息组”或“独占消费者”。 “消息组”的缺点是发送方必须事先了解消息使用者的潜在分区。如果生产者具有这方面的知识,那么“消息组”是一个很好的解决方案,因为解决方案在某种程度上独立于消费者。

但是,通过让消费者在队列中声明“独占消费者”,可以从消费者侧实现类似的解决方案。虽然我没有在JmsTemplate实现中看到任何直接支持这一点的内容,但似乎Spring的JmsTemplate实现将队列名称传递给ActiveMQ,然后ActiveMQ“做正确的事情”并强制执行独占的消费者行为。

所以...

更改以下内容......

private final String consumerOne = "Consumer.myConsumer1.VirtualTopic.TEST-TOPIC";

至...

private final String consumerOne = "Consumer.myConsumer1.VirtualTopic.TEST-TOPIC";?consumer.exclusive=true

一旦我这样做,只调用了两个声明的接收方法中的一个,并且在我的所有测试运行中都维护了消息顺序。

© www.soinside.com 2019 - 2024. All rights reserved.