ibm mq java 发布订阅,JMS调用IBM MQ 监听模式的点对点模式
JMS调用IBM MQ 监听方式的点对点模式
前面三篇,第一篇讲了安装IBM MQ时遇到的一些问题。第二篇讲了点对点模式的调用。第三篇讲了发布订阅模式。本篇说一下监听模式。
监听模式只是在消费者端监听就可以了。对于消息发布者,代码不用做改动。改动的代码用标签标注(代码不能修改颜色?):
package test2;
// SCCSID "@(#) MQMBID sn=p000-L120604 su=_H-IvIK4nEeGko6IWl3MDhA pn=MQJavaSamples/jms/JmsConsumer.java"
/*
*
* 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.
*
*/
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:
" + message);
System.out.println("Received message:
" + message);
// }
// else {
// System.out.println("No message received!
");
// 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[i]).charAt(0) != '-') {
throw new IllegalArgumentException("Expected a '-' character next: " + args[i]);
}
char opt = (args[i]).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("
Usage:");
System.out
.println("JmsConsumer -m queueManagerName -d destinationName [-h host -p port -l channel]");
return;
}
} // end class
ibm mq java 发布订阅,JMS调用IBM MQ 监听模式的点对点模式相关推荐
- java 集成ibm mq 教程_Spring Boot JMS与IBM WebSphere MQ集成配置
Spring Boot JMS与IBM WebSphere MQ集成配置 [TOC] 前言 Spring Boot作为简化Spring开发的框架,已经为我们集成了ActiveMQ和RabbitMQ.只 ...
- JMS调用IBM MQ监听模式
最近几天一直纠结于监听模式.在监听模式下遇到了两个问题. 1.消息接收者不关闭竟然收不到消息. 2.在监听模式下,每次只收一条消息,即使设定了监听,也是只收到一条消息 今天就针对这两个问题作出解答. ...
- MQTT的学习研究(十三) IBM MQTTV3 简单发布订阅实例
使用IBM MQTTv3实现相关的发布订阅功能 MQTTv3的发布消息的实现: Java代码 package com.etrip.mqttv3; import com.ibm.micro.clie ...
- java redis mq_redis之mq实现发布订阅模式
概述 Redis不仅可作为缓存服务器,还可用作消息队列,本示例演示如何使用redis实现发布/订阅消息队列. 在Redis中,发布者没有将消息发送给特定订阅者的程序.相反,发布的消息被描述为通道,而不 ...
- redis之mq实现发布订阅模式
https://github.com/smltq/spring-boot-demo/blob/master/mq-redis 概述 Redis不仅可作为缓存服务器,还可用作消息队列,本示例演示如何使用 ...
- redis java 发布订阅_Redis之发布订阅(Java)
上一章节我们已经学会了Redis在Java项目里面最基本的应用,我们这一章节来讲一讲Redis里面一个非常重要的功能:发布订阅 发布订阅(Pub/Sub):目前广泛使用的通信模型,它采用事件作为基本的 ...
- redis java 发布订阅_【Redis学习系列】Redis发布订阅
Redis 发布订阅 Redis 发布订阅 (pub/sub) 是一种消息通信模式:发送者 (pub) 发送消息,订阅者 (sub) 接收消息. Redis 客户端可以订阅任意数量的频道. 下图展示了 ...
- java监控activemq,ActiveMQ与Spring整合-监听消息
本课程全程使用目前比较流行的开发工具idea进行开发,涉及到目前互联网项目中常用的高并发解决方案技术, 如 dubbo,redis,solr,freemarker,activeMQ,springBo ...
- java session 数量_java中使用session监听实现同帐号登录限制、登录人数限制
本文主要介绍了java中使用session监听实现同帐号登录限制.登录人数限制,具体代码如下: 问题域: 1.同帐号登录:若此帐号已登录,不可再次登录(与QQ模式相反). 2.登录人数限制,超过.已达 ...
最新文章
- Windows下使用Notepad++修改二进制文件,exe可执行文件
- C++基础学习7:new/delete操作符
- android 小工具:pc 上用 curl 命令打开手机浏览器,浏览指定网址
- python端口扫描工具_Python实现的多线程端口扫描工具分享
- git 创建webpack项目_从0到1开发一个小程序cli脚手架(一)创建页面/组件模版篇...
- NLP《词汇表示方法(六)ELMO》
- CodeIgniter URL添加后缀
- python怎样创建项目_如何建立一个完美的 Python 项目
- python 取模是什么意思_编程语言中,取余和取模的区别到底是什么?
- JS 打印 data数据_用D3.js 十分钟实现字符跳动效果
- 微信小程序优惠券的购买和核销的操作流程设计
- 《数学分析新讲》_张筑生,12.5节:隐函数定理(1)
- 内存数据库 TimesTen
- 知识图谱构建通俗理解
- 访问k8s集群出现Unable to connect to the server: x509: certificate is valid for xxx, not xxx问题解决【详细步骤】
- เล่น บาคาร่ารับสิทธิประโยชน์มากมาย
- CentOS 7 几个版本
- NodeJS C++ Addons基础
- [Windows]_[系统内部版本号对照表]
- 数字信号处理(五)快速傅里叶变换