Reputation: 99
I want to make a connection & send a string to MQ
using JAVA.
Following are the MQ details.
I am new to this, so can you please help me with sample code for this. Thanks!
Upvotes: 2
Views: 47671
Reputation: 333
You can use the following code with some changes:
1.Change the host,port, channel,qName and qManager Name accordingly.
2.For OpenOption use MQC.MQOO_OUTPUT.
Hope this helps.
//method to connect and send message to Mq
public void mqSend(){
try{
//Create a Hashtable with required properties
Hashtable properties = new Hashtable<String, Object>();
properties.put("hostname", host);
properties.put("port", port);
properties.put("channel", channel);
//Create a instance of qManager
MQQueueManager qMgr = new MQQueueManager(qManagerName, properties);
//Connect to the Queue
MQQueue queue = qMgr.accessQueue(qname, openOptions);
//Creating the mqmessage
MQMessage mqMsg = new MQMessage();
mqMsg.writeString(//My Message);
MQPutMessageOptions pmo = new MQPutMessageOptions();
queue.put(mqMsg,pmo);
queue.close();
qMgr.disconnect();
}catch(MqException mqEx){
mqEx.printStackTrace();
}
}
Note:: please ignore the typos and formatting as I have typed this using a phone.
Upvotes: 8
Reputation: 7629
There are two different APIs you can use to send an MQ message using the Java language. You can use the MQ Classes for Java and you can use the JMS API.
Since you mention JNDI, I suspect you mean the JMS API. However, I will answer for both. You sound like you want some sample code. The IBM MQ product supplies you with sample code to look at.
For the MQ Classes for Java, I suggest you look at <wmq-installation-directory>\Tools\wmqjava\samples\MQSample.java
- that's the "Hello World" app for the Java classes.
For the JMS interface, I suggest you look at <wmq-installation-directory>\Tools\jms\samples\JmsProducer.java
Upvotes: 12