Java JMS MQ Beispiel

by Haf
23.06.2009
tags: , ,
 

Vor einiger Zeit wurde MQ installiert. Nun folgt endlich ein Beispiel wie man in Java MQ ansprechen kann. Dabei wird JMS (Java Message Servic) ohne JNDI (Java Naming and Directory Interface) verwendet. Ein JNDI-Beispiel folgt in einem späteren Blog.

Das Beispiel zeigt wie man mittels JMS eine MQ Nachricht verschicken und empfangen kann.
Die Architektur steht hier nicht im Vordergrund, das nur die MQ Kommunikation.

Für das Versenden von MQ Nachrichten ist ein MQQueueSender verantwortlich, entsprechendes gilt für das Empfangen (MQQueueReceiver).
Da bei MQ mehrere Clients Nachrichten verschicken können und jedem Client nur seine Antworten interessieren, gibt es die Möglichkeit dem Receiver einen Filter (Selector) anzugeben. Mit diesem Selector werden dann nur die MQ Nachrichten geliefert (empfangen), welche die Kriterien erfüllen.
Hier lohnt es sich (nur) Correlation und Message ID zu nutzen, da andere Eigenschaften zu langsam in der Filterung sind.
Bei der Correlation ID muss noch das Präfix ‘ID:’ angehangen werden, welches bei der Message ID schon automatisch vorhanden ist.

Nachfolgend ein kleines MQ JMS Beispiel

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;

import com.ibm.jms.JMSTextMessage;
import com.ibm.mq.jms.JMSC;
import com.ibm.mq.jms.MQQueue;
import com.ibm.mq.jms.MQQueueConnection;
import com.ibm.mq.jms.MQQueueConnectionFactory;
import com.ibm.mq.jms.MQQueueReceiver;
import com.ibm.mq.jms.MQQueueSender;
import com.ibm.mq.jms.MQQueueSession;
import com.ibm.msg.client.wmq.v6.base.internal.MQC;

public class TestMQJMSPlain {

     public static void main(String[] args) {

         MQQueueConnectionFactory cf = new MQQueueConnectionFactory();

         try {

           cf.setHostName("...");
           cf.setPort(1415);
           cf.setTransportType(JMSC.MQJMS_TP_CLIENT_MQ_TCPIP);
           cf.setQueueManager("...");
           cf.setChannel("SYSTEM.DEF.SVRCONN");
           String qIn = "...";
           String qOut = "...";

           MQQueueConnection connection = (MQQueueConnection) cf.createQueueConnection();
           // create a new Session, which is not transacted
           MQQueueSession session = (MQQueueSession) connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
           // create queues
           MQQueue queueRead = (MQQueue) session.createQueue(qIn);
           MQQueue queueWrite = (MQQueue) session.createQueue(qOut);
           // if the client on the other side is a non-jms client set this property
           queueWrite.setTargetClient(JMSC.MQJMS_CLIENT_NONJMS_MQ);

           // create a sender
           MQQueueSender sender =  (MQQueueSender) session.createSender(queueWrite);
           MQQueueReceiver receiver = null;      

           // create the message which will be sent over MQ
           JMSTextMessage message = (JMSTextMessage) session.createTextMessage(dat);
          // define the message type (not really necessary, depends from the client on the other side)
          message.setJMSType(MQC.MQFMT_STRING);
          // set a correlation ID
          message.setJMSCorrelationID("MyIdSomethingElse" + System.currentTimeMillis());
          // set the replay queue
          message.setJMSReplyTo(queueRead);

          // lets start the connection
          connection.start();

          // send the JMS-Message
          sender.send(message);

          // create the selector to retrieve only the right response
          // the combination of message and correlation ID depends the individually defined constraints
          // it is wise to use the correlation and message ID for the selector, cause this are
          // faster as some other properties
          // Hint: the format for correlation ID is
          // JMSCorrelationID = 'ID:AF01CE....hex string...'
          // the prefix 'ID' is mandatory. However the message ID contains the fragment 'ID:'
          String selector = "JMSCorrelationID = 'ID:" + dumpId2Hex(message.getJMSCorrelationIDAsBytes()) +
                                 "' AND JMSMessageID = '" + message.getJMSMessageID() + "'";

          // create a receiver with the defined selector
          receiver = (MQQueueReceiver) session.createReceiver(queueRead, selector);

          // receive the response (with match the filter in the selector). This call blocks max. 20000 ms
          Message receivedMessage = receiver.receive(20000);

          // check if message is received and a text message
          if (receivedMessage instanceof TextMessage) {
               // get the content...
              String replyString = ((TextMessage) receivedMessage).getText();
         }

         // close everything...
         sender.close();
         receiver.close();
         session.close();
         connection.close();		    

           }
	    catch (JMSException jmsex) {
	    	log.error("JMSException", jmsex);
	    }
	    catch (Exception ex) {
	    	log.error("An other error occurred.", ex);
	    }
     }
}
Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • MisterWong
  • Technorati
  • Yigg
No Comments

Leave A Comment

Note: You can use basic XHTML in your comments. Your email address will never be published.

Subscribe to this comment feed via RSS