To integrate Spring with Quartz scheduler framework. Spring comes with
many handy classes to support Quartz, and decouple your class to Quartz
APIs
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mahesh.common</groupId>
<artifactId>SpringExample</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>SpringExample</name>
<url>http://maven.apache.org</url>
<dependencies>
<!-- Spring 3 dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.1.2.RELEASE</version>
</dependency>
<!-- QuartzJobBean in spring-context-support.jar -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>3.1.2.RELEASE</version>
</dependency>
<!-- Spring + Quartz need transaction -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>3.1.2.RELEASE</version>
</dependency>
<!-- Quartz framework -->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>1.8.6</version>
</dependency>
</dependencies>
<build>
<finalName>SpringExample</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
package com.mahesh.common;
import java.util.Date;
import org.quartz.utils.Key;
public class RunMeTask {
String name;
public void printMe(Key str) {
Date d = new Date();
System.out.println(str+" Spring 3 + Quartz 1.8.6 ~ "+d);
}
public void printName() {
Date d = new Date();
System.out.println("Developed By Mahesh");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.mahesh.common;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
public class RunMeJob extends QuartzJobBean {
private RunMeTask runMeTask;
public void setRunMeTask(RunMeTask runMeTask) {
this.runMeTask = runMeTask;
}
protected void executeInternal(JobExecutionContext context)
throws JobExecutionException {
runMeTask.printMe(context.getJobDetail().getKey());
//runMeTask.printName();
}
}
<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-3.0.xsd">
<bean id="runMeTask" class="com.mahesh.common.RunMeTask" />
<!-- Spring Quartz -->
<bean name="runMeJob" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="com.mahesh.common.RunMeJob" />
<property name="group" value="mahesh" />
<property name="name" value="maheshJob" />
<property name="jobDataAsMap">
<map>
<entry key="runMeTask" value-ref="runMeTask" />
</map>
</property>
</bean>
<!-- Cron Trigger, run every 5 seconds -->
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="runMeJob" />
<property name="cronExpression" value="0/15 * * * * ?" />
</bean>
<!-- you can configure a SchedulerFactoryBean bean to start your scheduler task. -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<!-- <property name="jobDetails">
<list>
<ref bean="runMeJob" />
</list>
</property> -->
<property name="triggers">
<list>
<ref bean="cronTrigger" />
</list>
</property>
</bean>
</beans>
package com.mahesh.common;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AppQuartz {
public static void main(String[] args) throws Exception {
new ClassPathXmlApplicationContext("Spring-Quartz.xml");
}
}
mahesh.maheshJob Spring 3 + Quartz 1.8.6 ~ Tue Jan 14 16:52:00 IST 2014
mahesh.maheshJob Spring 3 + Quartz 1.8.6 ~ Tue Jan 14 16:52:15 IST 2014
mahesh.maheshJob Spring 3 + Quartz 1.8.6 ~ Tue Jan 14 16:52:30 IST 2014
Following is the content for pom.xml includes dependencies to integrate Spring with Quartz
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mahesh.common</groupId>
<artifactId>SpringExample</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>SpringExample</name>
<url>http://maven.apache.org</url>
<dependencies>
<!-- Spring 3 dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.1.2.RELEASE</version>
</dependency>
<!-- QuartzJobBean in spring-context-support.jar -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>3.1.2.RELEASE</version>
</dependency>
<!-- Spring + Quartz need transaction -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>3.1.2.RELEASE</version>
</dependency>
<!-- Quartz framework -->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>1.8.6</version>
</dependency>
</dependencies>
<build>
<finalName>SpringExample</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Following is the content for RunMeTask.java this is the class you want to schedule in Quartz.
package com.mahesh.common;
import java.util.Date;
import org.quartz.utils.Key;
public class RunMeTask {
String name;
public void printMe(Key str) {
Date d = new Date();
System.out.println(str+" Spring 3 + Quartz 1.8.6 ~ "+d);
}
public void printName() {
Date d = new Date();
System.out.println("Developed By Mahesh");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Following is the content for RunMeJob.java
package com.mahesh.common;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
public class RunMeJob extends QuartzJobBean {
private RunMeTask runMeTask;
public void setRunMeTask(RunMeTask runMeTask) {
this.runMeTask = runMeTask;
}
protected void executeInternal(JobExecutionContext context)
throws JobExecutionException {
runMeTask.printMe(context.getJobDetail().getKey());
//runMeTask.printName();
}
}
Following is the content for Spring configuration file Spring-Quartz.xml
<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-3.0.xsd">
<bean id="runMeTask" class="com.mahesh.common.RunMeTask" />
<!-- Spring Quartz -->
<bean name="runMeJob" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="com.mahesh.common.RunMeJob" />
<property name="group" value="mahesh" />
<property name="name" value="maheshJob" />
<property name="jobDataAsMap">
<map>
<entry key="runMeTask" value-ref="runMeTask" />
</map>
</property>
</bean>
<!-- Cron Trigger, run every 5 seconds -->
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="runMeJob" />
<property name="cronExpression" value="0/15 * * * * ?" />
</bean>
<!-- you can configure a SchedulerFactoryBean bean to start your scheduler task. -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<!-- <property name="jobDetails">
<list>
<ref bean="runMeJob" />
</list>
</property> -->
<property name="triggers">
<list>
<ref bean="cronTrigger" />
</list>
</property>
</bean>
</beans>
Following is the content to test the application AppQuartz.java
package com.mahesh.common;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AppQuartz {
public static void main(String[] args) throws Exception {
new ClassPathXmlApplicationContext("Spring-Quartz.xml");
}
}
OutPut:
mahesh.maheshJob Spring 3 + Quartz 1.8.6 ~ Tue Jan 14 16:51:45 IST 2014mahesh.maheshJob Spring 3 + Quartz 1.8.6 ~ Tue Jan 14 16:52:00 IST 2014
mahesh.maheshJob Spring 3 + Quartz 1.8.6 ~ Tue Jan 14 16:52:15 IST 2014
mahesh.maheshJob Spring 3 + Quartz 1.8.6 ~ Tue Jan 14 16:52:30 IST 2014
No comments:
Post a Comment