Transcript
Page 1: Compressing & decompressing in mule

Compressing/Decompressing Mule

By Anirban Sen Chowdhary

Page 2: Compressing & decompressing in mule

We often require to compress our payload in our flow and again decompress it back to it’s original size at the end of the operation

Page 3: Compressing & decompressing in mule

So, How can we compress/decompress payloads in Mule ??

.

Page 4: Compressing & decompressing in mule

Fortunately we have gzip-compress-transformer and gzip-uncompress-transformer available in our Mule

Page 5: Compressing & decompressing in mule

So, to compress a payload in our Mule flow, we can use gzip-compress-transformer as follows :-

As you can see we have used a file inbound to pick a file in our flow, and then we compress it with gzip-compress-transformer and the output of the flow will produce a compressed file

Page 6: Compressing & decompressing in mule

Our corresponding Mule flow will be as follows :- <flow name="GZipCompress" doc:name="GZipCompress">

<file:inbound-endpoint path="E:\backup\test"responseTimeout="10000" doc:name="File"><file:filename-regex-filter pattern="abc.doc"caseSensitive="false" /></file:inbound-endpoint>

<string-to-byte-array-transformerdoc:name="String to Byte Array" /><loggermessage="Payload size before compression : #[Integer.parseInt(payload.size())/1024] KB"level="INFO" doc:name="Logger" />

<!-- If you send gzip a String then it gets serialized and mess ends up in the gzip file. To avoid this convert to byte[] first --><gzip-compress-transformer />

<loggermessage="Payload size after compression : #[Integer.parseInt(payload.size())/1024] KB"level="INFO" doc:name="Logger" />

<file:outbound-endpoint path="E:\backup\test\newfolder"responseTimeout="10000" doc:name="File" /></flow>

Page 7: Compressing & decompressing in mule

As you can see in the code it will pick a file called abc.doc from E:\backup\test location , compress it and put the compressed file to E:\backup\test\newfolder

So, let us place a file abc.doc in the source folder as follows :-

You can see the file size is about 83 KB before compression

Page 8: Compressing & decompressing in mule

Let run our application and we can see, the file has been transferred to location E:\backup\test\newfolder

You can also see in the log that the payload size before compression was 83.0 KB and size after compression is 21.9912 KB

Page 9: Compressing & decompressing in mule

We can see our compressed file here in location E:\backup\test\newfolder :-

Page 10: Compressing & decompressing in mule

Now what about decompressing the file back to the original size ???

Page 11: Compressing & decompressing in mule

In case of decompressing a file back to original size ( Size before the compression) we will be using gzip-uncompress-transformer as follows:-

As you can see, it will pick up the compressed file abc.doc from the location E:\backup\test\newfolder and put it into a new location E:\backup\test\originalFileSize

Page 12: Compressing & decompressing in mule

The corresponding our Mule flow will be :-

<flow name="GZipUnCompress" doc:name="GZipUnCompress"initialState="started"><file:inbound-endpoint path="E:\backup\test\newfolder"responseTimeout="10000" doc:name="File"><file:filename-regex-filter pattern="abc.doc"caseSensitive="false" /></file:inbound-endpoint><gzip-uncompress-transformer />

<byte-array-to-string-transformerdoc:name="Byte Array to String" /><file:outbound-endpoint path="E:\backup\test\originalFileSize"responseTimeout="10000" doc:name="File" /></flow>

Page 13: Compressing & decompressing in mule

Now if we run the flow, we will get the following in our console :-

The file has been decompressed back to it’s original size into the location E:\backup\test\originalFileSize

Page 14: Compressing & decompressing in mule

Hope you enjoyed the tips of compressing and decompressing using gzip-compress-transformer/gzip-uncompress-transformer in Mule

Next slide I will bring some more simple yet interesting topic

Page 15: Compressing & decompressing in mule

Thank You


Recommended