Basic Send 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.MQQueueSender;
import com.ibm.mq.jms.MQQueueSession;
import com.ibm.msg.client.wmq.WMQConstants;

public class MQSend {
   
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");

MQQueueSender sender =  (MQQueueSender) session.createSender(queue);


long uniqueNumber = System.currentTimeMillis() % 1000;

TextMessage message = (TextMessage) session.createTextMessage("Basic Queue Test "+ uniqueNumber);    

// Start the connection
connection.start();
sender.send(message);

System.out.println("Sent message to Queue MyTestQueue: " + message.getText());
sender.close();
session.close();
connection.close();
System.out.println("Message Sent OK.\n");
}
catch (JMSException jmsex) {
System.out.println(jmsex);
System.out.println("Message Send Failure\n");
}
catch (Exception ex) {
System.out.println(ex);
System.out.println("Message Send Failure\n");
}
}
}

Comments

  1. Thanks for the awesome code.
    Will this code run in java 1.6 ?

    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
  3. Hi there I am trying to send messages to MQ queue using Nifi. Nifi uses a processor called PublishJMS to publish messages in the queue. I have been able to do that. But only issue is the JMS is appending message headers and file properties with my message. I only need to send the message on the queue.

    ReplyDelete

Post a Comment

Popular posts from this blog

Basic Receive Message to MQ with Java and IBM MQ JMS

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