[英]How to connect to MQ using Java
我想建立一个连接并使用 JAVA 向MQ
发送一个字符串。
以下是 MQ 详细信息。
我是新手,所以请您帮我提供示例代码。 谢谢!
您可以使用两种不同的 API 来使用 Java 语言发送 MQ 消息。 您可以使用Java的MQ 类,也可以使用JMS API 。
既然你提到了 JNDI,我怀疑你的意思是 JMS API。 但是,我会为两者都回答。 您听起来像是想要一些示例代码。 IBM MQ 产品为您提供了示例代码供您查看。
对于 Java 的 MQ 类,我建议您查看<wmq-installation-directory>\\Tools\\wmqjava\\samples\\MQSample.java
- 这是 Java 类的“Hello World”应用程序。
对于JMS接口,建议你查看<wmq-installation-directory>\\Tools\\jms\\samples\\JmsProducer.java
您可以使用以下代码进行一些更改:
1. 相应地更改主机、端口、通道、qName 和 qManager Name。
2.对于 OpenOption 使用 MQC.MQOO_OUTPUT。
希望这可以帮助。
//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();
}
}
注意:请忽略错别字和格式,因为我是用手机输入的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.