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:
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 5:Â
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 6:
When you run above program, you will get following output.
There are two ways by which you can specify quartz jobs.
MethodInvokingJobDetailFactoryBeanJobDetailFactoryBean
MethodInvokingJobDetailFactoryBean is used for simpler task. It specifies target object and target method, so target object’s method will be executed at the time specified using trigger.
Project structure:
Steps for creating Spring Quartz Scheduler Example using MethodInvokingJobDetailFactoryBean are:
Step 1:
Create simple java maven project with Name “SpringQuartzSchedulerExample”.
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()); } } |
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 MethodInvokingJobDetailFactoryBean which specify class and method which will get executed.
- Need to put entry for triggers which specify time interval and start delay time.
- Need to specify ScheduleFactoryBean to bind triggers and MethodInvokingJobDetailFactoryBean 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 |
<?xml version="1.0" encoding="UTF-8"?> <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" /> <!-- specifing class and method that is going to be called on a specified time basis --> <bean id="emailJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="sendEmailTask" /> <property name="targetMethod" value="sendEmail" /> </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 put,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 17, 2016 3:15:52 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1441e2aa: startup date [Sun Jan 17 15:15:52 IST 2016]; root of context hierarchy Jan 17, 2016 3:15:53 PM 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 17, 2016 3:15:53 PM org.springframework.context.support.DefaultLifecycleProcessor start INFO: Starting beans in phase 2147483647 Jan 17, 2016 3:15:53 PM org.springframework.scheduling.quartz.SchedulerFactoryBean startScheduler INFO: Starting Quartz Scheduler now Sending email at Sun Jan 17 15:15:54 IST 2016 Sending email at Sun Jan 17 15:16:04 IST 2016 Sending email at Sun Jan 17 15:16:14 IST 2016 Sending email at Sun Jan 17 15:16:24 IST 2016 Sending email at Sun Jan 17 15:16:34 IST 2016 |
Download source code:
We are done with Spring Quartz Scheduler Example using MethodInvokingJobDetailFactoryBean.
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.