[英]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.