IBM websphere MQ 消息发送与获取
一. 所需依赖包,安装 IBM websphere MQ 后,在安装目录下的 java 目录内
import java.io.IOException;
import java.util.Properties; import com.ibm.mq.MQC;
import com.ibm.mq.MQEnvironment;
import com.ibm.mq.MQException;
import com.ibm.mq.MQGetMessageOptions;
import com.ibm.mq.MQMessage;
import com.ibm.mq.MQPutMessageOptions;
import com.ibm.mq.MQQueue;
import com.ibm.mq.MQQueueManager;
import com.ibm.mq.constants.CMQC;
import com.ibm.mq.constants.MQConstants; /**
* 客户端模式开发
*
* @author Geely
*
*/
public class MQTest1 { public static void main(String[] args) throws MQException, IOException { // 发送消息给队列
put(); // 从队列读取消息
get(); // 获取队列深度
getDepth(); } @SuppressWarnings("unchecked")
static void put() throws MQException, IOException { // 配置MQ服务器连接参数
MQEnvironment.hostname = "127.0.0.1";
MQEnvironment.port = 1414;
MQEnvironment.channel = "QM_ACK";
// MQEnvironment.userID = "";
// MQEnvironment.password = "";
// 设置队列管理器字符集 编码
MQEnvironment.CCSID = 1381; // 设置应用名称,方便服务器MQ 查看应用连接
MQEnvironment.properties.put(MQConstants.APPNAME_PROPERTY, "MQ Test By Java"); // 设置应用名称,方便服务器MQ 查看应用连接
MQEnvironment.properties.put(MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES); // 创建实例,连接队列管理器
MQQueueManager queueManager = new MQQueueManager("QM",MQEnvironment.properties); // 以可写的方式访问队列管理器已定义的队列QUEUE1,当然也可以创建队列
MQQueue putQueue = queueManager.accessQueue("QUEUE_RECV", CMQC.MQOO_OUTPUT); // 新建并发送消息给队列
MQMessage myMessage = new MQMessage();
myMessage.expiry = -1; // 设置消息用不过期
String name = "MePlusPlus's 博客";
myMessage.writeUTF(name); // 使用默认的消息选项
MQPutMessageOptions pmo = new MQPutMessageOptions();
// 发送消息
putQueue.put(myMessage, pmo);
putQueue.close(); // 断开连接
queueManager.disconnect();
} @SuppressWarnings("unchecked")
static void get() throws MQException, IOException { // 配置MQ服务器连接参数
MQEnvironment.hostname = "127.0.0.1";
MQEnvironment.port = 1414;
MQEnvironment.channel = "QM_ACK";
// MQEnvironment.userID = "";
// MQEnvironment.password = "";
// 设置队列管理器字符集 编码
MQEnvironment.CCSID = 1381; // 设置应用名称,方便服务器MQ 查看应用连接
MQEnvironment.properties.put(MQConstants.APPNAME_PROPERTY, "MQ Test By Java"); // 设置应用名称,方便服务器MQ 查看应用连接
MQEnvironment.properties.put(CMQC.TRANSPORT_PROPERTY, CMQC.TRANSPORT_MQSERIES_BINDINGS); // 创建实例,连接队列管理器
MQQueueManager queueManager = new MQQueueManager("QM",MQEnvironment.properties); // 打开队列.
int openOptions = CMQC.MQOO_INPUT_AS_Q_DEF|CMQC.MQOO_OUTPUT|CMQC.MQOO_INQUIRE; // 以可读的方式访问队列管理器已定义的队列QUEUE1
// MQQueue getQueue = queueManager.accessQueue("QUEUE_RECV",CMQC.MQOO_INPUT_AS_Q_DEF);
MQQueue getQueue = queueManager.accessQueue("QUEUE_RECV", openOptions, null, null, null); MQGetMessageOptions gmo = new MQGetMessageOptions();
// Get messages under sync point control.
// 在同步点控制下获取消息.
//gmo.options = gmo.options + CMQC.MQGMO_SYNCPOINT;
// Wait if no messages on the Queue.
// 如果在队列上没有消息则等待.
//gmo.options = gmo.options + CMQC.MQGMO_WAIT;
// Fail if QeueManager Quiescing.
// 如果队列管理器停顿则失败.
//gmo.options = gmo.options + CMQC.MQGMO_FAIL_IF_QUIESCING;
// Sets the time limit for the wait.
// 设置等待的时间限制.
gmo.waitInterval = 3000; // 从队列读取消息
MQMessage theMessage = new MQMessage();
getQueue.get(theMessage, gmo); String name = theMessage.readUTF(); System.out.println(name);
getQueue.close(); // 断开连接
queueManager.disconnect();
} static void getDepth() throws MQException, IOException { Properties props = new Properties();
props .put("hostname", "127.0.0.1");
props .put("port", 1414); //端口号
props .put("channel", "QM_ACK"); //服务器连接通道
props .put("CCSID", 1381);
props .put("transport", "MQSeries"); // 创建实例,连接队列管理器
MQQueueManager queueManager = new MQQueueManager("QM",props ); // 打开队列.
int openOptions = CMQC.MQOO_INPUT_AS_Q_DEF|CMQC.MQOO_OUTPUT|CMQC.MQOO_INQUIRE;
// 以可读的方式访问队列管理器已定义的队列QUEUE1
MQQueue getQueue = queueManager.accessQueue("QUEUE_RECV", openOptions, null, null, null); MQGetMessageOptions gmo = new MQGetMessageOptions();
// Get messages under sync point control.
// 在同步点控制下获取消息.
// gmo.options = gmo.options + CMQC.MQGMO_SYNCPOINT;
// Wait if no messages on the Queue.
// 如果在队列上没有消息则等待.
// gmo.options = gmo.options + CMQC.MQGMO_WAIT;
// Fail if QeueManager Quiescing.
// 如果队列管理器停顿则失败.
// gmo.options = gmo.options + CMQC.MQGMO_FAIL_IF_QUIESCING; // Sets the time limit for the wait.
// 设置等待的时间限制.
gmo.waitInterval = 3000; int depth = getQueue.getCurrentDepth();
System.out.println("该队列当前的深度为:"+depth);
System.out.println("===========================");
while(depth-->0)
{
MQMessage msg = new MQMessage();// 要读的队列的消息
getQueue.get(msg, gmo);
System.out.println("消息的大小为:"+msg.getDataLength());
System.out.println("消息的内容:\n"+msg.readUTF());
System.out.println("---------------------------");
}
System.out.println("Finish!!!"); getQueue.close(); // 断开连接
queueManager.disconnect(); } }
JAVA从MQ读取消息的时候报错及解决
JAVA通过writeUTF将消息写入到MQ,再通过JAVA采用MQMessage读消息的方法readUTF()去读取的时候,就不会报错,可以正常读出来。但是如果采用在MQ资源管理器中插入测试消息或者是通过另外一台MQ服务器往当前MQ服务器通过远程队例写消息过来,通过JAVA读取出会错.
MQMessage mqmsg = new MQMessage();// 要读的队列的消息
getQueue.get(mqmsg, gmo); System.out.println("消息的大小为:" + mqmsg.getDataLength()); byte[] rawData = new byte[mqmsg.getMessageLength()]; // 先转byte
mqmsg.readFully(rawData); // 读出所有数据
String msg = new String(rawData); // byte转string
System.out.println("消息的内容:\n"+msg);
IBM websphere MQ 消息发送与获取的更多相关文章
- IBM WebSphere MQ介绍安装以及配置服务详解
首先介绍一下MQ MQ消息队列的简称是一种应用程序对应用程序的通信方法.说白了也就是通过队列的方式来对应用程序进行数据通信.而无需专用链接来链接它们. MQ的通讯方式 1.数据报的方式 Datagra ...
- IBM WebSphere MQ介绍安装以及配置服务详解(转)
首先介绍一下MQ MQ消息队列的简称是一种应用程序对应用程序的通信方法.说白了也就是通过队列的方式来对应用程序进行数据通信.而无需专用链接来链接它们. MQ的通讯方式 1.数据报的方式 Datagra ...
- IBM WebSphere MQ 7.5基本用法
一.下载7.5 Trial版本 http://www.ibm.com/developerworks/downloads/ws/wmq/ 这是下载网址,下载前先必须注册IBM ID,下载完成后一路Nex ...
- 使用JMS接口接入WebSphere MQ消息
在你的应用程序中利用IBM WebSphere MQ消息中间件提供Java消息服务开放接口. IBM WebSphere MQ(WMQ)是一套面向消息的中间件(message-oriented mid ...
- IBM WebSphere MQ 通道类型配置
IBM WebSphere MQ 通道类型配置 初学MQ,四种常见通道,windows下操作 目录 Sender--Receiver Server-Receiver Server-Requester ...
- IBM websphere MQ使用说明
百度文库: IBM websphere MQ使用说明 IBM MQ安装和配置
- IBM WebSphere MQ安装及配置详解
打开MQ安装程序,选择下一步,默认安装WebSphere MQ, 完成MQ的安装工作,启动WebSphere MQ, 服务器配置,选择新建队列管理器,创建名为 "mq"的队列管理器 ...
- 【RocketMQ】MQ消息发送
消息发送 首先来看一个RcoketMQ发送消息的例子: @Service public class MQService { @Autowired DefaultMQProducer defaultMQ ...
- IBM WebSphere MQ的oracle的jdbc
一.IBM WebSphere MQ7.0的jdbc支持数据库有: DB2 Informix Informix_With_Date_Format Microsoft_SQL_Server Oracle ...
随机推荐
- xml布局内容总结(一)--Android
关于安卓项目中xml的使用非常多.为了达到一些好的UI效果.须要对xml比較熟练.会使用非常多的小技巧,本人准备对这些小技巧进行整理和总结,希望进行分享和交流. 关于weight的使用,因为weigh ...
- iOS开源项目:asi-http-request
使用CFNetwork实现的http库,能同时在iphone和macos下使用:http://allseeing-i.com/ASIHTTPRequest/ 他提供以下功能: 向服务器发送或者从服务器 ...
- 深入JavaScript模块化编程
今天看requirejs官网的manual,发现了下面这篇好文章,于是花点时间翻译了一下,翻译不好的地方请指正,谢谢! 推荐阅读原文:) http://www.adequatelygood.com ...
- Android -- 动态添加布局
在做项目的时候,遇到了scrollView与listView结合的使用,导致了滑动的混乱,但是有一个办法可以解决掉这个问题,就是手写listView的高度,还有另外一种方法,传送门:<Andro ...
- Python 谷歌翻译
#coding=utf-8 import re import urllib import urllib2 url_google = 'http://translate.google.cn' reg_t ...
- Report Studio值提示通过prompt宏函数给sql查询传参
场景:当我们在DW中新建了一个表,但是在FM中没有创建模型,想针对这个表直接做一个报表,那么就需要在reportstudio中直接用sql来查询,为了追求查询速度,我们可以把页面用户选择的条件直接传给 ...
- Linux统计/监控工具SAR详细介绍
转载:http://www.ctohome.com/FuWuQi/1b/688.html sysstat 工具简介 sysstat 是 Linux 系统中的常用工具包.它的主要用途是观察服务负载,比如 ...
- css 小问题解决方法整理
1,图片垂直居中: 设置包括图片的div:height=20px:line-height=20px. 设置图片vertical-align:middle 就可以. 2,行内块元素有3px bug,可通 ...
- android DPI与分辨率的关系及计算方式
android DPI与分辨率的关系及计算方式 Low density (120), ldpi Medium density (160), mdpi High density (240 ...
- angularjs中的$eval方法
在controller中定义了一个变量 $scope.a_1 = "abc"; 想在view里面动态输出,因为这个数字是动态的,这么输出肯定是不行的{{'a_' + '1'}},因 ...