The following code shows how you can create a compressed
BytesMessage
and publish it onto a topic:
InputStream in = null; GZIPOutputStream out = null; try { ByteArrayOutputStream bos = new ByteArrayOutputStream(1024 * 64); out = new GZIPOutputStream(bos); String filename = "input.xml"; in = new BufferedInputStream(new FileInputStream(filename)); byte[] buf = new byte[1024 * 4]; int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } out.finish(); //publish it BytesMessage msg = session.createBytesMessage(); msg.writeBytes(bos.toByteArray()); publisher.publish(msg); } catch (IOException e) { e.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (IOException ignore) { } } if (out != null) { try { out.close(); } catch (IOException ignore) { } } }Decompressing JMS messages
The following code shows how you can decompress a JMS
BytesMessage
when your subscriber receives it and write it to file:
if (mesg instanceof BytesMessage) { final BytesMessage bMesg = (BytesMessage) mesg; byte[] sourceBytes; try { sourceBytes = new byte[(int) bMesg.getBodyLength()]; bMesg.readBytes(sourceBytes); System.out.println("Read " + sourceBytes.length + " bytes"); } catch (JMSException e1) { throw new RuntimeException(e1); } GZIPInputStream in = null; OutputStream out = null; try { in = new GZIPInputStream( new ByteArrayInputStream(sourceBytes)); String filename = "message.xml"; out = new FileOutputStream(filename); byte[] buf = new byte[1024 * 4]; int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } System.out.println("Wrote to " + filename); } catch (IOException e) { e.printStackTrace(); } finally { if (in != null) try { in.close(); } catch (IOException ignore) { } if (out != null) try { out.close(); } catch (IOException ignore) { } } }
Hi, we've used similar solution for some time. But if you use TIBCO EMS there is automatic compression/decompression by TIBCO libraries on the client side. Just set this porperty on the outgoing message:
ReplyDeletemsg.setBooleanProperty("JMS_TIBCO_COMPRESS",true);
hi ,
Deletecan u please tell me where we will set this property and give me a small example for this.
Thanks
Sumalatha
Hi,
ReplyDeleteI using Deflater for both compression and decompression. When i am trying to receive the message iam getting an exception
[JMSClientExceptions:055128]Read attempted in WRITE mode.
Thanks,
ReplyDeleteThe above was solved by calling msg.reset() after the call to msg.writeBytes(bos.toByteArray());