简体   繁体   中英

What's the difference between SimpleMessageListenerContainer and DirectMessageListenerContainer in Spring AMQP?

What's the difference between SimpleMessageListenerContainer and DirectMessageListenerContainer in Spring AMQP? I checked both of their documentation pages, SimpleMessageListenerContainer has almost no explanation on inner workings, and DirectMessageListenerContainer has the following explanation:

The is not so simple. 并不是那么简单。 Recent changes to the rabbitmq java client has facilitated a much simpler listener container that invokes the listener directly on the rabbit client consumer thread. There is no txSize property - each message is acked (or nacked) individually.

I don't really understand what these mean. It says listener container that invokes the listener directly on the rabbit client consumer thread . If so, then how does SimpleMessageListenerContainer do the invocation?

I wrote a small application and used DirectMessageListenerContainer and just to see the difference, I switched to SimpleMessageListenerContainer , but as far as I can see there was no difference on RabbitMQ side. From Java side the difference was in methods ( SimpleMessageListenerContainer provides more) and logs ( DirectMessageListenerContainer logged more stuff)

I would like to know the scenarios to use each one of those.

The SMLC has a dedicated thread for each consumer (concurrency) which polls an internal queue. When a new message arrives for a consumer on the client thread, it is put in the internal queue and the consumer thread picks it up and invokes the listener. This was required with early versions of the client to provide multi-threading. With the newer client that is not a problem so we can invoke the listener directly (hence the name).

There are a few other differences aside from txSize .

See Choosing a Container .

In the DirectMessageListenerContainer some of the logic is moved into the AMQP implementation as opposed to ListenerContainer as is SimpleMessageListenerContainer

This is what the Javadocs in SimpleMessageListenerContainer say for setTxSize() -

/**
 * Tells the container how many messages to process in a single transaction (if the channel is transactional). For
 * best results it should be less than or equal to {@link #setPrefetchCount(int) the prefetch count}. Also affects
 * how often acks are sent when using {@link AcknowledgeMode#AUTO} - one ack per txSize. Default is 1.
 * @param txSize the transaction size
 */

The client sends an ack every time txSize number of messages are processed. This is controlled in the method

private boolean doReceiveAndExecute(BlockingQueueConsumer consumer) throws Throwable { //NOSONAR

    Channel channel = consumer.getChannel();

    for (int i = 0; i < this.txSize; i++) {

        logger.trace("Waiting for message from consumer.");
        Message message = consumer.nextMessage(this.receiveTimeout);
        .
        .

In the newer implementations, each message is acked on the thread directly and based on the transactional model (Single or publisher confirms ) the consumer sends Acknowledgments to Rabbit MQ

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM