Bootstrap FreeKB - IBM MQ - PUT a JMS message in an IBM MQ queue in Java
IBM MQ - PUT a JMS message in an IBM MQ queue in Java

Updated:   |  IBM MQ articles

There are different ways to connect to IBM MQ in a Java application.

If you have access to the IBM MQ system, the following commands can be used to get the values needed for the connection factory.

  • Use the hostname command to determine the hostname of the IBM MQ system
  • Use the dspmq command to determine the name of the queue manager
  • Use the display lsstatus command to determine the port
  • Use the display channel command to determine the name of the channel and the SSL cipher

In this example, the Java app is connecting to IBM MQ in Java using JMS

package com.jms.demo;

import java.util.Properties;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import com.ibm.msg.client.jms.JmsConnectionFactory;
import com.ibm.msg.client.jms.JmsFactoryFactory;
import com.ibm.msg.client.wmq.WMQConstants;

public class Producer {
  public static void main(String[] args) throws NamingException, JMSException {
    String HOST         = "your_hostname";
    int PORT            = your_port;
    String QMGR         = "your_manager";
    String QUEUE        = "your_queue";
    String CHANNEL      = "your_channel";
    String USERNAME     = "your_username";
    String PASSWORD     = "your_password";
	
    JmsFactoryFactory ff    = JmsFactoryFactory.getInstance(WMQConstants.WMQ_PROVIDER);
    JmsConnectionFactory cf = ff.createConnectionFactory();

    cf.setStringProperty (WMQConstants.WMQ_HOST_NAME,             HOST);
    cf.setIntProperty    (WMQConstants.WMQ_PORT,                  PORT);
    cf.setStringProperty (WMQConstants.WMQ_CHANNEL,               CHANNEL);
    cf.setIntProperty    (WMQConstants.WMQ_CONNECTION_MODE,       WMQConstants.WMQ_CM_CLIENT);
    cf.setStringProperty (WMQConstants.WMQ_QUEUE_MANAGER,         QMGR);
    cf.setBooleanProperty(WMQConstants.USER_AUTHENTICATION_MQCSP, true);
    cf.setStringProperty (WMQConstants.USERID,                    USERNAME);
    cf.setStringProperty (WMQConstants.PASSWORD,                  PASSWORD);

    Connection conn = cf.createConnection(USERNAME, PASSWORD);
    conn.start();
    Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Queue queue = session.createQueue("queue:///"+QUEUE);
    TextMessage message = session.createTextMessage("Hello World");
    MessageProducer producer = session.createProducer(queue);
    producer.send(message);
    conn.close();
    return "message should have been PUT on queeue";
  }		
}

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 11696c in the box below so that we can be sure you are a human.