Koen Handekyn
Tuesday, July 01, 2003
 
From: Sun Developer Network - Forums

"Hi, Could anybody tell me how to put a file in a JMS queue? Is there any way to put the file contents in the queue and retrieve it from the same? Thanks in advance."

The following will work for both text file and binary file:

Sender:

BytesMessage msg = session.createBytesMessage();
File file = new File('Your file path and name');
int length = (int) file.length(); // your file length in bytes
byte[] bytes = new byte[length];
FileInputStream stream = new FileInputStream(file);
stream.read(bytes);// read file into bytes
msg.writeInt(length);// used for receiver
msg.writeBytes(bytes);//
sender.send(msg);
stream.close();


Receiver:

BytesMessage msg = (BytesMessage) message;
int length = msg.readInt();
byte[] bytes = new byte[length];
msg.readBytes(bytes);
FileOutputStream out = new FileOutputStream('New name');
if (out.canWrite()) {
out.write(bytes);
}
out.close();


Powered by Blogger