我希望我的应用同时处理从RabbitMQ接收到的多条消息。 我可能已经尝试了所有的google-page-1解决方案,但无法正常工作。 这是我的设置:
POM.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
</parent>
.
.
.
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
</dependency>
application.properties:
#############################
# RabbitMQ #
#############################
#AMQP RabbitMQ configuration
spring.rabbitmq.host=zzzzzzzz
spring.rabbitmq.port=5672
spring.rabbitmq.username=zzzzzzz
spring.rabbitmq.password=zzzzzzz
#Rabbit component names
com.cp.neworder.queue.name = new-order-queue-stg
com.cp.neworder.queue.exchange = new-order-exchange-stg
com.cp.completedorder.queue.name = completed-order-queue
com.cp.completedorder.queue.exchange = completed-order-exchange
#Rabbit MQ concurrect consumers config
spring.rabbitmq.listener.simple.concurrency=3
spring.rabbitmq.listener.simple.retry.initial-interval=3000
配置文件:
@Configuration
public class RabbitMQConfig {
@Value("${com.cp.neworder.queue.name}")
private String newOrderQueueName;
@Value("${com.cp.neworder.queue.exchange}")
private String newOrderExchangeName;
@Bean
Queue queue() {
return new Queue(newOrderQueueName, true);
}
@Bean
TopicExchange exchange() {
return new TopicExchange(newOrderExchangeName);
}
@Bean
Binding binding(Queue queue, TopicExchange exchange) {
return BindingBuilder.bind(queue).to(exchange).with(newOrderQueueName);
}
@Bean
SimpleMessageListenerContainer container(ConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter) {
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
container.setQueueNames(newOrderQueueName);
container.setMessageListener(listenerAdapter);
return container;
}
@Bean
MessageListenerAdapter listenerAdapter(OrderMessageListener receiver) {
return new MessageListenerAdapter(receiver, "receiveOrder");
}
}
我的消费者类按预期工作,它一次只处理一个请求。我怎么知道?
我的设置中有什么错误?我如何使它工作?
谢谢。
答案 0 :(得分:1)
SimpleMessageListenerContainer
具有设置并发使用者的方法。它具有setConcurrentConsumers
method,您可以在其中设置使用者数量。
@Bean
SimpleMessageListenerContainer container(ConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter) {
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
container.setQueueNames(newOrderQueueName);
container.setMessageListener(listenerAdapter);
container. setConcurrentConsumers(10);
return container;
}
使用这种配置,启动应用程序时,您将能够在RabbitMQ管理员中看到多个使用者
答案 1 :(得分:1)
您没有使用Boot来创建容器,因此无法应用boot属性。
尝试
@Bean
SimpleMessageListenerContainer container(ConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter,
RabbitProperties properties) {
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
container.setQueueNames(newOrderQueueName);
container.setMessageListener(listenerAdapter);
container.setConcurrentConsumers(properties.getListener().getSimple().getConcurrency();
return container;
}