Java 向setMessageListener注册侦听器服务
我有一个spring amqp应用程序,我想使用它,但我似乎不知道如何让它注册我的侦听器服务。这是我基于注释的配置文件Java 向setMessageListener注册侦听器服务,java,spring,spring-amqp,Java,Spring,Spring Amqp,我有一个spring amqp应用程序,我想使用它,但我似乎不知道如何让它注册我的侦听器服务。这是我基于注释的配置文件 package com.jerry.configuration; import org.springframework.amqp.core.AmqpAdmin; import org.springframework.amqp.core.Queue; import org.springframework.amqp.rabbit.connection.CachingConnect
package com.jerry.configuration;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class SpringAmqpConfiguration {
protected final String helloWorldQueueName = "hello.world.queue";
@Bean
public ConnectionFactory connectionFactory() {
CachingConnectionFactory connectionFactory = new CachingConnectionFactory("localhost");
connectionFactory.setUsername("guest");
connectionFactory.setPassword("guest");
return connectionFactory;
}
@Bean
public AmqpAdmin amqpAdmin() {
return new RabbitAdmin(connectionFactory());
}
@Bean
public RabbitTemplate rabbitTemplate() {
RabbitTemplate template = new RabbitTemplate(connectionFactory());
//The routing key is set to the name of the queue by the broker for the default exchange.
template.setRoutingKey(this.helloWorldQueueName);
//Where we will synchronously receive messages from
template.setQueue(this.helloWorldQueueName);
return template;
}
@Bean
// Every queue is bound to the default direct exchange
public Queue helloWorldQueue() {
return new Queue(this.helloWorldQueueName);
}
@Bean
public Binding binding() {
return declare(new Binding(helloWorldQueue(), defaultDirectExchange()));
}
//https://docs.spring.io/spring-amqp/docs/1.5.0.BUILD-SNAPSHOT/reference/html/_reference.html#async-annotation-driven
@Bean
public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory() {
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
factory.setConnectionFactory(connectionFactory());
factory.setConcurrentConsumers(3);
//If you want a fixed number of consumers, omit the max.
factory.setMaxConcurrentConsumers(10);
//Set message listener : container.setMessageListener(this.myFirstListener);
return factory;
}
/**
* Listener Bean
* Threadpool
*
<bean id="messageListener" class="com.spring.rabbitmq.service.MessageListenerService" />
<rabbit:listener-container connection-factory="connectionFactory">
<rabbit:listener ref="messageListener" queues="my.first.queue" />
</rabbit:listener-container>
*/
}
如何在这个bean中注册侦听器服务,以及如何注册多个侦听器来侦听不同的队列
@Bean
public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory() {
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
factory.setConnectionFactory(connectionFactory());
factory.setConcurrentConsumers(3);
//If you want a fixed number of consumers, omit the max.
factory.setMaxConcurrentConsumers(10);
//Set message listener : container.setMessageListener(this.myFirstListener);
return factory;
}
对于listener类,您应该执行以下操作:
@EnableRabbit
@Service
public class MessageListenerService {
private static final Logger LOGGER = org.slf4j.LoggerFactory.getLogger(MessageListenerService.class);
@RabbitListener(queues = { Constant.QUEUE_NAME })
public void onMessage(Message message) {
LOGGER.info(message.getMessageProperties().toString());;
LOGGER.info(new String(message.getBody()));
}
}
对于listener类,您应该执行以下操作:
@EnableRabbit
@Service
public class MessageListenerService {
private static final Logger LOGGER = org.slf4j.LoggerFactory.getLogger(MessageListenerService.class);
@RabbitListener(queues = { Constant.QUEUE_NAME })
public void onMessage(Message message) {
LOGGER.info(message.getMessageProperties().toString());;
LOGGER.info(new String(message.getBody()));
}
}
如何在这个bean中注册侦听器服务
为MessageListenerService
定义@Bean
,创建SimpleMessageListenerContainer
@Bean
并使用setMessageListener()
容器工厂不是用于用户定义的侦听器,而是用于@RabbitListener
pojo注释
我如何注册多个侦听不同队列的侦听器
@Bean
public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory() {
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
factory.setConnectionFactory(connectionFactory());
factory.setConcurrentConsumers(3);
//If you want a fixed number of consumers, omit the max.
factory.setMaxConcurrentConsumers(10);
//Set message listener : container.setMessageListener(this.myFirstListener);
return factory;
}
如果希望同一个侦听器侦听多个队列,请在容器中设置队列名称
如果每个队列需要不同的侦听器,则每个队列都需要一个SimpleMessageListenerContainer
或者,改用@RabbitListener
注释
如何在这个bean中注册侦听器服务
为MessageListenerService
定义@Bean
,创建SimpleMessageListenerContainer
@Bean
并使用setMessageListener()
容器工厂不是用于用户定义的侦听器,而是用于@RabbitListener
pojo注释
我如何注册多个侦听不同队列的侦听器
@Bean
public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory() {
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
factory.setConnectionFactory(connectionFactory());
factory.setConcurrentConsumers(3);
//If you want a fixed number of consumers, omit the max.
factory.setMaxConcurrentConsumers(10);
//Set message listener : container.setMessageListener(this.myFirstListener);
return factory;
}
如果希望同一个侦听器侦听多个队列,请在容器中设置队列名称
如果每个队列需要不同的侦听器,则每个队列都需要一个SimpleMessageListenerContainer
或者,改用
@RabbitListener
注释。上述服务的配置如何,我如何能有多个侦听器?是否使用springboot?否。Spring mvcok,在这种情况下,此链接将非常有用。您的配置与上面的服务类似吗?我如何可以有多个侦听器?您是否使用springboot?不。Spring mvcok,在这种情况下,此链接将非常有用