I am new in Rabbitmq and I want to use single queue with multiple consumers, please help me for multiple consumers and how can we handle request for multiple consumers from single queue ?
Below is my code for Single Queue and Single Consumers
My Configuration Class
@Configuration
public class ConfigureRabbitMq {
public static final String EXCHANGE_NAME = "mikeexchange2";
public static final String QUEUE_NAME = "mikequeue2";
@Bean
Queue createQueue() {
return new Queue(QUEUE_NAME, true, false, false);
}
@Bean
TopicExchange exchange(){
return new TopicExchange(EXCHANGE_NAME);
}
@Bean
Binding binding(Queue q, TopicExchange exchange){
return BindingBuilder.bind(q).to(exchange).with("mike.#");
}
@Bean
SimpleMessageListenerContainer container(ConnectionFactory connectionFactory
, MessageListenerAdapter messageListenerAdapter){
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
container.setQueueNames(QUEUE_NAME);
container.setMessageListener(messageListenerAdapter);
return container;
}
@Bean
MessageListenerAdapter listenerAdapter(Receive handler){
return new MessageListenerAdapter(handler, "handleMessage");
}
}
My Receiver Class
@Service
public class Receive {
public void handleMessage(String messageBody){
System.out.println("HandleMessage!!!");
System.out.println(messageBody);
}
}
My Sender Class
@RestController
public class Send {
private final RabbitTemplate rabbitTemplate;
public Send(RabbitTemplate rabbitTemplate) {
this.rabbitTemplate = rabbitTemplate;
}
@RequestMapping(method = RequestMethod.GET, value = "/api/send/{msg}")
public String sendMessage(@PathVariable("msg") String themessage){
for(int i=0;i<5000000;i++) {
rabbitTemplate.convertAndSend(ConfigureRabbitMq.EXCHANGE_NAME,
"mike.springmessages", themessage+""+Integer.toString(i));
}
return "We have sent a message! :" + themessage;
}
}