Trigger a Mule flow from Command Line Interface(CLI) using Linux CURL command

Niladri.Biswas
Posted by in Mule category on for Beginner level | Points: 250 | Views : 2822 red flag

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

  1. PostgreSQL
  2. PostgreSQL JDBC driver
  3. Download Mule ESB
  4. 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
    		) 
    		
  5. A REST service that will return a json object
  6. 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.

Page copy protected against web site content infringement by Copyscape

About the Author

Niladri.Biswas
Full Name: Niladri Biswas
Member Level: Platinum
Member Status: Member
Member Since: 10/25/2010 11:04:24 AM
Country: India
Best Regards, Niladri Biswas
http://www.dotnetfunda.com
Technical Lead at HCL Technologies

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)