【发布时间】:2013-10-03 07:37:09
【问题描述】:
我想通过java向WebSphere mq发送和接收文件,我不知道如何设置,有没有人可以帮助我?,需要一个java示例 谢谢
【问题讨论】:
标签: java ibm-mq file-transfer
我想通过java向WebSphere mq发送和接收文件,我不知道如何设置,有没有人可以帮助我?,需要一个java示例 谢谢
【问题讨论】:
标签: java ibm-mq file-transfer
有一个用 Java 编写的名为 Universal File Mover (UFM) 的免费开源项目正是这样做的。你可以在http://www.capitalware.biz/ufm_overview.html找到UFM
去下载源代码看看怎么做,或者直接使用 UFM。
【讨论】:
1) 首先,您需要编写一些代码来从您要发送的文件中读取数据。
2) 接下来参考向/从队列发送/接收消息的 MQ Java 示例。 MQSample.java 是一个很好的例子。您需要修改示例以设置您从文件中读取的数据。比如:
// Define a simple WebSphere MQ Message ...
MQMessage msg = new MQMessage();
// ... and write some text in UTF8 format
msg.write(fileData);
queue.put(msg);
3) 在接收端做相反的操作
queue.get(msg);
msg.readFully(byte[]);
【讨论】:
每当您提出问题时,您都应该首先说明您针对问题尝试过的方法。现在,您听起来像是在要求某人为您编写代码,这不是 SO 的动机。 在您的下一个问题中记住这一点。
现在,我们来回答您的问题。您要实现的目标实际上非常简单。
你必须:
- 从您的 Java 程序中读取文件到一个字符串中(希望您只想传输文本文件)。
- 创建一个 MQMessage。
- 将字符串写入 MQMessage。
- 在队列中发布 MQMessage。
发送方
将文件读入字符串变量的代码:
static String readFile(String path, Charset encoding)
throws IOException
{
byte[] encoded = Files.readAllBytes(Paths.get(path));
return encoding.decode(ByteBuffer.wrap(encoded)).toString();
}
参考:here
用于从该字符串创建 MQMessage 并发布到队列中的代码:
MQQueueManager qMgr = new MQQueueManager(YourQueueManagerName);
// Set up the options on the queue we wish to open...
int openOptions = MQC.MQOO_INPUT_AS_Q_DEF |
MQC.MQOO_OUTPUT ;
// Now specifythe queue that we wish to open, and the open options...
MQQueue inputQ =
qMgr.accessQueue(Inputqueue,openOptions);
Charset charset = Charset.forName("UTF-8");
String messg=(readFile("C:\test.txt", charset));
MQMessage InputMsg1 = new MQMessage();
InputMsg1.writeString(messg);
MQPutMessageOptions pmo = new MQPutMessageOptions();
inputQ.put(InputMsg1,pmo);
inputQ.close();
qMgr.disconnect();
接收方
从队列中读取消息的代码:
MQQueueManager qMgr2 = new MQQueueManager(OutQM);
// Set up the options on the queue we wish to open...
int openOptions2 = MQC.MQOO_INPUT_AS_Q_DEF |
MQC.MQOO_OUTPUT ;
// Now specifythe queue that we wish to open, and the open options...
MQQueue Output =
qMgr2.accessQueue(OutQ,openOptions2);
MQMessage retrievedMessage = new MQMessage();
MQGetMessageOptions gmo = new MQGetMessageOptions(); // accept the defaults
// // same as
// // MQGMO_DEFAULT
// // get the message off the queue..
Output.get(retrievedMessage, gmo);
String msgText = retrievedMessage.readString(retrievedMessage.getDataLength());
在字符串变量中接收到消息后,您可以按照自己的方式使用数据,也可以将其保存在文件中。
【讨论】: