MuleSoft
Compression Module in Mulesoft

Compression Module in MuleSoft.

Pre-requisites

  • We need to have an Anypoint Studio should be setup.
  • We need to an order_table in database. For this instance, we are using the MySQL database.
  • Create a table with the fields as ID, name, email, contact, status, and payment.
  • We need to have some sample set of records in the database table, as shown in the table.

Sample set of records

Compression Module

  • The MuleSoft Compression Module provides out-of-the-box capabilities to compress and decompress files within your Mule applications.
  • The specific use of this module is when integrating with systems that require or return large files, helping to reduce file size, optimize bandwidth, and simplify archiving.
  • The compression module provides 4 different operations:
    • Compress:
      • This operation is used to compress a single file into the Zip or Gzip format.
    • Decompress:
      • This operation is used to decompress a single file from a specified format.
    • Archive:
      • This operation is used to compress multiple entries or files into a new archive file.
    • Extract:
      • This operation is used to decompress the archived content.
  • Let’s understand this component with a requirement. We need to design a scheduled process to synchronize Customer data from a MySQL database to the local file system. This process will exclusively retrieve Customer records from the MySQL DB based on the status “Yes”, consolidate them into a single file, compress the file into a ZIP archive send it over mail, and after that update the status “Completed”. The automation is scheduled to run daily at 11:00 AM IST, ensuring timely and consistent data synchronization.

Steps:

  1. To begin, a new Mule project needs to be created. Then, drag and drop the Scheduler and configure the cron expression as “0 0 11 * * *” and the time zone with “Asia/Calcutta”.

Drag and drop Scheduler

  1. Next, add the start logger and end logger, which determine the start and end of the flow. By configuring the logger with “Start of flow ” ++ (flow.name as String)

Start logger and end logger

  1. Next, we’ll add a Database Select operation to our flow and configure it to query the required data. To ensure security, we’ll reference the database connection details using secure property placeholders, which allows us to keep sensitive credentials encrypted and externalized from the main codebase. Database Select operation

Secure Properties Config

  1. Next, we’ll incorporate a database query to retrieve records where the status flag is set to “Yes.” To enhance flexibility and maintainability, we’ll use configuration properties to dynamically fetch the value of the status field, making the solution easily adaptable across environments. Customer Table

Configuration Properties

  1. Then, we’ll drag and drop a transform message component to transform the payload into JSON format and create a variable “consolidated record”, having value as an empty array.

Consolidated Records

Consolidated Records

  1. Next, we’ll make use of a choice router to process the records only if the payment is completed and wrap in for-each to iterate over the records.

Choice Router

  1. Next, we’ll consolidate the records and store them in “consolidated records” variable.

Consolidate Records Variable

  1. Next, we’ll add a choice router to check for the variable consolidated records is not empty.

Variable Consolidated Records

  1. Then, we’ll add the compress component which will compress the records.

Compress Component

  1. Next, we’ll use the Mail Connector’s Send component to email the consolidated records as a zipped attachment.

SMTP Protocol

Mule Debugger

  1. Then, we’ll update the Status as ‘Completed’ using a bulk update. To do so, we’ll configure the query with the following syntax: Update Customer set Status=: Status where ID=:ID and use the input parameters from the variable consolidated records.

Update Status

  1. The setup is ready to be deployed.

Setup Ready to be Deployed

  1. Lastly, we’ll verify the mail received and DB, with the updated value for the records.

Mulesoft customer

Ready to simplify your data workflows with smart automation? Connect with TGH’s MuleSoft experts to compress, archive, and email your database records effortlessly. Get started today!

Contact TGH

Author

Aniket Pal

Leave a comment

Your email address will not be published. Required fields are marked *