Spring amqp batch receiving messages

怎甘沉沦 提交于 2019-12-12 14:14:04

问题


We use Spring AMQP to read message from RabbitMQ, right now we only read one message at a time off the queue, is there anyway I can read multiple messages off the queue then process the batch?

I see there is a BatchingStrategy available in Spring, how can i plugin that one into the connectionFactory?

here is my code:

CachingConnectionFactory connectionFactory = new CachingConnectionFactory(host, port);
        connectionFactory.setUsername(username);
        connectionFactory.setPassword(password);

SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        JsonMessageConverter converter = new JsonMessageConverter();
        DefaultClassMapper defaultClassMapper = new DefaultClassMapper();
        defaultClassMapper.setDefaultType(Message.class);
        converter.setClassMapper(defaultClassMapper);
        factory.setMessageConverter(converter);
        factory.setConcurrentConsumers(3);

...

public class Processor implements IChannelProcessor {
@Override
    public void process(Message message) {
        validateMessageEvent(message);
        // process the message

回答1:


The BatchingStrategy is for putting multiple message segments into a single amqp message; the container automatically debatches such messages. It won't help for your purposes.

To do what you want; instead of using POJO messaging (@RabbitListener) you would have to use a message listener container with acknowledgeMode set to MANUAL with a ChannelAwareMessageListener.

When you have processed your batch of messages, call basicAck on the channel to ack all the messages in the batch.



来源:https://stackoverflow.com/questions/33003502/spring-amqp-batch-receiving-messages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!