Java Send / Read from Remote MQ Queue on CentOS Linux

package my.mq.samples;

import javax.jms.JMSException;
import javax.jms.Session;

import com.ibm.jms.JMSMessage;
import com.ibm.jms.JMSTextMessage;

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.jms.*;
import com.ibm.msg.client.services.*;
import com.ibm.msg.client.wmq.WMQConstants;

public class MQConnect {

   
public static void main(String[] args) {

    try {
      MQQueueConnectionFactory cf = new MQQueueConnectionFactory();
      cf.setHostName("82.195.136.246");
      cf.setPort(60000);
     
      cf.setIntProperty(WMQConstants.WMQ_CONNECTION_MODE, WMQConstants.WMQ_CM_CLIENT);
     
      cf.setQueueManager("dtm.queue.manager");
      cf.setChannel("SYSTEM.DEF.SVRCONN");
     
      MQQueueConnection connection = (MQQueueConnection) cf.createQueueConnection("Trevor","Password");
     
      MQQueueSession session = (MQQueueSession) connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
      MQQueue queue = (MQQueue) session.createQueue("queue:///Q1");
      MQQueueSender sender =  (MQQueueSender) session.createSender(queue);
      MQQueueReceiver receiver = (MQQueueReceiver) session.createReceiver(queue);    
     
      long uniqueNumber = System.currentTimeMillis() % 1000;
      JMSTextMessage message = (JMSTextMessage) session.createTextMessage("Basic Queue Test "+ uniqueNumber);    

      // Start the connection
      connection.start();
      sender.send(message);
      System.out.println("Sent message:\\n" + message);
      JMSMessage receivedMessage = (JMSMessage) receiver.receive(10000);
      System.out.println("\\nReceived message:\\n" + receivedMessage);
      sender.close();
      receiver.close();
      session.close();
      connection.close();
      System.out.println("\\nSUCCESS\\n");
    }
    catch (JMSException jmsex) {
      System.out.println(jmsex);
      System.out.println("\\nFAILURE\\n");
    }
    catch (Exception ex) {
      System.out.println(ex);
      System.out.println("\\nFAILURE\\n");
    }
  }
}

Comments

  1. I have read your blog its very attractive and impressive. I like it your blog.

    Java Training in Chennai Core Java Training in Chennai Core Java Training in Chennai

    Java Online Training Java Online Training Core Java 8 Training in Chennai Core java 8 online training JavaEE Training in Chennai Java EE Training in Chennai

    ReplyDelete
  2. Great Information sharing .. I am very happy to read this article .. thanks for giving us go through info.Fantastic nice. I appreciate this post. UP Ration Card application

    ReplyDelete
  3. am not able to receive message from the above code. am always getting null response.
    Try to receive the message from the MQ. I can able to see the messages in the MQ Explorer. But i cant able to get the messages. It always return null. But i can able to drop message to the Queue.

    ReplyDelete

  4. Very useful information to everyone thanks for sharing, learn the latest updated Technology at Best Training institutions
    Salesforce Lightning is the latest updated technology
    Salesforce Online Training in Bangalore
    Salesforce Training online in India

    ReplyDelete

Post a Comment

Popular posts from this blog

Basic Send Message to MQ with Java and IBM MQ JMS

Basic Receive Message to MQ with Java and IBM MQ JMS

Creating a simple Alert / Success Message with ASP.NET/VB using Bootstrap