一. 所需依赖包,安装 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 消息发送与获取的更多相关文章

  1. IBM WebSphere MQ介绍安装以及配置服务详解

    首先介绍一下MQ MQ消息队列的简称是一种应用程序对应用程序的通信方法.说白了也就是通过队列的方式来对应用程序进行数据通信.而无需专用链接来链接它们. MQ的通讯方式 1.数据报的方式 Datagra ...

  2. IBM WebSphere MQ介绍安装以及配置服务详解(转)

    首先介绍一下MQ MQ消息队列的简称是一种应用程序对应用程序的通信方法.说白了也就是通过队列的方式来对应用程序进行数据通信.而无需专用链接来链接它们. MQ的通讯方式 1.数据报的方式 Datagra ...

  3. IBM WebSphere MQ 7.5基本用法

    一.下载7.5 Trial版本 http://www.ibm.com/developerworks/downloads/ws/wmq/ 这是下载网址,下载前先必须注册IBM ID,下载完成后一路Nex ...

  4. 使用JMS接口接入WebSphere MQ消息

    在你的应用程序中利用IBM WebSphere MQ消息中间件提供Java消息服务开放接口. IBM WebSphere MQ(WMQ)是一套面向消息的中间件(message-oriented mid ...

  5. IBM WebSphere MQ 通道类型配置

    IBM WebSphere MQ 通道类型配置 初学MQ,四种常见通道,windows下操作 目录 Sender--Receiver Server-Receiver Server-Requester ...

  6. IBM websphere MQ使用说明

    百度文库: IBM websphere MQ使用说明 IBM MQ安装和配置

  7. IBM WebSphere MQ安装及配置详解

    打开MQ安装程序,选择下一步,默认安装WebSphere MQ, 完成MQ的安装工作,启动WebSphere MQ, 服务器配置,选择新建队列管理器,创建名为 "mq"的队列管理器 ...

  8. 【RocketMQ】MQ消息发送

    消息发送 首先来看一个RcoketMQ发送消息的例子: @Service public class MQService { @Autowired DefaultMQProducer defaultMQ ...

  9. IBM WebSphere MQ的oracle的jdbc

    一.IBM WebSphere MQ7.0的jdbc支持数据库有: DB2 Informix Informix_With_Date_Format Microsoft_SQL_Server Oracle ...

随机推荐

  1. MFC中设置对话框/窗体大小固定

    对话框:1.响应WM_GETMINMAXINFO消息(设置lpMMI->ptMinTrackSize和lpMMI->ptMaxTrackSize)2.响应消息WM_SYSCOMMAND,屏 ...

  2. hadoop常见问题总结1

    本文地址:http://www.cnblogs.com/archimedes/p/hadoop-problem1.html,转载请注明源地址. 问题1:http://localhost:50030 H ...

  3. 步步为营_Android开发课[3]_Activity学习

    Focus on technology, enjoy life! -- QQ:804212028 浏览链接:http://blog.csdn.net/y18334702058/article/deta ...

  4. TortoiseSVN 源代码下载

    SVN的客户端软件TortoiseSVN http://tortoisesvn.tigris.org/ 这是我采用的 TortoiseSVN 的官方网站,页面上的两只小乌龟真的很好看. 目前最新的版本 ...

  5. Python中的作用域

    Python中的作用域 Python 中,一个变量的作用域总是由在代码中被赋值的地方所决定的. 当 Python 遇到一个变量的话他会按照这样的顺序进行搜索: 本地作用域(Local)→当前作用域被嵌 ...

  6. object sender, LinkLabelLinkClickedEventArgs e 参数解释

    开始是一个LinkLabel 控件,LinkLabel 绑定了Clicke事件 LinkLabel Delete = new LinkLabel(); Delete.Text = "删除&q ...

  7. [置顶] ArcGIS Runtime SDKs 10.2 for iOS & Android& OS X发布

    我们高兴的宣布:ArcGISRuntime SDKs 10.2 for iOS & Android & OS X正式发布!在10.2版本中,你可以在iOS.Android和Mac设备上 ...

  8. Mysql Field * doesn't have a default value解决方法

    Mysql Field * doesn't have a default value解决方法 MySQL 5中,出现错误提示: Field 'id' doesn't have a default va ...

  9. xampp集成包如何重置mysql的密码

    转自:http://blog.sina.com.cn/s/blog_4b2bcac501013s4l.html 安装使用xampp,装好后root默认没有密码,phpmyadmin是用config文件 ...

  10. vnc/route/ifconfig 配置

    重启网卡的方法: 1 network 利用root帐户 # service network restart ############################################## ...