Step 1: You need to have the Webspeher MQ client jar “com.ibm.mq.allclient.jar” in your project at “${basedir}/lib” and the JMS or JEE jar from a maven repository.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> //..... <properties> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> <mq-client.version>8.0.0.0</mq-client.version> </properties> <repositories> <repository> <id>localrepository</id> <url>file://${basedir}/lib</url> </repository> </repositories> <dependencies> <!-- Websphere MQ Implementation --> <dependency> <groupId>com.ibm.mq</groupId> <artifactId>allclient</artifactId> <version>${mq-client.version}</version> </dependency> <!-- JMS API --> <dependency> <groupId>javax.jms</groupId> <artifactId>javax.jms-api</artifactId> <version>2.0.1</version> </dependency> //..... </dependencies> </project> |
Step 2: Use the “JMS API” to send a message to a queue using a file based JNDI.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | import java.util.Hashtable; import javax.jms.JMSException; import javax.jms.Queue; import javax.jms.QueueConnection; import javax.jms.QueueSender; import javax.jms.QueueSession; import javax.jms.Session; import javax.jms.TextMessage; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import com.ibm.mq.constants.CMQC; import com.ibm.msg.client.jms.JmsConstants; public class JmsMqMessageSender { public static void sendRequestMsg(String messageId, String payload) throws JMSException { Hashtable<String, String> env = new Hashtable<String, String>(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory"); env.put(Context.PROVIDER_URL, "file:///home/myapp/mq_admin_objects"); //mq_admin_objects is a folder containing the .bindings file //.bindings file contains host name, channel, queue names, etc defined Context ctx = null; javax.jms.QueueConnectionFactory cf = null; QueueConnection reqConnection = null; try { ctx = new InitialContext(env); cf = (javax.jms.QueueConnectionFactory) ctx.lookup("JMS_QCF_MYAPP"); reqConnection = cf.createQueueConnection(); QueueSession qSession = reqConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); Queue reqQueue = (Queue)ctx.lookup("MYAPP_REQUEST.JREQUEST"); Queue respQueue = (Queue)ctx.lookup("MYAPP_RESPONSE.JREQUEST"); QueueSender qSender = qSession.createSender(reqQueue); TextMessage reqMsg = qSession.createTextMessage(); reqMsg.setText(payload); reqMsg.setJMSMessageID(messageId); reqMsg.setJMSCorrelationID(messageId); reqMsg.setJMSExpiration(28800000); reqMsg.setIntProperty(JmsConstants.JMS_IBM_CHARACTER_SET, 1208); reqMsg.setStringProperty(JmsConstants.JMS_IBM_FORMAT, CMQC.MQFMT_STRING); reqMsg.setJMSReplyTo(respQueue); qSender.send(reqMsg); } catch (NamingException e) { e.printStackTrace(); } finally { reqConnection.close(); } } public static void main(String[] args) throws JMSException { String messageId = "AAB1234"; String messagePaylod = "<message>testing of message sending using Webspeher MQ</message>"; sendRequestMsg(messageId, messagePaylod); } } |
Step 3:You need to have the “Websphere MQ” set up and the “.bindings file generated using a Webspehere MQ admin tool and copied to “/home/myapp/mq_admin_objects”. So, it will be “/home/myapp/mq_admin_objects/.bindings“. The “.bindings” file will contain the host, port number, connection factory, and destination (e.g. queue) details.