Schedule a REST service In Mule Studio - Using Quartz

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

In this article, we will explore of scheduling a REST based service using the Quartz component.

Introduction

In one of the previous article we have seen as how to schedule a service using mule by writing some content to file after every 10 seconds.In this article, we will explore of scheduling a REST based service using the Quartz component.

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 atleast one Table say "MyJsonStorage" should be created as under

    		-- Table Creation Script
    		CREATE TABLE "MyJsonStorage"
    		(
    		   "StoreJSONObject" json
    		) 
    		
  5. A REST service (I have made it using Dotnet.You are free to make it in your favourite langauge) that will return a json object as under

  6. Everything should be up and running properly

Objective

We will poll a REST service after every 5 seconds.That service will return some JSON object which we will insert to a PostgreSQL database.

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.

Let us then add Quartz Inbound component .Next double click on the Quartz Inbound component to open it's properties tab.In the "General" tab, give a proper "Job Name" (say "RESTServiceJobSchedule") and set the "Repeat Interval" to 5 seconds.

Now we need to add a new job. Click on plus sign.From the "Select element to use" popup that appears, choose the "quartz:event-generator-job" and click "Finish" button.

Now visit the "Advance" tab and let the "ResponseTimeout" be 1000(the default one).So, the screen will be

Click "OK"

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 "tblJSON"("StoreHere") VALUES (CAST(#[message.payload] AS json))"/>
</jdbc:connector>

<flow name="RESTServiceScheduling" doc:name="RESTServiceScheduling">

	 <!-- Step 1: Generates events at a given interval of time -->
	 <quartz:inbound-endpoint jobName="RESTServiceJobSchedule" repeatInterval="5000" doc:name="Quartz" responseTimeout="10000">
		  <quartz:event-generator-job/>
	 </quartz:inbound-endpoint>
	   

	<!-- Step 2: This will read the REST service data -->
	<http:rest-service-component httpMethod="GET" serviceUrl="http://localhost:12186/RestServiceImpl.svc/StorageUsage" />
	
	<!-- 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>

We want to store the whole JSON object into a JSON column type.So first we are converting the Object into String and then we are converting the String to JSON object and inserting into the database.

A question that may arise is then why not directly cast from Object to JSON ?

The reason is that, if we transform from JSON to Object in Mule, then the payload will become a java.util.Map, which will represent the JSON object coming from the remote HTTP endpoint. If we pass this Map payload directly to the JDBC outbound endpoint, JDBC will try to persist it as an serialized object in the database.This is not the stuff that we are looking for.Henceforth, the better approach is to CAST the string to JSON and then store it into the database.

Run the application

So we are almost done.As a last step, let us run the flow and we can see that the operation is happening at a time interval of 5 seconds

If we now look into the database, the result will be as under

References

  1. Quartz Endpoint Reference
  2. Quartz Transport Reference

Conclusion

So this article has taught us as how to schedule REST service using Quartz component.Hope it will be helpful.Thanks for reading.

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)