如何使用 Java 连接到 MQ
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25669990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to connect to MQ using Java
提问by Amrinder Singh
Hey I want to connect & send a string to MQ using JAVA I am new to this so can u please help me with the code for this. I just want to make connection & send a string thats it.
嘿,我想使用 JAVA 连接并发送一个字符串到 MQ 我是新手,所以你能帮我解决这个问题吗?我只想建立连接并发送一个字符串。
I have the following information with me regarding MQ. JNDI Name:ABCDEFH Queue Manager:ABCDEFH Host Name or IP Address:ABCDEFH Port:ABCDEFH Channel:ABCDEFH Transport Type:ABCDEFH
我有以下关于 MQ 的信息。 JNDI 名称:ABCDEFH 队列管理器:ABCDEFH 主机名或 IP 地址:ABCDEFH 端口:ABCDEFH 通道:ABCDEFH 传输类型:ABCDEFH
回答by Morag Hughson
There are two different APIs you can use to send an MQ message using the Java language. You can use the MQ Classes for Javaand you can use the JMS API.
您可以使用两种不同的 API 来使用 Java 语言发送 MQ 消息。您可以使用Java的MQ 类,也可以使用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.
既然你提到了 JNDI,我怀疑你的意思是 JMS API。但是,我会为两者都回答。您听起来像是想要一些示例代码。IBM MQ 产品为您提供了示例代码供您查看。
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.
对于 Java 的 MQ 类,我建议您查看<wmq-installation-directory>\Tools\wmqjava\samples\MQSample.java
- 那是 Java 类的“Hello World”应用程序。
For the JMS interface, I suggest you look at <wmq-installation-directory>\Tools\jms\samples\JmsProducer.java
对于JMS接口,建议你看看 <wmq-installation-directory>\Tools\jms\samples\JmsProducer.java
回答by FAIZAN AHMED KHAN
You can use the following code with some changes:
您可以使用以下代码进行一些更改:
1.Change the host,port, channel,qName and qManager Name accordingly.
1. 相应地更改主机、端口、通道、qName 和 qManager Name。
2.For OpenOption use MQC.MQOO_OUTPUT.
2.对于 OpenOption,使用 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.
注意:请忽略错别字和格式,因为我是用手机输入的。