<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Scheduler &#8211; CodePills.com</title>
	<atom:link href="https://codepills.com/tag/scheduler/feed/" rel="self" type="application/rss+xml" />
	<link>https://codepills.com</link>
	<description>Helping you make a better code</description>
	<lastBuildDate>Sat, 23 Mar 2024 08:38:54 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	
	<item>
		<title>How to trigger action with Scheduler and Quartz in Camel</title>
		<link>https://codepills.com/how-to-trigger-action-with-scheduler-and-quartz-in-camel/</link>
					<comments>https://codepills.com/how-to-trigger-action-with-scheduler-and-quartz-in-camel/#respond</comments>
		
		<dc:creator><![CDATA[Andrej Buday]]></dc:creator>
		<pubDate>Wed, 06 Jan 2021 16:47:38 +0000</pubDate>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Camel]]></category>
		<category><![CDATA[Component]]></category>
		<category><![CDATA[CRON]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[messaging]]></category>
		<category><![CDATA[Quartz]]></category>
		<category><![CDATA[Quartz2]]></category>
		<category><![CDATA[Scheduler]]></category>
		<category><![CDATA[Spring]]></category>
		<guid isPermaLink="false">https://codepills.com/?p=1118</guid>

					<description><![CDATA[This article is about triggering action with Scheduler and Quartz components in Camel <a href="https://codepills.com/how-to-trigger-action-with-scheduler-and-quartz-in-camel/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
										<content:encoded><![CDATA[<p>In any project using Camel you might come to the stage you want to trigger action periodically.</p>
<p>We have recently looked on how to <a href="https://codepills.com/how-to-download-a-file-from-azure-claud-storage/" title="How to download a file from Azure Claud Storage">download file from Azure Cloud Storage</a> 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?</p>
<p>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&#8217;s Scheduler and Camel&#8217;s Quartz2 components, mostly for your legacy Camel projects.</p>
<p><span id="more-1118"></span></p>
<p>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.</p>
<h2>Using Camel Scheduler component as trigger</h2>
<p>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.</p>
<p>In the extended class <i>ScheduleFromAzureCloudStorageToDisk</i> we will override <i>configure()</i> method. <i>configure()</i> method is actually used for Camel&#8217;s messaging system. We start with Camel&#8217;s <i>from</i> method for building routes and we will place our trigger there. We will use the <i>Scheduler</i> component with combination of <i>delay</i> 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.</p>
<p>The second, thing to do is to set route id for Camel context. Any unique name for route name will do it.</p>
<p>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 <a href="https://codepills.com/how-to-download-a-file-from-azure-claud-storage/" title="How to download a file from Azure Claud Storage">How to download a file from Azure Claud Storage</a>.</p>
<pre><code>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)
    {
        ...
    }

}
</code></pre>
<h2>Using Camel Quartz2 component as trigger</h2>
<p>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.</p>
<p>Instead, it has Quartz and Quartz2 components that provide a scheduled delivery of messages using the Quartz Scheduler version 2.x.</p>
<p>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.</p>
<pre><code>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)
    {
        ...
    }

}
</code></pre>
<p>Let&#8217;s explain code above. We have created Spring component <i>ScheduleFromAzureCloudStorageToDisk</i> extending <i>RouteBuilder</i> class. We will implement trigger in <i>configure()</i> method. We will place Quartz2 component into the <i>from</i> method, which will build a route for Camel. Route in <i>from</i> method will have form as quartz2 component, then <i>import</i> trigger, then unique id name of import trigger (in our case <i>azure-file</i>) and lastly define time trigger in CRON format after <i>cron</i> parameter for import Quartz2 trigger.</p>
<p>Again, regarding <i>downloadFromAzureCloudStorageToDisk(final Exchange exchange)</i> method, you can find its implementation in article <a href="https://codepills.com/how-to-download-a-file-from-azure-claud-storage/" title="How to download a file from Azure Claud Storage">How to download a file from Azure Claud Storage</a> .</p>
<p>That is all. All magic regarding triggering on specific time happens in CRON setting.</p>
<h3>How to set up CRON </h3>
<p>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</p>
<p>Examples:</p>
<ul>
<li><code>0/30+*+*+*+*+?</code> Every 30 seconds</li>
<li><code>0+0+6+*+*+?</code> Every day at 6:00 AM</li>
<li><code>0+*+0/6+*+*+?</code> Every 6 hours</li>
</ul>
<p>If you would like to know how to write a CRON job, check the article <a href="https://codepills.com/simple-guide-write-cron-jobs/" title="How to write CRON jobs">How to write CRON jobs</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://codepills.com/how-to-trigger-action-with-scheduler-and-quartz-in-camel/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
