In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "what is SimpleMessageListenerContainer and DirectMessageListenerContainer". In daily operation, I believe that many people have doubts about what is SimpleMessageListenerContainer and DirectMessageListenerContainer. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the questions of "what is SimpleMessageListenerContainer and DirectMessageListenerContainer?" Next, please follow the editor to study!
SimpleMessageListenerContainer and DirectMessageListenerContainer are used in the development of RabbitMQ consumer code.
In versions prior to version 2.0, there was only one MessageListenerContainer, that is, SimpleMessageListenerContainer; 2.0, followed by a second container-DirectMessageListenerContainer.
1. SimpleMessageListenerContainer
By default, the listener container starts a single consumer who receives messages from the queue. From the previous documentation, we know that there are many properties that control concurrency.
The simplest is concurrentConsumers, which only creates (fixed) the number of consumers that will process messages concurrently.
In addition, a new attribute maxConcurrentConsumers has been added, and the container will dynamically adjust concurrency based on the workload. This works with four additional attributes: continutiveactivetrigger, startConsumerMinInterval, continutiveidletrigger, and stopConsumerMinInterval.
1.1. By default, the algorithm for adding consumers works as follows
If the maxConcurrentConsumers has not been reached and the existing user has been active for 10 consecutive cycles and at least 10 seconds have elapsed since the last user started, a new user will be started. If the consumer receives at least one message in the txSize *, the consumer is considered to be active.
1.2. By default, the algorithm for reducing consumers works as follows
If more than one concurrentConsumers is running and a consumer detects 10 consecutive timeouts (idle), and the previous consumer stopped at least 60 seconds ago, the consumer will stop. The timeout depends on the receiveTimeout and txSize properties. If the consumer does not receive any message in txSize *, it is considered idle. Therefore, in the case of a default timeout (1 second) and a txSize of 4, stopping the user will be considered after 40 seconds of idle time (4 timeouts correspond to 1 idle detection).
1.3.Configuring @ Beanpublic SimpleRabbitListenerContainerFactory simpleRabbitListenerContainerFactory (ConnectionFactory connectionFactory) {SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory (); factory.setConnectionFactory (connectionFactory); / / initializing the number of consumers factory.setConcurrentConsumers (this.concurrentConsumers); / / maximum number of consumers factory.setMaxConcurrentConsumers (this.maxConcurrentConsumers); / / Manual confirmation message factory.setAcknowledgeMode (AcknowledgeMode.MANUAL); factory.setErrorHandler (rabbitErrorHandler); return factory;} II. DirectMessageListenerContainer
With DirectMessageListenerContainer, you need to make sure that ConnectionFactory is configured with a task executor that has enough threads in all listener containers that use the ConnectionFactory to support the required concurrency. The default connection pool size is only 5.
Concurrency is based on configured queues and consumersPerQueue. Each consumer of each queue uses a separate channel, and concurrency is controlled by the rabbit client library; by default, it uses five thread pools; you can configure taskExecutor to provide the maximum concurrency you need.
2.1. configuration as follows: @ Beanpublic DirectRabbitListenerContainerFactory directRabbitListenerContainerFactory (ConnectionFactory connectionFactory) {DirectRabbitListenerContainerFactory factory = new DirectRabbitListenerContainerFactory (); factory.setConnectionFactory (connectionFactory); / / number of consumers per queue factory.setConsumersPerQueue (this.consumersPerQueue); / / Manual confirmation message factory.setAcknowledgeMode (AcknowledgeMode.MANUAL); factory.setErrorHandler (rabbitErrorHandler); return factory;} III. Service comparison
SimpleMessageListenerContainer provides the following features, but DirectMessageListenerContainer does not:
TxSize- uses SimpleMessageListenerContainer, which you can set to control the number of messages delivered in a transaction and / or reduce the number of ack, but this may result in an increase in the number of repeated deliveries after failure. Like txSize and SimpleMessageListenerContainer, DirectMessageListenerContainer has mesagesPerAck, which can be used to reduce ack, but not for transactions-each message is delivered and packaged in a separate transaction.
Maxconcurrentconsumer and consumer scaling intervals / triggers-there is no automatic scaling in DirectMessageListenerContainer; however, it allows you to programmatically change the consumersPerQueue property and adjust the consumer accordingly.
However, compared with SimpleMessageListenerContainer, DirectMessageListenerContainer has the following advantages:
It is more efficient to add and remove queues at run time; with SimpleMessageListenerContainer, the entire consumer thread is restarted (all consumers cancel and recreate); and for DirectMessageListenerContainer, unaffected consumers are not canceled.
The context switching between RabbitMQ client thread and consumer thread is avoided.
Threads are shared across consumers, rather than having a dedicated thread for each consumer in SimpleMessageListenerContainer.
At this point, the study of "what is SimpleMessageListenerContainer and DirectMessageListenerContainer" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.