WebSphere MQ发送文件范例

最近在研究使用WebSphere MQ发送、接收文件。

package com.genersoft.test;
 import java.io.File;
 import java.io.FileInputStream;
 import com.ibm.mq.MQC;
 import com.ibm.mq.MQEnvironment;
 import com.ibm.mq.MQException;
 import com.ibm.mq.MQMessage;
 import com.ibm.mq.MQPutMessageOptions;
 import com.ibm.mq.MQQueue;
 import com.ibm.mq.MQQueueManager;
 /**
 *
 * @author fts
 * @date 2016-01-22
 * @功能 发送文件
 *
 */
 public class MQFileSender {
 final int BUFFER_LEN = 1024*1024*3;
 private MQQueueManager qmgr;
 private MQQueue outQueue;
 private String queueName = “RECVQ”;
 private String host = “10.24.1.20”;
 private int port = 1414;
 private int ccsid = 1381;
 private String channel = “RECVQCHANNEL”;
 private String qmgrName = “GATEWAY1”;
 private MQMessage outMsg;
 private MQPutMessageOptions pmo;
 private String fileName = “D:\\1.jpg”;
 //private String fileName = “D:\\OperateFile\\New.java”;
 public static void main(String args[]) {
 MQFileSender mfs = new MQFileSender();
 mfs.runGoupSender();
 }
 public void runGoupSender() {
 try {
 init();
 sendGroupMessages();
 qmgr.commit();
 System.out.println(“\n Messages successfully Send “);
 } catch (MQException mqe) {
 mqe.printStackTrace();
 try {
 System.out.println(“\n Backing out Transaction “);
 qmgr.backout();
 System.exit(2);
 } catch (Exception e) {
 e.printStackTrace();
 System.exit(2);
 }
 } catch (Exception e) {
 e.printStackTrace();
 System.exit(2);
 }
 }
 private void init() throws Exception {
 /** 设置MQEnvironment 属性以便客户机连接 */
 MQEnvironment.hostname = host;
 MQEnvironment.channel = channel;
 MQEnvironment.port = port;
 MQEnvironment.CCSID = ccsid;
 // MQEnvironment.properties.put(MQC.TRANSPORT_PROPERTY,MQC.TRANSPORT_WEBSPHERE
 // MQ);
 /* 连接到队列管理器 */
 qmgr = new MQQueueManager(qmgrName);
 /* 设置队列打开选项以便输出 */
 int opnOptn = MQC.MQOO_OUTPUT | MQC.MQOO_FAIL_IF_QUIESCING;
 outQueue = qmgr.accessQueue(queueName, opnOptn, null, null, null);
 }
 public void sendGroupMessages() throws Exception {
 /* 设置放置消息选项 */
 pmo = new MQPutMessageOptions();
 pmo.options = pmo.options + MQC.MQPMO_LOGICAL_ORDER;
 pmo.options = pmo.options + MQC.MQPMRF_GROUP_ID;
 outMsg = new MQMessage();
 /* 设置消息标记,表示该消息属于组 */
 outMsg.messageFlags = MQC.MQMF_MSG_IN_GROUP;
 FileInputStream fis = new FileInputStream(new File(fileName));
 byte buffer[] = new byte[BUFFER_LEN];
 int count = 0;
 while (true) {
 count = fis.read(buffer, 0, BUFFER_LEN);
 if(count == -1){
 break;
 }
 outMsg.write(buffer,0,count);
 if(count < BUFFER_LEN){
 outMsg.messageFlags = MQC.MQMF_LAST_MSG_IN_GROUP;
 }
 outQueue.put(outMsg, pmo);
 outMsg.clearMessage();
 }
 fis.close();
 }
 }

接收文件代码:

