W3Schools Learner's Blog

W3Schools Programming knowledge summary website

div

3/16/2018

Spring Quartz Scheduler Example| Simple Trigger and CronTrigger

Spring Quartz Scheduler Example| Simple Trigger and  CronTrigger.

Spring integration and configuration quartz timer task is very simple. This project is based on integration of SSM Maven project. If you are a SSH project, please download spring-context-support-4.1.0.RELEASE.jar and quartz-2.2.1.rar related jar package, and import it into lib directory. 

Quartz task triggers need to use quartz cron expression. Quartz expressions are used to set up when tasks are executed. If you don't know, you can see quartz cron expressions tutorial: http://www.w3school-learn.com/2018/03/cron-expression-generator-and-tutorial.html

The following starts the spring quartz simple Trigger and  CronTrigger integration:

1:Importing jar packages for spring quartz in maven pom.xml file
  1. <!-- quartz timer jar-->
  2. <dependency>
  3. <groupId>org.quartz-scheduler</groupId>
  4. <artifactId>quartz</artifactId>
  5. <version>2.2.1</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.springframework</groupId>
  9. <artifactId>spring-context-support</artifactId>
  10. <version>4.1.4.RELEASE</version>
  11. </dependency>
2:Create MyQuartzTask.java file, simple trigger and cronTrigger.
  1. package com.baidu;
  2. import java.text.SimpleDateFormat;
  3. import java.util.Date;

  4. public class MyQuartzTask {
  5.     //simple trigger task
  6.     public void doSimpleTask(){
  7.         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  8.         System.out.println("doSimpleTask starting..."+sdf.format(new Date()));
  9.     }
  10.     
  11.     //cron trigger task
  12.     public void doCronTask(){
  13.         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  14.         System.out.println("doCronTask starting..."+sdf.format(new Date()));
  15.     }
  16. }

3:Create spring-quartz.xml file, configuring simple trigger and crontrigger 
     
     3.1: Simple Trigger
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  7.         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
  8.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
  9.     
  10.      <!--Injecting task bean  -->   
  11.      <bean id="quartzTask" class="com.baidu.MyQuartzTask"></bean> 
  12.      
  13.      <!-- 1.Simple Trigger task detail information-->   
  14. <bean id="myJobDetail1" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
  15.       <!-- Setting up task execution object -->
  16.       <property name="targetObject" ref="quartzTask"></property>
  17.       <property name="targetMethod" value="doSimpleTask"></property>
  18.       <property name="concurrent" value="false"></property>
  19.      </bean>
  20.      
  21.      <!-- 2.making simple trigger  -->  
  22.      <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
  23.       <property name="jobDetail" ref="myJobDetail1"></property>
  24.       <property name="startDelay" value="1000"></property>
  25.       <property name="repeatInterval" value="2000"></property>
  26.      </bean> 
  27.      
  28.      <!-- Configuration simple trigger scheduling factory -->   
  29.      <bean id="schedulerFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
  30.       <property name="triggers">
  31.       <list>
  32.       <!-- Schedule task -->
  33.       <ref bean="simpleTrigger"/>
  34.       <!--<ref bean=""> more trigger......-->
  35.       </list>
  36.       </property>
  37.      </bean> 
  38. </beans>
     3.2: Cron Trigger
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  7.         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
  8.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
  9.     
  10.      <!--Injecting task bean  -->   
  11.      <bean id="quartzTask" class="com.baidu.MyQuartzTask"></bean> 
  12.      
  13.      <!-- 2.cron trigger task detail information -->
  14. <bean id="myJobDetail2" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
  15.       <!-- Setting up task execution object -->
  16.       <property name="targetObject" ref="quartzTask"></property>
  17.       <property name="targetMethod" value="doCronTask"></property>
  18.       <property name="concurrent" value="false"></property>
  19.      </bean>
  20.      
  21.      <!-- 3.cron trigger -->
  22.      <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
  23.       <property name="jobDetail" ref="myJobDetail2"></property>
  24.       <!-- Setting up quartz cron expression ,fire every three seconds -->
  25.       <property name="cronExpression" value="0/3 * * * * ?"></property>
  26.      </bean>
  27.      
  28.      <!-- Configuration cron trigger scheduling factory-->   
  29. <bean id="schedulerFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
  30.       <property name="triggers">
  31.       <list>
  32.       <ref bean="cronTrigger"/>
  33. <!--<rel bean=""> more cron trigger......-->
  34.       </list>
  35.       </property>
  36.      </bean> 
  37. </beans>
4:Scanning the spring-quartz.xml file in web.xml 
  1. <!-- start spring -->
  2. <context-param>
  3. <param-name>contextConfigLocation</param-name>
  4. <param-value>classpath:spring/spring*.xml</param-value>
  5. </context-param>

  6. <listener>
  7. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  8. </listener>

No comments:

Post a Comment

Note: only a member of this blog may post a comment.