我是web sphere mq编程的初学者。
任何人都有将mq队列连接到java简单应用程序的示例教程。
基本上我想通过我的java代码读取Mq队列。
请帮帮我。
提前致谢
答案 0 :(得分:3)
您的RC = 2538是因为您遇到了编码错误,我已经在MQJE001: Completion Code '2', Reason '2538'
的另一个问题中指出了其次,http://www.capitalware.biz/mq_code_java.html有34个Java / MQ(非JMS)样本MQ程序
你有没有审查过它们和/或试过它们?
答案 1 :(得分:3)
我在jboss网站上找到了这个教程和代码。它也有效。试试this并享受。
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);
}
}
}
}