FreeKB - PUT a JMS message in a queue in Java Eclipse
PUT a JMS message in a queue in Java Eclipse

Home > Search


Create a Dynamic Web Project in Eclipse. Let's say the name of the project is jmsDemo. Download these JAR files and add them to your projects Java Build Path.

http://central.maven.org/maven2/com/ibm/mq/com.ibm.mq.allclient/9.0.4.0/com.ibm.mq.allclient-9.0.4.0.jar

http://central.maven.org/maven2/javax/jms/javax.jms-api/2.0.1/javax.jms-api-2.0.1.jar

 

At jmsDemo > Java Resources > src, create a new package. For the sake of this tutorial, let's name the package com.jms.demo.

 

In the package, create a class or a servlet called jmsPUT.

 

If you created a class, add the following markup to the class. 

package com.jms.demo;

import javax.jms.Destination;
import javax.jms.JMSContext;
import javax.jms.JMSException;
import javax.jms.JMSProducer;
import javax.jms.TextMessage;
import com.ibm.msg.client.jms.JmsConnectionFactory;
import com.ibm.msg.client.jms.JmsFactoryFactory;
import com.ibm.msg.client.wmq.WMQConstants;

public class jmsPUT {

	// Variables
	private static final String HOST         = "your_hostname";
	private static final int PORT            = your_port;
	private static final String QMGR         = "your_manager";
	private static final String QUEUE_NAME   = "your_queue";
	private static final String CHANNEL      = "your_channel";
	private static final String APP_USER     = "your_username";
	private static final String APP_PASSWORD = "your_password";
	
	public static void main(String[] args) {
		// Variables
		JMSContext context      = null;
		Destination destination = null;
		JMSProducer producer    = null;

		try {
			// Create the connection factory
			JmsFactoryFactory ff    = JmsFactoryFactory.getInstance(WMQConstants.WMQ_PROVIDER);
			JmsConnectionFactory cf = ff.createConnectionFactory();

			// Set the properties
			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.setStringProperty(WMQConstants.WMQ_APPLICATIONNAME, "jmsPUT (JMS)");
			cf.setBooleanProperty(WMQConstants.USER_AUTHENTICATION_MQCSP, true);
			cf.setStringProperty(WMQConstants.USERID, APP_USER);
			cf.setStringProperty(WMQConstants.PASSWORD, APP_PASSWORD);

			// PUT - Creates a message in the queue
			context             = cf.createContext();
			destination         = context.createQueue("queue:///" + QUEUE_NAME);
			TextMessage message = context.createTextMessage("Hello World");
			producer            = context.createProducer();
			producer.send(destination, message);
			System.out.println("Message:\n" + message);

		} catch (JMSException jmsex) {
			System.out.println("FAILURE");
            return;
		}
	}		
}

 

If you created a servlet, add the following markup to the servlet.

package com.jms.demo;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.jms.Destination;
import javax.jms.JMSContext;
import javax.jms.JMSException;
import javax.jms.JMSProducer;
import javax.jms.TextMessage;
import com.ibm.msg.client.jms.JmsConnectionFactory;
import com.ibm.msg.client.jms.JmsFactoryFactory;
import com.ibm.msg.client.wmq.WMQConstants;

@WebServlet("/jmsPUT")
public class jmsPUT extends HttpServlet {

	// Variables
	private static final long serialVersionUID = 1L;
	private static final String HOST           = "your_hostname";
	private static final int PORT              = your_port;
	private static final String QMGR           = "your_manager";
	private static final String QUEUE_NAME     = "your_queue";
	private static final String CHANNEL        = "your_channel";
	private static final String APP_USER       = "your_username";
	private static final String APP_PASSWORD   = "your_password";
	
	
    public jmsPUT() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.getWriter().append("Served at: ").append(request.getContextPath());
		JMSContext context      = null;
		Destination destination = null;
		JMSProducer producer    = null;
		
		try {			
			// Create the connection factory
			JmsFactoryFactory ff    = JmsFactoryFactory.getInstance(WMQConstants.WMQ_PROVIDER);
			JmsConnectionFactory cf = ff.createConnectionFactory();

			// Set the properties
			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.setStringProperty(WMQConstants.WMQ_APPLICATIONNAME, "jmsPUT (JMS)");
			cf.setBooleanProperty(WMQConstants.USER_AUTHENTICATION_MQCSP, true);
			cf.setStringProperty(WMQConstants.USERID, APP_USER);
			cf.setStringProperty(WMQConstants.PASSWORD, APP_PASSWORD);

			// PUT - Creates a message in the queue
			context             = cf.createContext();
			destination         = context.createQueue("queue:///" + QUEUE_NAME);
			TextMessage message = context.createTextMessage("Hello World");
			producer            = context.createProducer();
			producer.send(destination, message);
			System.out.println("Sent message:\n" + message);		

		} catch (JMSException jmsex) {
			System.out.println("FAILURE");
			return;
		}				
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}

 

In the markup, replace your_hostname, your_port, your_manager, your_queue, your_channel, your_username and your_password with valid values for the queue you are going to PUT the message into.

Right-click on jmsPUT.java and select Run as > Java application (class) or Run on server (servlet). This should PUT the message Hello World into the queue. To verify the message was placed in the queue, for whatever server is running the queue, you would need to use that server to verify the message is in the queue. For example, if using an IBM MQ server, you could use the amqsbcg command to verify the message is in the queue.



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




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




Comments