简体   繁体   English

如何在 SpringBoot:RabbitMQ 中每个队列只配置一个消费者?

[英]How to configure only one consumer per queue in SpringBoot:RabbitMQ?

Application overview: There can be multiple dynamic queues created.应用概述:可以创建多个动态队列。 Each queue should only have one consumer.每个队列应该只有一个消费者。 Only when the consumer has finished processing one message, only then the consumer should pick up the another message.只有当消费者处理完一条消息时,消费者才应该拿起另一条消息。

Here is my configuration:这是我的配置:

@EnableRabbit
@Configuration
public class RabbitMqSenderConfig {
    private static final Logger logger = LoggerFactory.getLogger(RabbitMqSenderConfig.class);

    @Value("${spring.rabbitmq.addresses}")
    private String addressURL;


    @Bean
    public ConnectionFactory connectionFactory() throws URISyntaxException {
        return new CachingConnectionFactory(new URI(addressURL));
    }

    /**
     * Required for executing adminstration functions against an AMQP Broker
     */
    @Bean
    public AmqpAdmin amqpAdmin() throws URISyntaxException {
        return new RabbitAdmin(connectionFactory());
    }

    @Bean
    public MessageConverter jsonMessageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    @Bean
    public AmqpTemplate rabbitTemplate() throws URISyntaxException {
        final RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory());
        rabbitTemplate.setMessageConverter(jsonMessageConverter());
        return rabbitTemplate;
    }


}

Listener:听众:

public class RabbitMqConsumer extends SimpleMessageListenerContainer {

    public void startConsumers() throws Exception {
        super.doStart();
    }
}

Handle message method:处理消息方法:

public void handleMessage(DeploymentJob deploymentJob) {
    // Deployment running, takes almost 10-15 minutes each
    try {
        System.out.println("deploymentJob.getSocketHandler().getSessions() -> "+deploymentJob.getSocketHandler().getSessions());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Everytime a pull request is created, I send the details to rabbitMQ in a queue named after target branch, so that all the pull requests gets validated in queue, and I create a consumer like this.每次创建拉取请求时,我都会将详细信息发送到以target分支命名的队列中的rabbitMQ,以便所有拉取请求在队列中得到验证,并像这样创建一个使用者。

    rabbitTemplate.convertAndSend(repoName, queue_name, deploymentJob);
    RabbitMqConsumer container = new RabbitMqConsumer();
    container.setConnectionFactory(rabbitMqSenderConfig.connectionFactory());
    container.setQueueNames(queue_name);
    container.setConcurrentConsumers(1);
    container.setMessageListener(new MessageListenerAdapter(new ConsumerHandler(), new Jackson2JsonMessageConverter()));
    container.startConsumers();

The issue here is it is creating new consumers for each and every message in the queue, which I want to avoid.这里的问题是它正在为队列中的每条消息创建新的消费者,我想避免这种情况。 And I am not finding a way to limit only one consumer per queue.而且我没有找到一种方法来限制每个队列只有一个消费者。 or to check if the consumer does not exists then don't create a new instance of RabbitMqConsumer .或者检查消费者是否不存在然后不要创建RabbitMqConsumer的新实例。 Is this possible?这可能吗?

Consumer section消费者板块

在此处输入图片说明

Just use a ConcurrentHashMap keyed by the queue name to determine which queues you already have a consumer for;只需使用以队列名称为键的ConcurrentHashMap来确定您已经拥有消费者的队列; when you create a new consumer, add it to the map.创建新消费者时,将其添加到地图中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获取所有消费者队列 rabbitMq 的列表? - How to get a list of all consumer queue rabbitMq? SpringBoot + Rabbitmq - DLQ 队列不工作 - SpringBoot + Rabbitmq - DLQ queue not working 如何在Spring Boot Rabbitmq中分别配置生产者和消费者? - How to separately configure producer and consumer in spring boot rabbitmq? 骆驼 jms 消费者抛出 RejectedExecutionException,但仅在一个特定队列上 - camel jms consumer throws RejectedExecutionException, but only on one particular queue 如何从 RabbitMQ 死信队列中一一消费消息 - How to consume message from RabbitMQ dead letter queue one by one 使用 rabbitmq 时如何分别配置消费者和生产者 spring 启动应用程序? - How to configure separately both consumer and producer spring boot app when using rabbitmq? 如何在一次调用中获取 RabbitMQ 中单个队列中存在的所有消息 - How to get all the messages present in a single Queue in RabbitMQ in one call 如何在 SpringBoot 中配置 CharacterEncodingFilter? - How to configure CharacterEncodingFilter in SpringBoot? RabbitHandler 在 Spring 中创建消费者并重试致命异常以用于侦听 RabbitMQ 的队列 - RabbitHandler to create consumer and retry on Fatal Exception in Spring for queue on listening to RabbitMQ 消费者线程 state 当 rabbitmq 中给定队列中没有可用消息时 - Consumer thread state when no messages are available in the given queue in rabbitmq
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM