title: rabbitmq configuration to receive messages filtered on a pattern using single queue and many listeners
body: <p>I have this requirement to push messages to a single queue but receive them at more than one place (a java class).</p>
  
  <p>For that I used a topic-exchange and bound it to a queue that receives messages based on different patterns. Now when I try to receive them at the other end the listener-container send the messages to both the listeners rather than bifurcating them based on the pattern decided.</p>
  
  <p>I have attached the configuration and the code below for a quick look</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" ?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rabbit="http://www.springframework.org/schema/rabbit"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit.xsd"&gt;
  
      &lt;rabbit:connection-factory id="connectionFactory" host="localhost" username="guest" password="guest" /&gt;
  
      &lt;rabbit:admin connection-factory="connectionFactory" /&gt;
  
      &lt;rabbit:template id="pgTemplate" connection-factory="connectionFactory" exchange="PG-EXCHANGE"/&gt;
  
      &lt;rabbit:queue id="pgQueue" /&gt;
  
      &lt;rabbit:topic-exchange id="pgExchange" name="PG-EXCHANGE"&gt;
          &lt;rabbit:bindings&gt;
              &lt;rabbit:binding queue="pgQueue" pattern="pg"&gt;&lt;/rabbit:binding&gt;
              &lt;rabbit:binding queue="pgQueue" pattern="txn"&gt;&lt;/rabbit:binding&gt;
          &lt;/rabbit:bindings&gt;
      &lt;/rabbit:topic-exchange&gt;
  
      &lt;bean id="pgReceiver" class="com.pg.mq.service.impl.MessageReceiver"/&gt;
      &lt;bean id="txnReceiver" class="com.pg.mq.service.impl.MessageReceiver"/&gt;
  
      &lt;rabbit:listener-container id="ListenerContainer" connection-factory="connectionFactory"&gt;
          &lt;rabbit:listener ref="pgReceiver" queues="pgQueue" method="handleMessage"/&gt;
          &lt;rabbit:listener ref="txnReceiver" queues="pgQueue" method="handleMessage"/&gt;
      &lt;/rabbit:listener-container&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p><strong>Message Sender</strong></p>
  
  <pre><code>public String pushMessagePG(Object object) {
          if(object != null &amp;&amp; object instanceof Rabbit)
              pgTemplate.convertAndSend("pg", object); // send
          return null;
      } 
  </code></pre>
  
  <p><strong>Message Receivers</strong></p>
  
  <p>PG RECEIVER</p>
  
  <pre><code>public void handleMessage(Rabbit message) {
          System.out.println("inside Message Receiver");
          System.out.println("Listener received message-----&gt;" + message);
      }
  </code></pre>
  
  <p>TXN RECEIVER</p>
  
  <pre><code>public void handleMessage(Rabbit message) {
          System.out.println("inside TXN Message Receiver");
          System.out.println("Listener received message-----&gt;" + message);
      }
  </code></pre>
  
  <p><strong>Calling Code</strong></p>
  
  <pre><code>Sender sender = (Sender) context.getBean("messageSender");
          for(int i = 0; i &lt; 30; i++) sender.pushMessagePG(new Rabbit(5));
          Thread.sleep(2000);
          for(int i = 0; i &lt; 30; i++) sender.pushMessageTXN(new Rabbit(2));
  </code></pre>