Friday, 12 August 2016

Subscribe to an IBM MQ Topic using Spring


This code works to subscribe to an IBM MQ Topic called TESTTOPIC set up with MQExplorer 7.5.0.1 using the defaults. Code uses uses Spring SimpleMessageListenerContainer

Keywords: JMS DefaultMessageListenerContainer SimpleMessageListenerContainer MQTopicConnectionFactory MQQueueConnectionFactory

 package poc;  
 import javax.jms.JMSException;  
 import javax.jms.Message;  
 import javax.jms.MessageListener;  
 import org.junit.Test;  
 import org.springframework.jms.listener.AbstractMessageListenerContainer;  
 import org.springframework.jms.listener.SimpleMessageListenerContainer;  
 import com.ibm.mq.jms.MQTopicConnectionFactory;  
 public class PubSub2Test {  
   @Test  
   public void test() throws InterruptedException, JMSException {  
     MQTopicConnectionFactory connectionFactory = new MQTopicConnectionFactory(); // MQQueueConnectionFactory does not work here  
     connectionFactory.setHostName("localhost");  
     connectionFactory.setPort(1414);  
     connectionFactory.setQueueManager("AAA.QMGR");  
     connectionFactory.setChannel("AAA.SVRCONN");  
     connectionFactory.setTransportType(1);  
     AbstractMessageListenerContainer listenerContainer = new SimpleMessageListenerContainer(); // a DefaultMessageListenerContainer does not work here  
     listenerContainer.setPubSubDomain(true);  
     listenerContainer.setConnectionFactory(connectionFactory);  
     listenerContainer.setDestinationName("TESTTOPIC");  
     MessageListener listener = new MessageListener() {  
       @Override  
       public void onMessage(Message arg0) {  
         System.out.println("\nMessage received: " + arg0.toString() + "\n");  
       }  
     };  
     listenerContainer.setMessageListener(listener);  
     listenerContainer.start();  
     while (true) {  
       Thread.sleep(3000);  
       System.out.print(".");  
     }  
   }  
 }  

No comments: