title: RabbitMQ+SpringAMPQ: Receiving multiple queues in SimpleMessageListenerContainer
body: <p>I am currently learning a bit about RabbitMQ + SpringAMQ and I was trying to make my SimpleMessageListenerContainer to be able to read messages delivered to my two queues, but only one is receiving it. If you have any further tips about the code or if there is any way of doing it in a better way please let me know It would be nice.</p>
  
  <p>EDIT:
  The issue is
  <strong>The code is not sending a message to both queues only to one.</strong></p>
  
  <p>Basically I am receiving from two queues, one inserts something in the database the other one will receive a validation request. I wish later on to send a response to the routing key and exchange.</p>
  
  <p>Here is my Application.java:</p>
  
  <pre><code>import org.springframework.amqp.core.Binding;
  import org.springframework.amqp.core.BindingBuilder;
  import org.springframework.amqp.core.DirectExchange;
  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.listener.SimpleMessageListenerContainer;
  import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
  import org.springframework.beans.factory.annotation.Qualifier;
  import org.springframework.boot.SpringApplication;
  import org.springframework.boot.autoconfigure.SpringBootApplication;
  import org.springframework.context.annotation.Bean;
  
  @SpringBootApplication
  public class ChallengeApplication {
  
      public static void main(String[] args) {
          SpringApplication.run(ChallengeApplication.class, args);
      }
  
      static final String responseExchange = "response.exchange";
      static final String routingKey = "response.routing.key";
      static final String queueInsertion = "insertion.queue";
      static final String queueValidation = "validation.queue";
  
      @Bean
      public ConnectionFactory connectionFactory() {
          CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
          connectionFactory.setHost("127.0.0.1");
          connectionFactory.setVirtualHost("/");
          connectionFactory.setUsername("guest");
          connectionFactory.setPassword("guest");
          return connectionFactory;
      }
  
      @Bean
      Queue queueInsertion() {
          return new Queue(queueInsertion, true);
      }
  
      @Bean
      Queue queueValidation() {
          return new Queue(queueValidation, true);
      }
  
      @Bean
      DirectExchange exchange() {
          return new DirectExchange(responseExchange);
      }
  
      @Bean
      Binding bindingInsertion(@Qualifier("queueInsertion") Queue queue, DirectExchange exchange) {
          return BindingBuilder.bind(queue).to(exchange).with(queueInsertion);
      }
  
      @Bean
      Binding queueValidation(@Qualifier("queueValidation") Queue queue, DirectExchange exchange) {
          return BindingBuilder.bind(queue).to(exchange).with(queueValidation);
      }
  
      @Bean
      SimpleMessageListenerContainer containerValidation(ConnectionFactory connectionFactory,
              MessageListenerAdapter listenerAdapter) {
          SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
          container.setConnectionFactory(connectionFactory);
          container.setConcurrentConsumers(3);
          container.setQueueNames(queueValidation, queueInsertion);
          container.setMessageListener(listenerAdapter);
          return container;
      }
  
  
      @Bean
      MessageListenerAdapter listenerAdapter(Receiver receiver) {
          return new MessageListenerAdapter(receiver, "receiveMessage");
      }
  
  }
  </code></pre>
  
  <p>As you can see my SimpleMessageListenerContainer has in the setQueueNames both queues I want to listen to. And my MessageListenerAdapter sends it to my Receiver.java to the method receiveMessage, another question would be how can I read from which queue I am receiving the message? It's possible this way or should I use the @RabbitListener in order to point it to each place?</p>
  
  <p>Receiver.java</p>
  
  <pre><code>import java.util.List;
  import java.util.concurrent.CountDownLatch;
  import java.util.regex.Pattern;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.dao.DataAccessException;
  import org.springframework.stereotype.Component;
  
  import com.axur.challenge.DAO.WhitelistDAO;
  import com.axur.challenge.formatters.InputData;
  import com.axur.challenge.model.Whitelist;
  import com.google.gson.Gson;
  import com.google.gson.JsonParseException;
  
  @Component
  public class Receiver {
  
      @Autowired
      private WhitelistDAO whitelistDAO;
  
      private CountDownLatch latch = new CountDownLatch(1);
  
      public void receiveMessage(String message) {
          try {
              InputData inputData = new Gson().fromJson(message, InputData.class);
              String url = inputData.getUrl();
              if (url == null) {
                  receivedInsertion(inputData);
              } else {
                  receivedValidation(inputData);
              }
          } catch (JsonParseException e) {
              System.out.println("It was not possible to read the input");
          }
          latch.countDown();
      }
  
      public void receivedInsertion(InputData inputData) {        
          Whitelist whitelist = new Whitelist();
          String client = inputData.getClient();
  
          if (client == null) {
              whitelist.setClient("global");
          } else {
              whitelist.setClient(inputData.getClient());
          }
  
          whitelist.setRegex(inputData.getRegex());
  
          System.out.println("Whitelist: " + whitelist);
  
          try {
              Whitelist selectedWhitelist = whitelistDAO.getSpecificWhitelist(whitelist.getClient(), whitelist.getRegex());
              System.out.println("selectedWhitelist: " + selectedWhitelist);
              if (selectedWhitelist == null || !selectedWhitelist.equals(whitelist)) {
                  whitelistDAO.insertWhitelist(whitelist);
                  System.out.println("Whitelist Added");
              } else {
                  System.out.println("Already added before");
              }
  
          } catch (DataAccessException dae) {
              System.out.println("Whitelist NOT Added");
              System.err.println(dae);
          }
  
      }
  
      public void receivedValidation(InputData inputData) {
          //getting all regex from this client
          List&lt;Whitelist&gt; listWhitelist = whitelistDAO.getWhitelist(inputData.getClient(), inputData.getRegex());
          boolean match = false;
          //verify if the regex works for the url provided
          if (listWhitelist != null) {
              int index = 0;
              while (match == false &amp;&amp; index &lt; listWhitelist.size()) {
                  match = checkRegex(listWhitelist.get(index).getRegex(), inputData.getUrl());
                  index++;
              }
          }
  
          if (match == true) {
  
          } else {
  
          }
      }
  
      public boolean checkRegex(String regex, String url) {
          return Pattern.compile(regex).matcher(url).find();
      }
  
      public CountDownLatch getLatch() {
          return latch;
      }
  
  }
  </code></pre>
  
  <p>And this is the Runner.java where I send the message to each queue to test it:</p>
  
  <pre><code>import java.util.concurrent.TimeUnit;
  import org.springframework.amqp.rabbit.core.RabbitTemplate;
  import org.springframework.boot.CommandLineRunner;
  import org.springframework.stereotype.Component;
  
  @Component
  public class Runner implements CommandLineRunner {
  
      private final RabbitTemplate rabbitTemplate;
      private final Receiver receiver;
  
      public Runner(Receiver receiver, RabbitTemplate rabbitTemplate) {
          this.receiver = receiver;
          this.rabbitTemplate = rabbitTemplate;
      }
  
      @Override
      public void run(String... args) throws Exception {
              System.out.println("Sending message...");
              rabbitTemplate.convertAndSend(ChallengeApplication.responseExchange, ChallengeApplication.queueInsertion, "{'client':null, 'regex':'[a-z]'}");
              rabbitTemplate.convertAndSend(ChallengeApplication.responseExchange, ChallengeApplication.queueValidation, "{'client':null, 'regex':'[a-z]'}");
              //rabbitTemplate.convertAndSend(ChallengeApplication.responseExchange, ChallengeApplication.routingKey, "{'client':null, 'regex':'[a-z]'}");
              receiver.getLatch().await(3, TimeUnit.SECONDS);
      }
  }
  </code></pre>
  
  <p>Thank you for your time!</p>