On Table Row in Database Module.
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 order_ID, customer_ID, quantity, product, contact_number, shipping_address, mail_ID.
- We need to have some sample set of records in the database table, as shown in the table.
On Table Row
- On Table Row is a database component which enables Mule applications to listen to a database table and trigger the flow when new or modified records are detected.
- It eliminates the need for manual intervention or scheduled queries, making the integration more efficient.
- Working of On Table Row:
- Polling Mechanism – The connector periodically checks the configured database table for new or modified records based on a defined column, typically a timestamp or an auto-increment ID.
- Row Selection – The connector fetches rows that meet the selection criteria, such as orderID > last processed orderID.
- It returns one record at a time.
- Advantages of On Table Row:
- Ensures no duplicate processing with watermarking.
- Scalable for high-volume order handling.
- Let’s understand this component with a requirement. We need to send an email notification to every customer, only for the new orders created in the order_table, that their order has been shipped, and it should be scheduled once every hour.
Steps:
- To begin, a new Mule project needs to be created. Then, drag and drop an on table row component into the project from the Database module.
- Then, configure the database connector configuration, and here I’m utilizing the secure properties to encrypt the connection details.
2.Next, we will configure the on table row component.
Table: We need to provide the name of the table, with which we are dealing.
Watermark column: We need to provide the column which we are using for watermarking.
ID column: We need to provide the column, which will be considered as row ID.
Scheduling Strategy: We need to schedule it, based on the requirement. For this instance, it will be once every hour.
3.Next, add the start logger and end logger, which determines the start and end of flow. By configuring the logger with “Start of flow ” ++ (flow.name as String)
4.Then, we’ll add a Transform Message to create a message for the customer.
%dw 2.0
output application/json
—
{
Message: “Your order ” ++ payload.order_ID as String default “” ++ ” having the product ” ++
payload.product as String default “” ++ ” with quantity ” ++ payload.quantity as String default “” ++ “has been shipped to the address” ++ payload.shipping_address as String default “” ,
EmailID: payload.mail_ID as String default “”
}
5.After adding the Transform Message component, we’ll place a Parse Template component to send the email in a specific format. To configure the Parse Template, we have specified the location of the HTML file, as it is already placed under the src/main/resources directory, and we are storing the response in the target variable.
mailContent.html file
6.Next, we’ll add an email connector to notify the customer that their order has been shipped and configure it. We’ll set details such as fromAddress, toAddress, subject, content, contentType, and encoding. The fromAddress is retrieved from the configuration file.
7.As shown here, we need to add the following TLS configuration in the Advanced section if using a Gmail account.
Key Value
mail.smtp.starttls.enable true
mail.smtp.ssl.trust smtp.gmail.com
8.The setup is complete. We’ll deploy the application, by passing the environment variables and the key for the secure properties.
9.Lastly, we’ll verify with the email.