JMS learning six (ActiveMQ messaging model)

ActiveMQ supports two distinct messaging models: PTP (ie point-to-point model) and Pub/Sub (ie publish/subscribe model), called: PTP Domain and Pub/Sub Domain respectively.

1. PTP message transmission model

1. PTP (using Queue is the queue target) Messages are sent from a producer to a consumer. In this delivery model, the destination is a queue. The message is first delivered to the queue destination, and then according to the queue delivery policy, the message is delivered from the queue to one of the consumers registered with the queue, one message at a time. There is no limit to the number of producers that can send messages to a queue destination, but each message can only be sent to and successfully consumed by one consumer. If there are no consumers registered with the queue destination, the queue will retain messages it receives and deliver messages to a consumer when it registers with the queue.

 

2. PTP messaging model features:

(1) If some messages have been received but not signed when the session is closed, these messages will be pushed again when the consumer connects to the same queue next time. (The consumption of the message is divided into three stages, if any one is not satisfied, it will be regarded as unconsumed)

(2) In the last article, we learned about message filters. If the messages do not meet the consumption conditions, these messages will not be consumed, and these messages will remain in the queue.

(3) The queue can store messages for a long time until there is a consumer to consume the message, and the consumer does not need to care about the connection status of the message producer, which fully reflects the advantages of asynchronous transmission!

 

2. Pub/Sub messaging model (send/subscribe model)

1. Pub/Sub (using Topic is the topic target) Messages are sent from one producer to any number of consumers. In this delivery model, the target is a topic. Messages are first delivered to the topic destination and then to all active consumers that have subscribed to this topic. There is no limit to the number of producers that can send messages to a topic target, and each message can be sent to any number of subscribed consumers. Topic targets also support the concept of durable subscriptions. A durable subscription means that the consumer is registered with the topic target, but the consumer can be inactive while the message is being delivered. When this consumer is active again, it will receive this information. If there are no consumers already registered with the topic target, the topic does not retain messages it receives unless there are inactive consumers registered with durable subscriptions.

2. Pub/Sub messaging model features:

(1) Message subscriptions are divided into non-durable subscriptions and persistent subscriptions. Non-durable subscriptions can only be received when the client (message consumer) is in an active state, that is, when the message consumer and the message service remain connected. The message of a topic (topic), and the message sent to the topic when the message consumer is offline will be lost and will never be received. A durable subscription can receive messages sent to a topic during the disconnection of the message consumer and the message server

The message is that the message consumer can receive the message sent during this period when it connects to the message service for the first time.

(2) Like the PTP mode, there is a selector when consuming messages. Messages that do not meet the conditions will not be consumed, and messages that are not consumed will remain in the Topic.

(3) In the non-durable subscription state, an unsigned message cannot be recovered or resent, and only a durable subscription can recover or resend an unconsumed message.

(4) In the application, if message loss cannot be tolerated, durable subscription is used, and if message loss can be tolerated, durable subscription is used. The performance of durable subscription and non-durable subscription is still very different, because it is necessary to The first requirement for durable subscription is that the message should be persisted, while non-durable subscription does not need to persist the message, so the efficiency is higher, so you should be careful which subscription method you use in the application, think twice! ! !

 

3. Examples

I won't post the code here because the demo of the two transmission modes has been written in the second article. If you need to, you can take a look: PTP, Pub/Sub transmission model article

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325117465&siteId=291194637