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 ...
随机推荐
- UBUNTU 14.04 INSTALL nsenter
cd /tmp; curl https://www.kernel.org/pub/linux/utils/util-linux/v2.25/util-linux-2.25.tar.gz | tar - ...
- 使用Vue.js制作仿Metronic高级表格(一)静态设计
Metronic高级表格是Metonic框架中自行实现的表格,其底层是Datatables.本教程将主要使用Vue实现交互部分,使用Bootstrap做样式库.jQuery做部分用户交互(弹窗). 使 ...
- Ubuntu 字体设置:使用Windows 字体
基础知识 Sans-serif=无衬线体=黑体:并不是具体一款字体,而是一类字体,选择它其实等于选择这类字体中优先级最高的那款字体. Serif=衬线体=白体:同上 Monospace=等宽字体,意思 ...
- 使用ssh向sqlserver2005数据库中保存image类型的二进制图片
1.首先设计数据库表,其中photo.photo2字段均为image类型的. 2.建立实体bean对象,设置两个字段为byte[]如:private byte[] photo; private byt ...
- 使用webService时,gsoap数据类型注意事项
今天在使用gsoap生成webservice客户端文件时,发现我的参数类型全被改了,比如string型变成了char*,原来有STL的地方也变没了,经过研究发现,原来和我生成的头文件时使用的参数有关, ...
- [置顶] 自娱自乐1之Linux UDC驱动(形式模板)
首先,我不是做驱动的开发人员.所以只能用自娱自乐来表示我的行为. 我不知道udc和gadget驱动是不是冷门的驱动,资料真是不多.我之前买了一本书,上面说到这些,就教你如何调试已写好的驱动.这样也可以 ...
- 使用ReportStudio打开cube模型创建报表出现两个最细粒度名称
本人也是第一次遇到这样的问题,此问题甚是简单,也许很简短的一句话就可以解决这个问题了,看官请留神哦 cube做好发布到cognos之后使用Analysis Studio打开结构正常 于是想到要用此数据 ...
- [Firebase] 4. Firebase Object related Database
The idea: This post we are going to learn how to build a Firebase Forage with object related databas ...
- windows 7 64bit安装apche php
http://windows.php.net/download#php-5.6-ts-VC11-x64http://www.apachehaus.com/cgi-bin/download.plx 下载 ...
- Linux 重启Tomcat脚本
#!/bin/test_restart.sh #Author : Javen #Desc : restart tomcat-test tomcatpath="/home/local/test ...