RabbitConfiguration.java
@Bean public SimpleMessageListenerContainer messageListenerContainer() { // configure listener to receive messages from queues SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(); container.setConnectionFactory(connectionFactory()); container.setConcurrentConsumers(8); container.setMaxConcurrentConsumers(32); // listen to queues container.setQueueNames("MonitoringV5_Queue1", "MonitoringV5_Queue2"); container.setMessageListener(new RabbitMessageListener()); return container; }
public class RabbitMessageListener implements MessageListener { @Override public void onMessage(Message message) { switch (message.getMessageProperties().getConsumerQueue()) { case "MonitoringV5_Queue1" -> Queue1(message); case "MonitoringV5_Queue2" -> Queue2(message); } } private void Queue1(Message message) { String str=new String(message.getBody()); System.out.println(str); Gson gson = new Gson(); ReadValueDto value= gson.fromJson(str,ReadValueDto.class); } private void Queue2(Message message) { String str=new String(message.getBody()); System.out.println(str); Gson gson = new Gson(); ReadValueDto value= gson.fromJson(str,ReadValueDto.class); } }
If your callback logic depends on the AMQP Channel
instance for any reason, you may instead use the ChannelAwareMessageListener
. It looks similar but has an extra parameter.
References
https://docs.spring.io/spring-amqp/docs/current/reference/html/#message-listener