samedi 18 avril 2015

spring rabbitMQ new listener on each message

I have a spring rabbit listener in the project. I want to have run it in a brand new thread on every message event. So, a message goes from the rabbitMQ queue to the system, and spring launches listener in another separate thread. Is it possible? I tried to make my listener bean as prototype, but no luck. What I need to do to achieve the result? Thank you



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);
}


}

Aucun commentaire:

Enregistrer un commentaire