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



在尝试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);
}
}

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

IntelIntel(R) Management Engine ComponentsDAL;C:WINDOWSSystem32OpenSSH;C:Program Files (x86)NVIDIA CorporationPhysXCommon;C:Program Files (x86)gnupgbin;C:UsersLesRAppDataLocalMicrosoftWindowsApps;c:Gradlegradle-5.0bin;;C:Program FilesJetBrainsIntelliJ IDEA 2018.3bin;;.]
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

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

完成此操作后,仅调用了两个声明的接收方法中的一个,并且在所有测试运行中都保持了消息顺序。

相关内容

  • 没有找到相关文章

最新更新