Java 对于 Websphere MQ 教程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21699187/
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
For Websphere MQ tutorial
提问by Bhaumik Shah
I am beginner in web sphere mq programming.
我是网络领域 mq 编程的初学者。
Any one has sample tutorial for connecting mq queue to java simple application.
任何人都有将 mq 队列连接到 java 简单应用程序的示例教程。
Basically i want to read a Mq queue through my java code.
基本上我想通过我的 java 代码读取一个 Mq 队列。
Please help me.
请帮我。
Thanks in advance
提前致谢
采纳答案by Roger
Your RC=2538 is because you have a coding error and I already pointed it out in your other question at MQJE001: Completion Code '2', Reason '2538'
您的 RC=2538 是因为您有一个编码错误,我已经在MQJE001的另一个问题中指出了这一点:完成代码 '2',原因 '2538'
Secondly, there are 34 Java/MQ (non-JMS) sample MQ programs at http://www.capitalware.biz/mq_code_java.html
其次,http://www.capitalware.biz/mq_code_java.html有34个Java/MQ(非JMS)示例MQ程序
Have you reviewed any of them and/or tried them out?
您是否查看过其中任何一个和/或尝试过它们?
回答by Aliti
I find this tutorial and code in jboss site. It works as well. Try thisand enjoy.
我在 jboss 站点中找到了本教程和代码。它也有效。试试这个并享受。
public class WebSphereMQMessageSendServiceBean implements MessageSendService {
private static Logger logger = Logger.getLogger(WebSphereMQMessageSendServiceBean.class);
private Connection connection = null;
private Session session = null;
private MessageProducer producer = null;
public boolean sendMessage(String xmlMsg, String instrumentId) throws QueueServiceAdaptorException{
boolean success = false;
String destinationType = "QUEUE";
String destinationName = "PRO.QDX.QDX.MSG.QDC";
String channel = "PROQDIB.SVRCONN";
String hostName = "localhost";
Integer port = "1414";
String queueManager = "REFDMQ01";
String uid = "nuwan";
String provider = "com.ibm.msg.client.wmq";
try {
JmsFactoryFactory jmsFactoryFactory = JmsFactoryFactory.getInstance(WMQConstants.WMQ_PROVIDER);
JmsConnectionFactory jmsConnectionFactory = jmsFactoryFactory.createConnectionFactory();
// Set the properties
jmsConnectionFactory.setStringProperty(WMQConstants.WMQ_HOST_NAME, hostName);
jmsConnectionFactory.setIntProperty(WMQConstants.WMQ_PORT, port);
jmsConnectionFactory.setStringProperty(WMQConstants.WMQ_CHANNEL, channel);
jmsConnectionFactory.setIntProperty(WMQConstants.WMQ_CONNECTION_MODE, WMQConstants.WMQ_CM_CLIENT);
jmsConnectionFactory.setStringProperty(WMQConstants.WMQ_QUEUE_MANAGER, queueManager);
// Create JMS objects
connection = jmsConnectionFactory.createConnection();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destination = null;
if (destinationType.equals("QUEUE")) {
destination = session.createQueue(destinationName);
}
else {
destination = session.createTopic(destinationName);
}
producer = session.createProducer(destination);
//connection.setExceptionListener(this);
connection.start();
TextMessage message = session.createTextMessage();
// If WMQ_MESSAGE_BODY is set to WMQ_MESSAGE_BODY_MQ, no additional header is added to the message body.
((MQDestination) destination).setMessageBodyStyle(WMQConstants.WMQ_MESSAGE_BODY_MQ);
((MQDestination)destination).setMQMDWriteEnabled(true);
message.setText(xmlMsg);
message.setJMSCorrelationID(instrumentId);
producer.send(message);
success = true;
logger.info("WebSphereMQMessageSender.sendMessage: Sent message:\n" + message);
}
catch (JMSException e) {
logger.error("WebSphereMQMessageSender.sendMessage: JMSException while sending message to QDIB", e);
success = false;
recordFailure(e);
throw new QueueServiceAdaptorException("WebSphereMQMessageSender.sendMessage: "
+ "JMSException while sending message to QDIB", e);
}
catch (Exception e) {
logger.error("WebSphereMQMessageSender.sendMessage: Exception while sending message to QDIB", e);
success = false;
throw new QueueServiceAdaptorException("WebSphereMQMessageSender.sendMessage: "
+ "Exception while sending message to QDIB", e);
}
finally {
cleanUp();
}
return success;
}
/**
* Record this run as failure.
*
* @param ex exception
*/
private void recordFailure(Exception ex) {
if (ex != null) {
if (ex instanceof JMSException) {
processJMSException((JMSException) ex);
} else {
logger.error("WebSphereMQMessageSender.recordFailure: " + ex);
}
}
logger.error("WebSphereMQMessageSender.recordFailure: FAILURE");
}
/**
* Process a JMSException and any associated inner exceptions.
*
* @param jmsex jmsex
*/
private void processJMSException(JMSException jmsex) {
logger.error(jmsex);
Throwable innerException = jmsex.getLinkedException();
if (innerException != null) {
logger.error("WebSphereMQMessageSender.processJMSException: Inner exception(s):");
}
while (innerException != null) {
logger.error(innerException);
innerException = innerException.getCause();
}
}
/**
* Release resources
* */
private void cleanUp() {
if (producer != null) {
try {
producer.close();
} catch (JMSException jmsex) {
logger.error("WebSphereMQMessageSender. cleanUp: Producer could not be closed.");
recordFailure(jmsex);
}
}
if (session != null) {
try {
session.close();
} catch (JMSException jmsex) {
logger.error("WebSphereMQMessageSender. cleanUp: Session could not be closed.");
recordFailure(jmsex);
}
}
if (connection != null) {
try {
connection.close();
} catch (JMSException jmsex) {
logger.error("WebSphereMQMessageSender. cleanUp: Connection could not be closed.");
recordFailure(jmsex);
}
}
}
}