设为首页 收藏本站
查看: 1911|回复: 0

[经验分享] JMS调用IBM MQ 监听方式的点对点模式

[复制链接]

尚未签到

发表于 2017-5-27 08:29:19 | 显示全部楼层 |阅读模式
  前面三篇,第一篇讲了安装IBM MQ时遇到的一些问题。第二篇讲了点对点模式的调用。第三篇讲了发布订阅模式。本篇说一下监听模式。
  监听模式只是在消费者端监听就可以了。对于消息发布者,代码不用做改动。改动的代码用<span>标签标注(代码不能修改颜色?):

package test2;
// SCCSID "@(#) MQMBID sn=p000-L120604 su=_H-IvIK4nEeGko6IWl3MDhA pn=MQJavaSamples/jms/JmsConsumer.java"
/*
*   <copyright
*   notice="lm-source-program"
*   pids="5724-H72,5655-R36,5655-L82,5724-L26,"
*   years="2008,2012"
*   crc="39457954" >
*  Licensed Materials - Property of IBM  
*   
*  5724-H72,5655-R36,5655-L82,5724-L26,
*   
*  (C) Copyright IBM Corp. 2008, 2012 All Rights Reserved.  
*   
*  US Government Users Restricted Rights - Use, duplication or  
*  disclosure restricted by GSA ADP Schedule Contract with  
*  IBM Corp.  
*   </copyright>
*/
import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.TextMessage;
import com.ibm.msg.client.jms.JmsConnectionFactory;
import com.ibm.msg.client.jms.JmsFactoryFactory;
import com.ibm.msg.client.wmq.WMQConstants;
/**
* A JMS consumer (receiver or subscriber) application that receives a message from the named
* destination (queue or topic).
*
* Tip: A subscriber application must be started before the publisher application.
*
* Notes:
*
* API type: IBM JMS API (v1.1, unified domain)
*
* Messaging domain: Point-to-point or Publish-Subscribe
*
* Provider type: WebSphere MQ
*
* Connection mode: Client connection
*
* JNDI in use: No
*
* Usage:
*
* JmsConsumer -m queueManagerName -d destinationName [-h host -p port -l channel]
*
* for example:
*
* JmsConsumer -m QM1 -d Q1
*
* JmsConsumer -m QM1 -d topic://foo -h localhost -p 1414
*/
public class JmsConsumer {
private static String host = "localhost";
private static int port = 1414;
private static String channel = "SYSTEM.DEF.SVRCONN";
private static String queueManagerName = null;
private static String destinationName = null;
private static boolean isTopic = true;
private static int timeout = 15000; // in ms or 15 seconds
// System exit status value (assume unset value to be 1)
private static int status = 1;
/**
* Main method
*
* @param args
*/
public static void main(String[] args) {
// Parse the arguments
args = new String[]{"-m","QMTest", "-d","testQueue"};
parseArgs(args);
// Variables
Connection connection = null;
Session session = null;
Destination destination = null;
MessageConsumer consumer = null;
try {
// Create a connection factory
JmsFactoryFactory ff = JmsFactoryFactory.getInstance(WMQConstants.WMQ_PROVIDER);
JmsConnectionFactory cf = ff.createConnectionFactory();
// Set the properties
cf.setStringProperty(WMQConstants.WMQ_HOST_NAME, host);
cf.setIntProperty(WMQConstants.WMQ_PORT, port);
cf.setStringProperty(WMQConstants.WMQ_CHANNEL, channel);
cf.setIntProperty(WMQConstants.WMQ_CONNECTION_MODE, WMQConstants.WMQ_CM_CLIENT);
cf.setStringProperty(WMQConstants.WMQ_QUEUE_MANAGER, queueManagerName);
// Create JMS objects
connection = cf.createConnection();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
if (isTopic) {
destination = session.createTopic(destinationName);
}
else {
destination = session.createQueue(destinationName);
}
consumer = session.createConsumer(destination);
// Start the connection
connection.start();
      consumer.setMessageListener(new MessageListener(){
public void onMessage(Message m) {
//如果想收到完整的消息“
System.out.println(m);
//如果想收到消息内容,执行下面的代码
TextMessage textMsg = (TextMessage) m;
try {
System.out.println(textMsg.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
});

// And, receive the message
//在指定的超市时间内接收下一条消息
//      Message message = consumer.receive(timeout);
//      if (message != null) {
//      System.err.println("Received message:\n" + message);
////        System.out.println("Received message:\n" + message);
//      }
//      else {
//        System.out.println("No message received!\n");
//        recordFailure(null);
//      }
recordSuccess();
}
catch (JMSException jmsex) {
recordFailure(jmsex);
}
finally {
if (consumer != null) {
try {
consumer.close();
}
catch (JMSException jmsex) {
System.out.println("Consumer could not be closed.");
recordFailure(jmsex);
}
}
if (session != null) {
try {
session.close();
}
catch (JMSException jmsex) {
System.out.println("Session could not be closed.");
recordFailure(jmsex);
}
}
if (connection != null) {
try {
connection.close();
}
catch (JMSException jmsex) {
System.out.println("Connection could not be closed.");
recordFailure(jmsex);
}
}
}
System.exit(status);
return;
} // end main()
/**
* Process a JMSException and any associated inner exceptions.
*
* @param jmsex
*/
private static void processJMSException(JMSException jmsex) {
System.out.println(jmsex);
Throwable innerException = jmsex.getLinkedException();
if (innerException != null) {
System.out.println("Inner exception(s):");
}
while (innerException != null) {
System.out.println(innerException);
innerException = innerException.getCause();
}
return;
}
/**
* Record this run as successful.
*/
private static void recordSuccess() {
System.out.println("SUCCESS");
status = 0;
return;
}
/**
* Record this run as failure.
*
* @param ex
*/
private static void recordFailure(Exception ex) {
if (ex != null) {
if (ex instanceof JMSException) {
processJMSException((JMSException) ex);
}
else {
System.out.println(ex);
}
}
System.out.println("FAILURE");
status = -1;
return;
}
/**
* Parse user supplied arguments.
*
* @param args
*/
private static void parseArgs(String[] args) {
try {
int length = args.length;
if (length == 0) {
throw new IllegalArgumentException("No arguments! Mandatory arguments must be specified.");
}
if ((length % 2) != 0) {
throw new IllegalArgumentException("Incorrect number of arguments!");
}
int i = 0;
while (i < length) {
if ((args).charAt(0) != '-') {
throw new IllegalArgumentException("Expected a '-' character next: " + args);
}
char opt = (args).toLowerCase().charAt(1);
switch (opt) {
case 'h' :
host = args[++i];
break;
case 'p' :
port = Integer.parseInt(args[++i]);
break;
case 'l' :
channel = args[++i];
break;
case 'm' :
queueManagerName = args[++i];
break;
case 'd' :
destinationName = args[++i];
break;
default : {
throw new IllegalArgumentException("Unknown argument: " + opt);
}
}
++i;
}
if (queueManagerName == null) {
throw new IllegalArgumentException("A queueManager name must be specified.");
}
if (destinationName == null) {
throw new IllegalArgumentException("A destination name must be specified.");
}
// Whether the destination is a queue or a topic. Apply a simple check.
if (destinationName.startsWith("topic://")) {
isTopic = true;
}
else {
// Otherwise, let's assume it is a queue.
isTopic = false;
}
}
catch (Exception e) {
System.out.println(e.getMessage());
printUsage();
System.exit(-1);
}
return;
}
/**
* Display usage help.
*/
private static void printUsage() {
System.out.println("\nUsage:");
System.out
.println("JmsConsumer -m queueManagerName -d destinationName [-h host -p port -l channel]");
return;
}
} // end class

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-381521-1-1.html 上篇帖子: IBM InfoSphere Change Data Capture, Version 6.3.1 Fix Pack 3 下篇帖子: 2010年美专利全球50强公司排名发布 IBM蝉联榜首
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表