Archive

Posts Tagged ‘CronTrigger’

Scheduling using Quartz

June 8, 2010 2 comments

Many times we need to perform repetitive jobs in our applications. One common way is to use Thread.sleep method but if you need to synchronize the start of the job based on clock cycles , using a Quartz scheduler is way simpler. Cron triggers can be used to schedule the time that the job starts. We will take a look at how to use Quartz in the non-spring and spring way.

Without Spring…
Get the Quartz 1.8.0 download from here. Add the slf4j jars to the classpath.
My main class looks like this…

public class Main {

 public static void main(String[] args) throws SchedulerException, ParseException {
 Main main = new Main();
 main.scheduleJobs();
 }

 public void scheduleJobs() throws SchedulerException, ParseException {
 SchedulerFactory factory = new StdSchedulerFactory();
 Scheduler sch = factory.getScheduler();
 JobDetail jobDetail = new JobDetail("testJob", null, MyTestJob.class);
 CronTrigger cron = new CronTrigger("testCron", null , "* * * * * ?");
 sch.scheduleJob(jobDetail, cron);
 sch.start();
 }
}

What we have done here is that we have created a scheduler by using SchedulerFactory. JobDetail is the definition of the job that will be run once the trigger fires. “testJob” is the name we give to the job detail, its group is “null” and the class that implements Job is MyTestJob. Similarly, the CronTrigger has the name “testCron”, belongs to group “null” and has a cron expression that fires every second.

The MyTestJob class is as follows…

public class MyTestJob implements Job {

 @Override
 public void execute(JobExecutionContext arg0) throws JobExecutionException {
 System.out.println("In my job.");
 }
}

If we run the program we will see some log messages and then the following output will be printed on the console.. one line every second…
In my job.
In my job.
In my job.
In my job.

Using Spring..
We first create a basic Spring project with Maven… see my older post to get an idea.
The dependencies on my pom.xml file are as follows…

<dependencies>
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-core</artifactId>
 <version>2.5.6</version>
 <scope>compile</scope>
 </dependency>
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-jdbc</artifactId>
 <version>2.5.6</version>
 <scope>compile</scope>
 </dependency>
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-context</artifactId>
 <version>2.5.6</version>
 <scope>compile</scope>
 </dependency>
 <dependency>
 <groupId>junit</groupId>
 <artifactId>junit</artifactId>
 <version>3.8.1</version>
 <scope>test</scope>
 </dependency>
 <dependency>
 <groupId>org.quartz-scheduler</groupId>
 <artifactId>quartz</artifactId>
 <version>1.8.0</version>
 </dependency>
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-context-support</artifactId>
 <version>2.5.6</version>
 <scope>compile</scope>
 </dependency>
 </dependencies>

We will create the beans.xml file in the resources dir. It looks like this….

<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

 <bean name="exampleJob">
 <property name="jobClass" value="com.wordpress.codesilo.ExampleJob" />
 </bean>

 <bean id="cronTrigger">
 <property name="jobDetail" ref="exampleJob" />
 <property name="cronExpression" value="* * * * * ?" />
 </bean>

 <bean id="scheduleFactory">
 <property name="triggers">
 <list>
 <ref bean="cronTrigger" />
 </list>
 </property>
 </bean>
</beans>

The ExampleJob class if the class that will run once the trigger fires. It looks like this..

public class ExampleJob extends QuartzJobBean {
 @Override
 protected void executeInternal(JobExecutionContext arg0)
 throws JobExecutionException {
 System.out.println("In the job");
 }
}

Our main class will have this only..

 public static void main(String[] args) throws SchedulerException {
 ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
 }

If we run main , we will see the following …
log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
In the job
In the job
……..

We will initialize log4j by adding the following log4j.xml file in the resources dir.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
 <appender name="console">
 <param name="Target" value="System.out"/>
 <layout>
 <param name="ConversionPattern" value="%-5p %c{1} - %m%n"/>
 </layout>
 </appender>

 <root>
 <priority value ="debug" />
 <appender-ref ref="console" />
 </root>

</log4j:configuration>

Now, we will see all the debug messages along with out System.out message …”In the job”

In order to inject a bean in the job class we change the exampleJob declaration in beans.xml to the following and declare the “injectedBean” …

<bean name="exampleJob">
 <property name="jobClass" value="com.wordpress.codesilo.ExampleJob" />
 <property name="jobDataAsMap">
 <map>
 <entry key="injectedBean" value-ref="injectedBean" />
 </map>
 </property>
 </bean>

 <bean id="injectedBean"/>

We will have a simple method that displays a message in the InjectedBean class

 public void execute(){
 System.out.println("In injected bean execute method");
 }

and call this method from the ExampleJob class.
Now, if we run main we should see the following output (after removing the debug logs)
In the job
In injected bean execute method
In the job
In injected bean execute method

Stateful Job and Concurrency

In order that the job is not fired concurrently we can have two approaches …

a) Make the job stateful.  All we need here is to implement StatefulJob when creating the ExampleJob class. So, the ExampleJob class declaration now looks like this..

 public class ExampleJob extends QuartzJobBean implements StatefulJob {
 ......
 ......
 }

Read more about it here.

b) Use “MethodInvokingJobDetailFactoryBean” and set the concurrent flag to false. Here is a section from the docs

By default, Quartz Jobs are stateless, resulting in the possibility of jobs interfering with each other. If you specify two triggers for the same JobDetail, it might be possible that before the first job has finished, the second one will start. If JobDetail classes implement the Stateful interface, this won’t happen. The second job will not start before the first one has finished. To make jobs resulting from the MethodInvokingJobDetailFactoryBean non-concurrent, set the concurrent flag to false.

 <bean id="jobDetail">
 <property name="targetObject" ref="exampleBusinessObject" />
 <property name="targetMethod" value="doIt" />
 <property name="concurrent" value="false" />
 </bean>