JMS - 从一个消费者到多个消费者
我有一个 JMS 客户端,它生成消息并通过 JMS 队列发送给其唯一的使用者。
我想要的是不止一个消费者收到这些消息。我想到的第一件事是将队列转换为主题,以便当前和新的消费者可以订阅并获取传递给所有人的相同消息。
这显然涉及修改生产者和消费者方面的当前客户端代码。
我还想看看其他选项,例如创建第二个队列,这样我就不必修改现有的消费者。我相信这种方法有一些优点,例如(如果我错了,请纠正我)平衡两个不同队列之间的负载,而不是一个,这可能会对性能产生积极影响。
我想获得有关这些选项以及您可能会看到的缺点/优点的建议。任何反馈都将受到高度赞赏。
I have a JMS client which is producing messages and sending over a JMS queue to its unique consumer.
What I want is more than one consumer getting those messages. The first thing that comes to my mind is converting the queue to a topic, so current and new consumers can subscribe and get the same message delivered to all of them.
This will obviously involve modifying the current clients code in both producer and consumer side of things.
I would like to also look at other options like creating a second queue, so that I don't have to modify the existing consumer. I believe there are advantages in this approach like (correct me if I am wrong) balancing the load between two different queues rather than one, which might have a positive impact on performance.
I would like to get advise on these options and cons / pros that you might see. Any feedback is highly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如您所说,您有几个选择。
如果将其转换为主题以获得相同的效果,则需要使消费者成为持久消费者。如果您的消费者不存在,队列提供的一件事是持久性。这将取决于您使用的 MQ 系统。
如果您想坚持使用队列,您将为每个消费者创建一个队列,并创建一个侦听原始队列的调度程序。
主题的优点
主题的缺点
队列的优点
队列的缺点
在开发消息系统时,我更喜欢主题,因为它给了我最大的力量,但是由于您已经在使用队列,因此需要您更改系统的工作方式来实现主题。
具有多个消费者的队列系统的设计和实现
来源
请记住,您还需要注意其他事项,例如问题异常处理、重新连接到连接以及如果您失去连接,则会排队等。这只是为了让您了解如何完成我所描述的内容。
在真实的系统中,我可能不会在第一次异常时退出。我会让系统继续尽可能地运行并记录错误。正如这段代码所示,如果将消息放入单个消费者队列失败,整个调度程序将停止。
Dispatcher.java
Main.java
You have a few options as you stated.
If you convert it to a topic to get the same effect you will need to make the consumers persistent consumers. One thing the queue offers is persistence if your consumer isn't alive. This will depend on the MQ system you are using.
If you want to stick with queues, you will create a queue for each consumer and a dispatcher that will listen on the original queue.
Pros of Topics
Cons of Topics
Pros of Queues
Cons of Queues
When developing a Messaging System I prefer topics as it gives me the most power, but seeing as you are already using Queues it would require you to change how your system works to implement Topics instead.
Design and Implementation of Queue System with multiple consumers
Source
Keep in mind there are other things you'll need to take care of such as problem exception handling, reconnection to the connection and queues if you lose your connection, etc. This is just designed to give you an idea of how to accomplish what I described.
In a real system I probably wouldn't exit out at the first exception. I would allow the system to continue operating the best it could and log errors. As it stands in this code if putting a message in a single consumers queue fails, the whole dispatcher will stop.
Dispatcher.java
Main.java
您可能不必修改代码;这取决于你怎么写。
例如,如果您的代码使用
MessageProducer
而不是QueueSender
发送消息,那么它将适用于主题和队列。同样,如果您使用MessageConsumer
而不是QueueReceiver
。本质上,在 JMS 应用程序中使用非特定接口与 JMS 系统交互是一种很好的做法,例如
MessageProducer
、MessageConsumer
、Destination
等等。如果是这种情况,那么这只是一个配置问题。You may not have to modify the code; it depends on how you wrote it.
For example, if your code sends messages using
MessageProducer
rather thanQueueSender
, then it will work for topics as well as queues. Similarly if you usedMessageConsumer
rather thanQueueReceiver
.Essentially, it is good practice in JMS applications to use non-specific interfaces to interact with the JMS system, such as
MessageProducer
,MessageConsumer
,Destination
, etc. If that's the case, it's a "mere" matter of configuration.