In this article we will look into as how to invoke a Mule flow from Command Line Interface(CLI) using Linux CURL command.
Introduction
Until now, we have seen many examples of Mule but in all the examples we have executed the Mule flow from the Mule Studio only. But we can invoke a Mule flow from Command Line Interface(CLI) using curl.This article will speak about that.
What we need to have for doing the experiment?
We need
- PostgreSQL
- PostgreSQL JDBC driver
- Download Mule ESB
-
A database to be created in PostgreSQL say "TestDB" where at least one Table say "MyJsonStorage" should be created as under
-- Table Creation Script
CREATE TABLE "MyJsonStorage"
(
"StoreJSONObject" json
)
- A REST service that will return a json object
- Everything should be up and running properly
Let us start the experiment
First create a Mule Project. Then import the Postgre SQL JDBC Driver properly. Next create a Datasource.After that, create the Connector
All these steps are covered in my previous Mule articles and henceforth are not repeated here
The flow

The Configuration File
<jdbc:postgresql-data-source name="PostgreSQL_Data_Source" user="postgres" password="niladri_123" url="jdbc:postgresql://localhost:5432/TestDB" transactionIsolation="UNSPECIFIED" doc:name="PostgreSQL Data Source"/>
<jdbc:connector name="PostgreSQL_JDBC_Connector" dataSource-ref="PostgreSQL_Data_Source" validateConnections="true" queryTimeout="-1" pollingFrequency="0" doc:name="Database">
<jdbc:query key="InsertRecord" value="INSERT INTO "MyJsonStorage"("StoreJSONObject") VALUES(CAST(#[message.payload] AS json))"/> <!-- Cast string to JSON -->
</jdbc:connector>
<flow name="TestStoreJSONObjectFlow1" doc:name="TestStoreJSONObjectFlow1">
<!-- Step 1: This will start the service.This may be replaced with an actual input page like a Html page -->
<http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8082" doc:name="HTTP"/>
<!-- Step 2: This will read the REST service data -->
<http:rest-service-component httpMethod="GET" serviceUrl="http://localhost:12186/RestServiceImpl.svc/EmployeeList" />
<!-- Step 3: Transform the HTTP-streamed payload into a java.lang.String -->
<object-to-string-transformer doc:name="Object to String"/>
<!-- Step 4: Dump into the destination Database -->
<jdbc:outbound-endpoint exchange-pattern="one-way" queryKey="InsertRecord" queryTimeout="-1" connector-ref="PostgreSQL_JDBC_Connector" doc:name="Destination"/>
</flow>
Run the application
We are now almost done. Next we need to run the application. Right click on "TestStoreJSONObject.mflow" and from the context menu, choose Run As->Mule Application.

We will encounter the below screen at some point of time

Because it is looking for the input data. Now press ALT+CTRL+T to open the terminal and type
curl http://localhost:8082
Press "Enter" key.

If we now look into the tblJson, we will find that 1 record has been inserted

References
The Art Of Scripting HTTP Requests Using Curl
Conclusion
Hope you have enjoyed the tutorial. More to come soon.