package com.genersoft.test;
 import java.io.File;
 import java.io.FileOutputStream;
 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.MQQueue;
 import com.ibm.mq.MQQueueManager;
 /**
 *
 * @author futsh
 * @date 2016-01-22
 * @功能 接收文件
 *
 */
 public class MQFileReceiver {
 private MQQueueManager qmgr;
 private MQQueue inQueue;
 private String queueName = “RECVQ”;
 private String host = “10.24.1.20”;
 private int port = 1414;
 private int ccsid = 1381;
 private String channel = “RECVQCHANNEL”;
 private String qmgrName = “GATEWAY1”;
 private MQMessage inMsg;
 private MQGetMessageOptions gmo;
 private String fileName = “D:\\2.jpg”;
 //private String fileName = “D:\\OperateFile\\mq\\New.java”;
 public static void main(String args[]) {
 MQFileReceiver mfr = new MQFileReceiver();
 mfr.runGoupReceiver();
 }
 public void runGoupReceiver() {
 try {
 init();
 getGroupMessages();
 qmgr.commit();
 System.out.println(“\n Messages successfully Receive “);
 } catch (MQException mqe) {
 mqe.printStackTrace();
 try {
 System.out.println(“\n Backing out Transaction “);
 qmgr.backout();
 System.exit(2);
 } catch (Exception e) {
 e.printStackTrace();
 System.exit(2);
 }
 } catch (Exception e) {
 e.printStackTrace();
 System.exit(2);
 }
 }
 private void init() throws Exception {
 /* 为客户机连接设置MQEnvironment 属 */
 MQEnvironment.hostname = host;
 MQEnvironment.channel = channel;
 MQEnvironment.port = port;
 MQEnvironment.CCSID = ccsid;
 /* 连接到队列管理器 */
 qmgr = new MQQueueManager(qmgrName);
 /* 设置队列打开选项以输 */
 int opnOptn = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_FAIL_IF_QUIESCING;
 /* 打开队列以输 */
 inQueue = qmgr.accessQueue(queueName, opnOptn, null, null, null);
 }
 public void getGroupMessages() throws Exception {
 /* 设置获取消息选项 */
 gmo = new MQGetMessageOptions();
 gmo.options = MQC.MQGMO_FAIL_IF_QUIESCING;
 gmo.options = gmo.options + MQC.MQGMO_SYNCPOINT;
 /* 等待消息 */
 gmo.options = gmo.options + MQC.MQGMO_WAIT;
 /* 设置等待时间限制 */
 gmo.waitInterval = 5000;
 /* 只获取消息*/
 gmo.options = gmo.options + MQC.MQGMO_ALL_MSGS_AVAILABLE;
 /* 以辑顺序获取消息 */
 gmo.options = gmo.options + MQC.MQGMO_LOGICAL_ORDER;
 gmo.matchOptions = MQC.MQMO_MATCH_GROUP_ID;
 /* 创建消息缓冲*/
 inMsg = new MQMessage();
 FileOutputStream fos = new FileOutputStream(new File(fileName));
 /* 处理组消息*/
 while (true) {
 inQueue.get(inMsg, gmo);
 int msgLength = inMsg.getMessageLength();
 byte[] buffer = new byte[msgLength];
 inMsg.readFully(buffer);
 fos.write(buffer, 0, msgLength);
 char x = gmo.groupStatus;
 /*查看是否是最后消息标*/
 if (x == MQC.MQGS_LAST_MSG_IN_GROUP) {
 System.out.println(“Last Msg in Group”);
 break;
 }
 inMsg.clearMessage();
 }
 fos.close();
 }
 }

有时候可能会出现如下错误:

Exception in thread “main” java.lang.NoClassDefFoundError: javax/resource/ResourceException
 at com.ibm.mq.MQEnvironment. <clinit>(MQEnvironment.java:441)
 at com.ibm.mq.pcf.PCFAgent.connect(PCFAgent.java:276)
 at com.ibm.mq.pcf.PCFAgent. <init>(PCFAgent.java:190)
 at com.ibm.mq.pcf.PCFMessageAgent. <init>(PCFMessageAgent.java:160)
 at MQTest.MQTestMain.testName(MQTestMain.java:33)
 at MQTest.MQTestMain.main(MQTestMain.java:17)

解决: 增加 mq_install\java\lib到你的PATH javax/resource/ResourceException 这个类是在connector.jar中

附:connector.jar以及com.ibm.mq.jar

MQ所需jar包

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏飞扬的花生

jsencrypt参数前端加密c#解密

      写程序时一般是通过form表单或者ajax方式将参数提交到服务器进行验证,如何防止提交的请求不被抓包后串改,虽然无法说绝对安全却给非法提交提高了难度...

3859
来自专栏张善友的专栏

Mix 10 上的asp.net mvc 2的相关Session

Beyond File | New Company: From Cheesy Sample to Social Platform Scott Hansel...

2547
来自专栏张善友的专栏

LINQ via C# 系列文章

LINQ via C# Recently I am giving a series of talk on LINQ. the name “LINQ via C...

2625
来自专栏pangguoming

Spring Boot集成JasperReports生成PDF文档

由于工作需要,要实现后端根据模板动态填充数据生成PDF文档,通过技术选型,使用Ireport5.6来设计模板,结合JasperReports5.6工具库来调用渲...

1.2K7
来自专栏张善友的专栏

Miguel de Icaza 细说 Mix 07大会上的Silverlight和DLR

Mono之父Miguel de Icaza 详细报道微软Mix 07大会上的Silverlight和DLR ,上面还谈到了Mono and Silverligh...

2707
来自专栏闻道于事

js登录滑动验证,不滑动无法登陆

js的判断这里是根据滑块的位置进行判断,应该是用一个flag判断 <%@ page language="java" contentType="text/html...

6768
来自专栏杨龙飞前端

scrollto 到指定位置

2494
来自专栏我和未来有约会

Silverlight第三方控件专题

这里我收集整理了目前网上silverlight第三方控件的专题,若果有所遗漏请告知我一下。 名称 简介 截图 telerik 商 RadC...

3985
来自专栏芋道源码1024

熔断器 Hystrix 源码解析 —— 断路器 HystrixCircuitBreaker

本文主要基于 Hystrix 1.5.X 版本 1. 概述 2. HystrixCircuitBreaker 3. HystrixCircuitBreaker....

5287
来自专栏魂祭心

原 canvas绘制clock

4054

扫码关注云+社区