Step 1: You need to have the Webspeher MQ client jar “com.ibm.mq.allclient.jar” in your project at “${basedir}/lib”. You can refer to it via Maven as a local file based 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 |
<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> //..... </dependencies> </project> |
Step 2: Use the “Websphere MQ API” to receive a “MQMessage” from a “MQQueue”.
|
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 |
import java.io.IOException; import java.util.Hashtable; import com.google.common.base.Charsets; import com.ibm.mq.MQEnvironment; import com.ibm.mq.MQException; import com.ibm.mq.MQMessage; import com.ibm.mq.MQQueue; import com.ibm.mq.MQQueueManager; import com.ibm.mq.constants.CMQC; import com.ibm.mq.constants.MQConstants; public class MQMessageReceiver { private static MQQueue getInboundMessageQueue() throws MQException { MQEnvironment.hostname = "12.8.134.6"; MQEnvironment.channel = "MYAPP.SVRCONN"; MQEnvironment.port = 1414; String queueName = "MYAPP.REQUEST"; String queueManagerName = "MYAPP.QMGR"; return getQueue(queueName, queueManagerName); } private static MQQueue getQueue(String queueName, String queueManagerName) throws MQException { @SuppressWarnings("unchecked") Hashtable<String, String> properties = MQEnvironment.properties; properties.put(MQConstants.TRANSPORT_PROPERTY, MQConstants.TRANSPORT_MQSERIES_CLIENT); MQQueueManager queueManager = new MQQueueManager(queueManagerName); int openOptions = MQConstants.MQOO_INPUT_AS_Q_DEF | MQConstants.MQOO_OUTPUT; MQQueue queue = queueManager.accessQueue(queueName, openOptions); return queue; } public static void main(String[] args) throws MQException, IOException { MQQueue inboundMessageQueue = getInboundMessageQueue(); // read message from input queue MQMessage getMessage = new MQMessage(); getMessage.characterSet = 1208; // UTF-8 getMessage.format = CMQC.MQFMT_STRING; inboundMessageQueue.get(getMessage); int messageLength = getMessage.getMessageLength(); String messageData = getMessage.readStringOfCharLength(messageLength); System.out.println("Recieved Reqeust - Message ID: " + new String(getMessage.messageId, Charsets.UTF_8)); System.out.println("Correlation ID: " + new String(getMessage.correlationId, Charsets.UTF_8)); System.out.println("Data: " + messageData); } } |
Step 3:You need to have the “Websphere MQ” set up as per the hostname, channel, Queue Manager and Queue to test.