Reputation: 140
I want to send and receive a file to WebSphere mq by java, i don't know how to set about it, could there any one help me?, a java sample is desirable thanks
Upvotes: 0
Views: 10200
Reputation: 7476
There is a free open source project written in Java called Universal File Mover (UFM) that does exactly that. You can find UFM at http://www.capitalware.biz/ufm_overview.html
Go download the source code and see how to do it or simply use UFM as is.
Upvotes: 1
Reputation: 15273
1) First you will need to write some code to read data from the file that you want to send.
2) Next refer to MQ Java samples that send/receive messages to/from a queue. MQSample.java is a good example for you. You need to modify the sample to set the data you read from the file. Something like:
// Define a simple WebSphere MQ Message ...
MQMessage msg = new MQMessage();
// ... and write some text in UTF8 format
msg.write(fileData);
queue.put(msg);
3) On the receive side do the reverse
queue.get(msg);
msg.readFully(byte[]);
Upvotes: 0
Reputation: 1258
Whenever you ask a question, you should first tell about approaches you have tried for your problem. Right now you sound like you are asking someone to write code for you which is not the motive of SO. Keep this in mind for your next question.
Now, we come to your question. What you are trying to achieve is actually very simple.
You have to:
- Read the file from your Java program into a string(Hope you want to transfer Text file only).
- Create an MQMessage.
- Write the string into the MQMessage.
- Post the MQMessage in the queue.
Sending Side
Code for reading file into a string variable:
static String readFile(String path, Charset encoding)
throws IOException
{
byte[] encoded = Files.readAllBytes(Paths.get(path));
return encoding.decode(ByteBuffer.wrap(encoded)).toString();
}
Reference: here
Code for creating MQMessage from this string and posting into the queue:
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();
Receiving Side
Code for reading message from the queue:
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());
After receiving the message in a string variable you can use the data the way you want or you can save it in a file.
Upvotes: 2