Created
February 20, 2015 20:51
-
-
Save sathish-io/469cdd9a54e2926ffc04 to your computer and use it in GitHub Desktop.
Simple JMS Client without JNDI lookup for IBM MQ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.jms.Connection; | |
import javax.jms.JMSException; | |
import javax.jms.Message; | |
import javax.jms.MessageConsumer; | |
import javax.jms.MessageProducer; | |
import javax.jms.Queue; | |
import javax.jms.Session; | |
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; | |
/** | |
* @author Sathish Kandasamy | |
* | |
*/ | |
public class SimpleJMSClient { | |
private static final String DEFAULT_IBM_MQ_CHANNEL_NAME = "SYSTEM.DEF.SVRCONN"; | |
private static final int RECEIVE_WAIT_PERIOD = 1; | |
// Queue manager details | |
private String host; | |
private String queueManagerName; | |
private int port; | |
private String channel; | |
private String queueName; | |
private Connection connection = null; | |
private Session session = null; | |
private Queue destination = null; | |
MessageProducer producer = null; | |
MessageConsumer consumer = null; | |
public SimpleJMSClient(String host, String queueManagerName, int port, String channel, String queueName) { | |
super(); | |
this.host = host; | |
this.queueManagerName = queueManagerName; | |
this.port = port; | |
this.channel = DEFAULT_IBM_MQ_CHANNEL_NAME; | |
this.queueName = queueName; | |
} | |
public void connect(String userName, String password) throws JMSException { | |
JmsFactoryFactory factoryFactory = JmsFactoryFactory.getInstance(WMQConstants.WMQ_PROVIDER); | |
JmsConnectionFactory connectionFactory = factoryFactory.createConnectionFactory(); | |
// Set the properties | |
connectionFactory.setStringProperty(WMQConstants.WMQ_HOST_NAME, host); | |
connectionFactory.setIntProperty(WMQConstants.WMQ_PORT, port); | |
connectionFactory.setStringProperty(WMQConstants.WMQ_CHANNEL, channel); | |
connectionFactory.setIntProperty(WMQConstants.WMQ_CONNECTION_MODE, WMQConstants.WMQ_CM_CLIENT); | |
connectionFactory.setStringProperty(WMQConstants.WMQ_QUEUE_MANAGER, queueManagerName); | |
// Create JMS objects | |
connection = connectionFactory.createConnection(userName, password); | |
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); | |
destination = session.createQueue(queueName); | |
//create producer and consumers | |
producer = session.createProducer(destination); | |
consumer = session.createConsumer(destination); | |
// start receiving incomming messages in the connection | |
connection.start(); | |
} | |
public void close() { | |
// stop receiving incomming messages in the connection | |
try { | |
connection.stop(); | |
} catch (JMSException e1) { | |
// TODO Auto-generated catch block | |
e1.printStackTrace(); | |
} | |
try { | |
if (session != null) { | |
session.close(); | |
} | |
} catch (JMSException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
try { | |
if (connection != null) { | |
connection.close(); | |
} | |
} catch (JMSException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
} | |
public String receiveTextMessage() throws JMSException { | |
String text = null; | |
Message message = consumer.receive(RECEIVE_WAIT_PERIOD); | |
if (message != null && message instanceof TextMessage) { | |
TextMessage tm = (TextMessage) message; | |
text = tm.getText(); | |
// System.out.println("Message: " + text); | |
} else { | |
// System.out.println("Unknown message: " + message); | |
} | |
return text; | |
} | |
public void sendTextMessage(String text) throws JMSException { | |
Message message = session.createTextMessage(text); | |
producer.send(message); | |
// message.getJMSMessageID(); | |
System.out.println("Sent message <" + text + ">"); | |
} | |
public static void main(String[] args) { | |
// Queue manager details | |
String host = "192.168.202.46"; | |
String queueManagerName = "MyTestQueueManager"; | |
int port = 1418; | |
String channel = "SYSTEM.DEF.SVRCONN"; | |
String queueName = "MYQUEUE"; | |
String userName = "mquser"; | |
String password = "mqpass"; | |
SimpleJMSClient client = new SimpleJMSClient(host, queueManagerName, port, channel, queueName); | |
try { | |
client.connect(userName, password); | |
for (int i = 0; i <= 5; i++) { | |
client.sendTextMessage("Hello World!" + i); | |
String message = client.receiveTextMessage(); | |
System.out.println("Received message: " + message); | |
} | |
client.close(); | |
} catch (JMSException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
} | |
} |
The app runs in IDE, but after I package it and run again, I get
Exception in thread "main" com.ibm.msg.client.jms.DetailedJMSException: JMSCC0091: The provider factory for connection type 'com.ibm.msg.client.wmq' could not be loaded.
at package.SimpleJMSClient.connect(SimpleJMSClient.java:45)
Caused by: com.ibm.msg.client.commonservices.CSIException: A Messaging Provider component was not found.
This is line 45
JmsFactoryFactory factoryFactory = JmsFactoryFactory.getInstance(WMQConstants.WMQ_PROVIDER);
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Following IBM client libraries needs to be in the class path to run the code.
com.ibm.mq.headers.jar
com.ibm.mq.jar
com.ibm.mq.jmqi.jar
com.ibm.mqjms.jar
connector.jar
dhbcore.jar
I taken it from IBM 7.x version.
Also needed javax.transaction.TransactionManager.class that I taken from IBM library jms.jar 6.x version.
Or you can use javax.transaction.jar downloaded from web.