A comparison between
Queue
and
Topic
1 ,JMS Queue Execute Load balancer semantics:
A message can only be received by one consumer .
If there is no consumeravailable when the message is sent, it will be saved until the consumer that can handle the message is available.
If a consumer receives a message and does not respond to it, then the message will be transferred to another consumer .
A Queue can have a lot of consumerand load balance in multiple available consumer .
How to understand: if a consumer receives a message and does not respond to it, then the message will be transferred to another consumer .
See the procedure in the annex:
in the Receiver.java, Create a consumer and achieve a synchronous receive:
while (true) {
// 设置接收者接收消息的时间,为了便于测试,这里谁定为100s
TextMessage message = (TextMessage) consumer.receive(18000);
// Object message = (Object) consumer.receive(28000);
if (null != message) {
System.out.println("订阅者1:收到消息-->" + message.getText());
} else {
System.out.println("订阅者1:运行结束...\n");
break;
}
}
in the Receiver2.java, the program that created the consumer but commented out the receiving part:
//while (true) {
//// 设置接收者接收消息的时间,为了便于测试,这里谁定为100s
//TextMessage message = (TextMessage) consumer.receive(18000);
//if (null != message) {
//System.out.println("订阅者2:收到消息-->" + message.getText());
//} else {
//System.out.println("订阅者2:消费者运行结束...\n");
//break;
//}
//}
First, separate thereceiver andReceiver2 Run and then run sender, you will get the following results:
Special Note: Subscriber 2 stops after subscriber 1, how to stop before Subscribers 1, the result is not the same.
When you turn on receive1 again, you will see:
if
Subscriber 2 stops before Subscriber 1, and the following results will appear:
2 ,Topic implement Publish and Subscribe semantics:
When a message is publish , it will be sent to all interested subscribers, so 0 to multiple Subscriber will receive a copy of the message. However, when the message agent receives a message, only the subscriber that activates the subscription can get a copy of the message.
3 , respectively, corresponding to two message modes:
Point-to-Point ( Point-to-point ), Publisher/subscriber Model ( publisher / subscriber )
Where in publicher/subscriber mode There are nondurable subscription(non-persistent subscriptions) and Durable subscription ( persistent subscription ) 2 kinds of message processing methods.
From for notes (Wiz)
List of attachments
"Activemq-5" activemq Comparison of learning-queue and topic