Simplest sample applications using WebSphere MQ JMS

Here are some simple sample applications for WebSphere MQ JMS that you may find useful. You may use them to verify your installation or to learn more about WebSphere MQ JMS semantics.

Both applications use client bindings rather than local bindings; and do not make use of JNDI for simplicity. They can be run as standalone J2SE/JSE programs.

Note: Applications need minor tweak in the config section and destination names according to your setup.

Author: Saket Rungta, wastedmonkeys.com

Simple Point-to-point application using WebSphere MQ JMS

package my.samples;

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

import com.ibm.jms.JMSMessage;
import com.ibm.jms.JMSTextMessage;
import com.ibm.mq.jms.JMSC;
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;

/**
* SimplePTP: A minimal and simple testcase for Point-to-point messaging (1.02 style).
*
* Assumes that the queue is empty before being run.
*
* Does not make use of JNDI for ConnectionFactory and/or Destination definitions.
*
* @author saket
*/
public class SimplePTP {
/**
* Main method
*
* @param args
*/
public static void main(String[] args) {
try {
MQQueueConnectionFactory cf = new MQQueueConnectionFactory();

// Config
cf.setHostName(“localhost”);
cf.setPort(1414);
cf.setTransportType(JMSC.MQJMS_TP_CLIENT_MQ_TCPIP);
cf.setQueueManager(“QM_thinkpad”);
cf.setChannel(“SYSTEM.DEF.SVRCONN”);

MQQueueConnection connection = (MQQueueConnection) cf.createQueueConnection();
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(“SimplePTP “+ 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”);
}
}
}
Simple Publish/Subscribe application using WebSphere MQ JMS

package my.samples;

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

import com.ibm.jms.JMSMessage;
import com.ibm.jms.JMSTextMessage;
import com.ibm.mq.jms.JMSC;
import com.ibm.mq.jms.MQTopic;
import com.ibm.mq.jms.MQTopicConnection;
import com.ibm.mq.jms.MQTopicConnectionFactory;
import com.ibm.mq.jms.MQTopicPublisher;
import com.ibm.mq.jms.MQTopicSession;
import com.ibm.mq.jms.MQTopicSubscriber;

/**
* SimplePubSub: A minimal and simple testcase for Publish/Subscribe (1.02 style).
*
* Topics are dynamically created on the queue manager and need not be pre-defined.
*
* (The Broker must be enabled on the queue manager and the JMS Publish/Subscribe
* queues must be defined manually.)
*
* Note: These samples are for WMQ Base Broker and incomplete for WebSphere Message Broker)
*
* Does not make use of JNDI for ConnectionFactory and/or Destination definitions.
*
* @author saket
*/
public class SimplePubSub {
/**
* Main method.
*
* @param args
*/
public static void main(String[] args) {
try {
MQTopicConnectionFactory cf = new MQTopicConnectionFactory();

// Config
cf.setHostName(“localhost”);
cf.setPort(1414);
cf.setTransportType(JMSC.MQJMS_TP_CLIENT_MQ_TCPIP);
cf.setQueueManager(“QM_thinkpad”);
cf.setChannel(“SYSTEM.DEF.SVRCONN”);

MQTopicConnection connection = (MQTopicConnection) cf.createTopicConnection();
MQTopicSession session = (MQTopicSession) connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
MQTopic topic = (MQTopic) session.createTopic(“topic://foo”);
MQTopicPublisher publisher = (MQTopicPublisher) session.createPublisher(topic);
MQTopicSubscriber subscriber = (MQTopicSubscriber) session.createSubscriber(topic);

long uniqueNumber = System.currentTimeMillis() % 1000;
JMSTextMessage message = (JMSTextMessage) session.createTextMessage(“SimplePubSub “+ uniqueNumber);

// Start the connection
connection.start();

publisher.publish(message);
System.out.println(“Sent message:\\n” + message);

JMSMessage receivedMessage = (JMSMessage) subscriber.receive(10000);
System.out.println(“\\nReceived message:\\n” + receivedMessage);

publisher.close();
subscriber.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”);
}
}
}

source/fuente: http://hursleyonwmq.wordpress.com


  1. Me uno a las felicitaciones, la verdad que me parece que el contenido es bastante …. Es muy interesante tu blog.
    Puedes visitar estos sitios, parecen interesantes.

    Ich einer in den Glückwünschen, der Wahrheit, die mir scheint, dass der Inhalt genug ist…. Es ist dein Blog sehr interessant.
    Du kannst diese Plätze besuchen, sie wirken interessant.

    Me one to the congratulations, the truth that seems to me that the content is enough…. Your blog Is very interesting.
    You can visit these places, they seem interesting.

    mugen-kan Karate Goju-ryu

    videoinformatica.net ip3122, cámaras de seguridad




Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s



A %d blogueros les gusta esto: