WebSphere MQ发送文件范例

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包

发表评论

电子邮件地址不会被公开。 必填项已用*标注