我在项目中有一个春天的兔子听众。我希望在每个消息事件的全新线程中运行它。因此,消息从rabbitMQ队列传递到系统,spring在另一个单独的线程中启动侦听器。 可能吗?我试图让我的监听器bean成为原型,但没有运气。我需要做些什么才能达到效果?谢谢
public class Rabbit {
@Bean
public ConnectionFactory connectionFactory(){
CachingConnectionFactory f=new CachingConnectionFactory();
f.setHost("127.0.0.1");
f.setPassword("123");
f.setPort(5672);
f.setUsername("root");
return f;
}
final static String QUEUE_NAME = "queueOrderSaver";
@Bean
public RabbitTemplate rabbitTemplate(){
RabbitTemplate t=new RabbitTemplate();
t.setConnectionFactory(connectionFactory());
t.setRoutingKey("queue.*");
t.setExchange("exchange");
return t;
}
@Bean
public RabbitAdmin rabbitAdmin(){
return new RabbitAdmin(connectionFactory());
}
@Bean
public TopicExchange exchange(){
return new TopicExchange("exchange");
}
@Bean
Queue queue() {
return new Queue(QUEUE_NAME);
}
@Bean
Binding binding(Queue q, TopicExchange e) {
return BindingBuilder.bind(q).to(e).with(QUEUE_NAME);
}
@Bean
SimpleMessageListenerContainer container(ConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter) {
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
//container.setConcurrentConsumers(20);
container.setQueueNames(QUEUE_NAME);
container.setMessageListener(listenerAdapter);
return container;
}
@Bean
MessageListenerAdapter listenerAdapter() {
return new MessageListenerAdapter(worker);
}
}