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>JobDetail class
<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>
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">Note: Use http://www.cronmaker.com/ to generate cron expressions (in this example task scheduled for every minutes)
<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>
Main class
package com.vinod.spring;Output
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");
}
}
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.......
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.......
No comments:
Post a Comment