Searching...
Saturday, 2 January 2016

Spring Quartz Scheduler- Wiring up jobs using triggers and the SchedulerFactoryBean

20:20
Spring provides the feature of scheduling task. In this example we will see how to create a simple helloworld task and schedule it using Spring.

Dependencies

<dependency>
<groupid>org.quartz-scheduler</groupid>
<artifactid>quartz</artifactid>
<version>1.8.5</version>
</dependency>
<!-- QuartzJobBean in spring-context-support.jar -->
<dependency>
<groupid>org.springframework</groupid>
<artifactid>spring-context-support</artifactid>
<version>3.0.4.RELEASE</version>
</dependency>

<dependency>
<groupid>org.springframework</groupid>
<artifactid>spring-tx</artifactid>
<version>3.0.4.RELEASE</version>
</dependency>

<dependency>
<groupid>org.springframework</groupid>
<artifactid>spring-core</artifactid>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupid>org.springframework</groupid>
<artifactid>spring-context</artifactid>
<version>3.2.4.RELEASE</version>
</dependency>
JobDetail class
package com.vinod.spring;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

public class HelloworldJob extends QuartzJobBean {

@Override
protected void executeInternal(JobExecutionContext arg0)
throws JobExecutionException {
System.out.println("Executing helloworld job.......");
}

}


Spring Configuration

<bean class="org.springframework.scheduling.quartz.JobDetailBean" id="helloworldjob">
<property name="jobClass" value="com.vinod.spring.HelloworldJob">
</property></bean>


<bean class="org.springframework.scheduling.quartz.CronTriggerBean" id="helloWorldJobTrigger">
<property name="jobDetail" ref="helloworldjob">
<property name="cronExpression" value="0 0/1 * 1/1 * ? *">
</property></property></bean>


<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean" id="schedulerFactoryBean">
<property name="jobDetails">
<list>
<ref bean="helloworldjob">
</ref></list>
</property>
<property name="triggers">
<list>
<ref bean="helloWorldJobTrigger">
</ref></list>
</property>
</bean>
Note: Use http://www.cronmaker.com/ to generate cron expressions (in this example task scheduled for every minutes)


Main class

package com.vinod.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 *@authorvinod.kumaran
 *
 */
public class HelloWorldJobExecuter {

public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
"spring-job-context.xml");
}

}
Output
Executing helloworld job.......
Executing helloworld job.......
Executing helloworld job.......
Executing helloworld job.......
Executing helloworld job.......
Executing helloworld job.......
Executing helloworld job.......
Executing helloworld job.......
Executing helloworld job.......
Executing helloworld job.......
Executing helloworld job.......
Executing helloworld job.......
Executing helloworld job.......
Executing helloworld job.......
Executing helloworld job.......
Executing helloworld job.......
Executing helloworld job.......
Executing helloworld job.......
Executing helloworld job.......
Executing helloworld job.......
Executing helloworld job.......
Executing helloworld job.......
Executing helloworld job.......
Executing helloworld job.......
Executing helloworld job.......
Executing helloworld job.......
Executing helloworld job.......

 

0 comments: