camel with timer and quartz example

Scheduling Example In camel Using Timer and Quartz With Simple Steps.


In our example we will learn about scheduler in camel.Using Timer and Quartz we can schedule a task that will execute automatically without manual intervention.

  • In eclipse go to MyFirstCamelProject that we created in earlier page and right click on src/main/java to create package.New -> package



  • Give any name for example com.preparationforinterview and click on Finish



  • Right click on package and create a java class for example Scheduler.



  • Right click on package and create one more java class for example Executor.

  • At the end we will get following structure



  • Add following dependency in pom.xml file.you can use any latest version.

  •  	
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    	<modelVersion>4.0.0</modelVersion>
    	<groupId>MyFirstCamelProject</groupId>
    	<artifactId>MyFirstCamelProject</artifactId>
    	<version>0.0.1-SNAPSHOT</version>
    	<dependencies>
    		<dependency>
    			<groupId>org.apache.camel</groupId>
    			<artifactId>camel-core</artifactId>
    			<version>2.13.0</version>
    		</dependency>
    		<dependency>
    			<groupId>org.apache.camel</groupId>
    			<artifactId>camel-stream</artifactId>
    			<version>2.13.0</version>
    		</dependency>
    		<dependency>
    			<groupId>org.slf4j</groupId>
    			<artifactId>slf4j-api</artifactId>
    			<version>1.7.12</version>
    		</dependency>
    		<dependency>
    			<groupId>org.slf4j</groupId>
    			<artifactId>slf4j-log4j12</artifactId>
    			<version>1.7.12</version>
    		</dependency>
    		<dependency>
    			<groupId>org.apache.camel</groupId>
    			<artifactId>camel-quartz</artifactId>
    			<version>2.5.0</version>
    		</dependency>
    	</dependencies>
    </project>
    
  • Run mvn:install from command prompt or if you are using Eclipse right click on pom.xml and run mvn install.It will download camel jars

  • Copy below code in Scheduler.

  •  	
    package com.preparationforinterview;
    
    import org.apache.camel.builder.RouteBuilder;
    
    public class Scheduler extends RouteBuilder {
    
    	@Override
    	public void configure() throws Exception {
    		from("timer://currentTimer?period=1000").setBody()
    				.simple("Current time is ").to("stream:out");
    
    	}
    }
    

    Explain

    To create a route in camel we need to extend RouteBuilder class and override configure method.In configure method we define our route.Timer we get from camel core component and it is only at consumer side.currentTimer is custom name. When event is fired then camel return exchange with empty body, we have set a simple expression of current time that will be displayed on console at every second.

    Note:

  • Copy below code in Executor.

  •  	
    package com.preparationforinterview;
    
    import org.apache.camel.CamelContext;
    import org.apache.camel.impl.DefaultCamelContext;
    
    public class Executor {
    	public static void main(String args[]) throws Exception{
    		
    		CamelContext ctx = new DefaultCamelContext();
    
    		ctx.addRoutes(new Scheduler());
    		ctx.start();
    		Thread.sleep(10000);
    		ctx.stop();
    	}
    }
    
    
    

    Explain

    First we need to create CamelContext. It has many implementation but we are using DefaultCamelContext.After CamelContext is created we add route.Its like registry of route CamelContext, till that part we have just configured route in CamelContext but until and unless we start CamelContext it will not execute route.We kept Thread.sleep(10000) so that camel gets enough time to process route.

    To check the output right click on Executor.java and run it.It will print current time on console
    To run the program from command prompt,open command prompt and go to project location and run below command.
    mvn compile exec:java -Dexec.mainClass=com.preparationforinterview.Executor
  • After execution we get following output on console.



  • To understand quartz example copy below in Scheduler.

  •  	
    package com.preparationforinterview;
    
    import org.apache.camel.builder.RouteBuilder;
    
    public class Scheduler extends RouteBuilder {
    
    	@Override
    	public void configure() throws Exception {
    		/*
    		 * from("timer://currentTimer?period=1000").setBody()
    		 * .simple("Current time is ").to("stream:out");
    		 */
    
    		from("quartz://currentTimer?trigger.repeatInterval=1000&trigger.repeatCount=5")
    				.setBody().simple("Preparaing for interview")
    				.to("stream:out");
    
    	}
    }
    
  • In above example we created simple expression but if we need to create complex expression for example we want to execute a task on every third day of month then in that scenario we write cron expression.For example if we want to print a message on every 20 second then copy below code in Scheduler
  •  	
    package com.preparationforinterview;
    
    import org.apache.camel.builder.RouteBuilder;
    
    public class Scheduler extends RouteBuilder {
    
    	@Override
    	public void configure() throws Exception {
    		/*
    		 * from("timer://currentTimer?period=1000").setBody()
    		 * .simple("Current time is ").to("stream:out");
    		 */
    
    /*		from("quartz://currentTimer?trigger.repeatInterval=1000&trigger.repeatCount=5")
    				.setBody().simple("Preparaing for interview")
    				.to("stream:out");*/
    		
    		from("quartz://firstDayOfTheMonth?cron=0/20+*+*+?+*+*")
    		.setBody().simple("Preparaing for interview")
    		.to("stream:out");
    
    	}
    }
    

Download source code

Camel With Timer And Quartz Example



Visit Others Links

Camel Introduction
Camel Setup
File Copy Example Using Camel
File Copy Example In Camel Using Spring
JMS Message Transfer Example Using Camel
JMS Message Transfer Example In Camel Using Spring
Processor In Camel
Object to JSON Transfer Example Using Spring In Camel
Camel With Bean Injection Example Using Spring DSL
Camel With Bean Injection Registry Example Using Java DSL
Camel Bean Parameter Binding Example Using Java DSL
Camel Error Handling With Redelivery Example Using Java DSL
Camel Exception Handling With Example Using Java DSL
Camel JUnit Test With Example Using Java DSL
Camel JUnit Test With Example Using Spring DSL
Camel Unit Test With Mock Component Example
Camel With Stream Component Example
Camel In-Memory Messaging Example Using Direct
Camel With Timer And Quartz Example
Camel Aggregator EIP Example
Camel Splitter EIP Example
Camel Routing Slip EIP Example
Camel Dynamic Router EIP Example
Camel Load Balancer EIP Example
Top Camel Interview Question
© 2019 PreparationForInterview.com