How to trigger action with Scheduler and Quartz in Camel

In any project using Camel you might come to the stage you want to trigger action periodically.

We have recently looked on how to download file from Azure Cloud Storage in legacy application. With the help of Camel and in Java project, all was written in a couple of minutes. But what if you have a special case and your file is changing periodically. For example, every day or every minute?

This article will show you a couple of code examples on how to initiate an action when periodic change is required. This article will also show you how to start the action with Camel’s Scheduler and Camel’s Quartz2 components, mostly for your legacy Camel projects.

We will extend code from an article about downloading a file from Azure Cloud Storage in our hypothetical scenario. We will create Spring Component in our new application for Camel messaging. It will download a certain file from Azure Cloud Storage periodically.

Using Camel Scheduler component as trigger

First of all, we need to extend RouteBuilder. The RouteBuilder is a base class which is derived from to create routing rules using the DSL. Instances of RouteBuilder are then added to the CamelContext.

In the extended class ScheduleFromAzureCloudStorageToDisk we will override configure() method. configure() method is actually used for Camel’s messaging system. We start with Camel’s from method for building routes and we will place our trigger there. We will use the Scheduler component with combination of delay method option for the file download in this example. This delay will trigger periodical lookup into the Azure Cloud Storage. Delay needs to be set up in milliseconds.

The second, thing to do is to set route id for Camel context. Any unique name for route name will do it.

Finally, all we need to do is create a method that will look into the Azure Cloud Storage and save the new document to local/external disk/source. To get a better idea how this method might look in the legacy application, check the article How to download a file from Azure Claud Storage.

package com.codepills.routes;

import org.apache.camel.Exchange;
import org.apache.camel.builder.RouteBuilder;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

@Component
public class ScheduleFromAzureCloudStorageToDisk extends RouteBuilder {

    protected Logger logger = Logger.getLogger(getClass());

    @Override
    public void configure() throws Exception
    {
        logger.info("Start setting up FromAzureCloudStorageToDisk");

        from("scheduler://fileInAzureBlobStorage?delay=30000")  // Trigger action every 30 seconds, number is in milliseconds
            .routeId("FromAzureStorageToDiskRoute")
            .process(this::downloadFromAzureCloudStorageToDisk); // .process((exchange) -> downloadFromAzureCloudStorageToDisk(exchange))

        logger.info("Finished setting up FromAzureCloudStorageToDisk");
    }

    private void downloadFromAzureCloudStorageToDisk(Exchange exchange)
    {
        ...
    }

}

Using Camel Quartz2 component as trigger

If you are working with a legacy application, you might use older versions of Camel. In older version of Camel, Camel does not have support for pure CRON component.

Instead, it has Quartz and Quartz2 components that provide a scheduled delivery of messages using the Quartz Scheduler version 2.x.

Difference between Quartz and Quartz2 is in its API on lower levels and availability in Camel version. Noticeable distinguish in the fact that Quartz2 offers a few more options. So selection and usage of correct Quartz component I will let decision on you.

package com.codepills.routes;

import org.apache.camel.Exchange;
import org.apache.camel.builder.RouteBuilder;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

@Component
public class ScheduleFromAzureCloudStorageToDisk extends RouteBuilder {

    protected Logger logger = Logger.getLogger(getClass());

    @Override
    public void configure() throws Exception
    {
        logger.info("Start setting up FromAzureCloudStorageToDisk");

        from("quartz2://import/azure-file?cron=0/30+*+*+*+*+?") // CRON format for every 30 seconds
            .routeId("FromAzureStorageToDiskRoute")
            .process(this::downloadFromAzureCloudStorageToDisk); // .process((exchange) -> downloadFromAzureCloudStorageToDisk(exchange))

        logger.info("Finished setting up FromAzureCloudStorageToDisk");
    }

    private void downloadFromAzureCloudStorageToDisk(Exchange exchange)
    {
        ...
    }

}

Let’s explain code above. We have created Spring component ScheduleFromAzureCloudStorageToDisk extending RouteBuilder class. We will implement trigger in configure() method. We will place Quartz2 component into the from method, which will build a route for Camel. Route in from method will have form as quartz2 component, then import trigger, then unique id name of import trigger (in our case azure-file) and lastly define time trigger in CRON format after cron parameter for import Quartz2 trigger.

Again, regarding downloadFromAzureCloudStorageToDisk(final Exchange exchange) method, you can find its implementation in article How to download a file from Azure Claud Storage .

That is all. All magic regarding triggering on specific time happens in CRON setting.

How to set up CRON

Since I know how hard and frustrating it is sometimes to set correct CRON format, here are a couple of examples to distinguish for you the order od time elements in CRON used in Quartz

Examples:

  • 0/30+*+*+*+*+? Every 30 seconds
  • 0+0+6+*+*+? Every day at 6:00 AM
  • 0+*+0/6+*+*+? Every 6 hours

If you would like to know how to write a CRON job, check the article How to write CRON jobs.

This entry was posted in Tutorials and tagged , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.