JMS Learning (eight)-ACTIVEMQ Consumer using push or pull to get a message

Source: Internet
Author: User
Tags prefetch

ACTIVEMQ is a message middleware, there are two ways for consumers to get messages from the message middleware:

①push mode: The message middleware actively pushes the message to the consumer; ②pull mode: The consumer actively pulls messages to the message middleware. Read the explanation of the push method on the official website:

Pull Messages adds significant latency per message.

With push, messages can be sent to consumers as quickly as possible (stream messages to consumers as fast as possible)

The Pull method increases the delay of the message, which means that the message arrives at the consumer for a long time (adds significant latency per message).

However, the push approach has a downside: if the consumer's ability to process messages is weak (a message takes a long time to process), and the message middleware constantly push messages to consumers, the consumer's buffer may overflow.

How did the ACTIVEMQ solve the problem? That's prefetch limit .

Prefetch limit Specifies how many messages you can push (push) to the consumer at a time.

Once the prefetch limit is reached, no more messages be dispatched to the consumer until the consumer starts sending back Acknowledgements of messages (to indicate, the message has been processed)

When the number of push messages reaches the value specified by Perfetch limit, the consumer has not returned an ACK to the message middleware, and the message middleware will no longer continue to push messages to consumers.

How much is the value of the prefetch limit set to fit? Depending on the application scenario.

If you have very few messages and each message takes a very long time to process you might want to set the prefetch value To 1 so, a consumer is given one message at a time.

If the number of messages is small (the rate at which producers produce messages is unpleasant), but each message consumer takes a long time to process, then the prefetch limit is set to 1 more appropriate. In this way, the consumer receives only one message at a time, and when it finishes processing the message, it sends an ACK to the message middleware, and the message middleware pushes the next message to the consumer.

What does the prefetch limit set to 0 mean?

Specifying a prefetch limit of zero means the consumer would poll for more messages, one at a time, instead of the message Being pushed to the consumer.

This means that at this point, the consumer polls the message middleware to get the message. It's not push, it's the pull way. That is, consumers take the initiative to pull messages from the message middleware.

Perfetch limit is the value of "message prefetching", which is set for how message middleware sends messages to consumers. It is also related to the way in which the consumer responds to the message middleware with acknowledgment ack (response): For example, the consumer is after each consumption of a message to the message middleware to confirm it? Or the use of "delayed acknowledgement"---that is, the method of batch confirmation (after consuming several messages, unified re-ack). This is Optimized acknowledge.

ActiveMQ can acknowledge receipt of messages back to the broker in batches (to improve performance).

Quote: "If Prefetchack is true, then prefetch must be greater than 0, and when Prefetchack is false, you can specify Prefetch as 0 and a positive number of any size."
However, when Prefetch=0 is, indicates that consumer will use pull (pulls) to obtain the message from the broker side, the broker side will not actively push the message to the client side until the client sends Pullcommand;
When Prefetch>0, the broker push mode is turned on, so long as the client side consumes and ACK a certain message, it will immediately push to the client side multiple messages. ”

So, how do you use push or pull in your program?

In terms of blocking, consumers have two ways of getting messages. Synchronous mode and asynchronous mode.

Synchronization is using the receive () method of the Activemqmessageconsumer. The asynchronous way is to use the consumer to implement the MessageListener interface, listen to the message.

When you use the synchronous receive () method to get a message, prefetch limit can be set to 0, or it can be set to greater than 0

A prefetch limit of zero means that the receive () method will first send a pull instruction and block until the broker side returns a message, which also means that the message can only be fetched individually (similar to request<-> Response) "

Prefetch limit greater than 0 means: "Broker-side will bulk push to the client a certain number of messages (<= prefetch), the client side will put these messages (Unconsumedmessage) into a local queue, as long as the queue has messages, the Receive method returns immediately , and when a certain amount of the message is ACK, the broker side continues the bulk push message to the client side. ”

When using MessageListener to get a message asynchronously, prefetch limit must be greater than 0. Because, prefetch limit equals zero means that the message middleware does not actively give the consumer push messages, and at this time the consumer with MessageListener passive access to the message (do not actively polling the message). The two are contradictory.

In addition, there is a point to note that consumers using synchronous acquisition of messages (Receive method) and asynchronous method of obtaining messages (MessageListener), the timing of confirmation of the message is different.

For specific reference: this article.

Reference: ACTIVEMQ messaging mechanism and an explanation of the ACK mechanism

JMS Learning (eight)-ACTIVEMQ Consumer using push or pull to get a message

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.