Basic Receive Message to MQ with Java and IBM MQ JMS

package my.mq.samples;

import javax.jms.JMSException;
import javax.jms.Session;
import javax.jms.TextMessage;
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.MQQueueSession;
import com.ibm.msg.client.wmq.WMQConstants;

public class MQReceive {
 
public static void main(String[] args)
{
try {
MQQueueConnectionFactory cf = new MQQueueConnectionFactory();
cf.setHostName("localhost");
cf.setPort(1414);
   
cf.setIntProperty(WMQConstants.WMQ_CONNECTION_MODE, WMQConstants.WMQ_CM_CLIENT);

cf.setQueueManager("QM_GRIDSERVER");
cf.setChannel("SYSTEM.ADMIN.SVRCONN");
   
MQQueueConnection connection = (MQQueueConnection) cf.createQueueConnection();
//MQQueueConnection connection = (MQQueueConnection) cf.createQueueConnection("username","password");
       
MQQueueSession session = (MQQueueSession) connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);

MQQueue queue = (MQQueue) session.createQueue("queue:///MyTestQueue");

MQQueueReceiver receiver = (MQQueueReceiver) session.createReceiver(queue);

connection.start();

TextMessage receivedMessage = (TextMessage) receiver.receive();
System.out.println("Received message from Queue MyTestQueue: " + receivedMessage.getText());

receiver.close();
session.close();
connection.close();
System.out.println("Message Received OK.\n");
}
catch (JMSException jmsex) {
System.out.println(jmsex);
System.out.println("Message Receive Failure\n");
}
catch (Exception ex) {
System.out.println(ex);
System.out.println("Message Receive Failure\n");
}
}
}

Comments

  1. Thanks for posting this code. I tried in order to learn mq and I always get message like " com.ibm.msg.client.jms.DetailedJMSSecurityException: JMSWMQ2013: The security authentication was not valid that was supplied for QueueManager ". It seems there is an authentication for connecting to the channel. I've never set any username and password. So why do u put blank in parameter supposed to a user and a password? Please, I need you help.

    ReplyDelete
  2. Amazing & Great informative blog,it gives very useful practical information to developer like me. Besides that Wisen has established as Best Java Online Training in India . or learn thru Online Training mode Hibernate Online Training . Nowadays Hibernate ORM has tons of job opportunities on various vertical industry. or learn thru JavaScript Online Training in India. Nowadays even JavaScript has tons of job opportunities on various vertical industry.

    ReplyDelete

Post a Comment

Popular posts from this blog

Basic Send Message to MQ with Java and IBM MQ JMS

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