Table of Contents
In this post, we will see how to schedule jobs using Spring Quartz scheduler or how to integrate spring with Quartz. We have already seen how to schedule jobs using Timer and TimerTask .
Step 4:
Create a class named “JobScheduler.java” in src/main/java in package com.arpit.java2blog.spring.. This class will extend QuartzJobBean and executeInternal method will get called at particular time specified in trigger.
Step 5:
In this step, you need to create “quartz-config.xml” in src/main/resources.
quartz-config.xml will have all configuration required to schedule a task . You need to put four entries here
Step 6:Â
Finally we will create main class to execute this. We will create object of ClassPathXmlApplicationContext to load quartz-config.xml create class name Application.java in src/main/java in package com.arpit.java2blog.spring
Step 7:
When you run above program, you will get following output.
There are two ways by which you can specify quartz jobs.
MethodInvokingJobDetailFactoryBeanJobDetailFactoryBean
JobDetailFactoryBean is used for complex task. You need to create job class which extends QuartzJobBean.
Project structure:
Steps for creating Spring Quartz Scheduler Example using JobDetailFactoryBean are:
Step 1:
Create simple java maven project with Name “SpringQuartzSchedulerExampleUsingQuartzJobBean”.
Step 2:
Update pom.xml to put spring core and spring quartz dependencies.
pom.xml
Update pom.xml to put spring core and spring quartz dependencies.
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
<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>com.arpit.java2blog</groupId> <artifactId>SpringQuartzSchedulerExample</artifactId> <version>0.0.1-SNAPSHOT</version> <name>SpringQuartzSchedulerExample</name> <description>It provide spring quartz scheduler example using MethodInvokingJobDetailFactoryBean</description> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <!-- Quartz framework dependencies --> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.0</version> </dependency> </dependencies> <build> <finalName>SpringQuartzSchedulerExample</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>${jdk.version}</source> <target>${jdk.version}</target> </configuration> </plugin> </plugins> </build> <properties> <spring.version>4.2.1.RELEASE</spring.version> <jdk.version>1.7</jdk.version> </properties> </project> |
Step 3:
Create  a class called “SendEmailTask.java” in src/main/java in package com.arpit.java2blog.spring.
It is class whose code you want to execute at specific time or time interval repeatedly.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package com.arpit.java2blog.spring; import java.util.Calendar; public class SendEmailTask { public void sendEmail() { // You may want to put some condition for sending email // Actual email send logic System.out.println("Sending email at "+ Calendar.getInstance().getTime()); } } |
Create a class named “JobScheduler.java” in src/main/java in package com.arpit.java2blog.spring.. This class will extend QuartzJobBean and executeInternal method will get called at particular time specified in trigger.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package com.arpit.java2blog.spring; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.springframework.scheduling.quartz.QuartzJobBean; public class JobScheduler extends QuartzJobBean { SendEmailTask sendEmailTask; public void setSendEmailTask(SendEmailTask sendEmailTask) { this.sendEmailTask = sendEmailTask; } @Override protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException { sendEmailTask.sendEmail(); } } |
Step 5:
In this step, you need to create “quartz-config.xml” in src/main/resources.
quartz-config.xml will have all configuration required to schedule a task . You need to put four entries here
- Need to specify task (SendEmailTask).
- Need to specify JobDetailFactoryBean
- Need to put entry for triggers which specify time interval and start delay time.
- Need to specify ScheduleFactoryBean to bind triggers and JobDetailFactoryBean together.
 quartz-config.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!--Specify Task bean --> <bean id="sendEmailTask" class="com.arpit.java2blog.spring.SendEmailTask" /> <!-- Spring Quartz --> <bean name="emailJob" class="org.springframework.scheduling.quartz.JobDetailFactoryBean"> <property name="jobClass" value="com.arpit.java2blog.spring.JobScheduler" /> <property name="jobDataAsMap"> <map> <entry key="sendEmailTask" value-ref="sendEmailTask" /> </map> </property> <property name="durability" value="true" /> </bean> <!-- simple trigger specify repeat interval and delay time --> <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean"> <property name="jobDetail" ref="emailJob" /> <property name="repeatInterval" value="10000" /> <property name="startDelay" value="1000" /> </bean> <!-- scheduler factory bean to bind,the executing code and time intervals together --> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="jobDetails"> <list> <ref bean="emailJob" /> </list> </property> <property name="triggers"> <list> <ref bean="simpleTrigger" /> </list> </property> </bean> </beans> |
Finally we will create main class to execute this. We will create object of ClassPathXmlApplicationContext to load quartz-config.xml create class name Application.java in src/main/java in package com.arpit.java2blog.spring
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package com.arpit.java2blog.spring; /** * @author Arpit Mandliya * */ import org.springframework.context.support.ClassPathXmlApplicationContext; public class Application { public static void main(String[] args) throws Exception { new ClassPathXmlApplicationContext("quartz-config.xml"); } } |
When you run above program, you will get following output.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
Jan 24, 2016 1:09:13 AM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1441e2aa: startup date [Sun Jan 24 01:09:13 IST 2016]; root of context hierarchy Jan 24, 2016 1:09:13 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [quartz-config.xml] SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details. Jan 24, 2016 1:09:14 AM org.springframework.context.support.DefaultLifecycleProcessor start INFO: Starting beans in phase 2147483647 Jan 24, 2016 1:09:14 AM org.springframework.scheduling.quartz.SchedulerFactoryBean startScheduler INFO: Starting Quartz Scheduler now Sending email at Sun Jan 24 01:09:15 IST 2016 Sending email at Sun Jan 24 01:09:25 IST 2016 Sending email at Sun Jan 24 01:09:35 IST 2016 Sending email at Sun Jan 24 01:09:45 IST 2016 Sending email at Sun Jan 24 01:09:55 IST 2016 |
Download source code:
We are done with Spring Quartz Scheduler Example using JobDetailFactoryBean.
Was this post helpful?
Let us know if this post was helpful. Feedbacks are monitored on daily basis. Please do provide feedback as that\'s the only way to improve.