Quartz is an open source job scheduling framework, which is completely written in Java and designed for use in J2SE and J2EE applications. It provides great flexibility without sacrificing simplicity. You can use it to create simple or complex schedules for executing a job. It has many features, such as database support, clusters, plug-ins, EJB job pre construction, JavaMail and others, support for cron like expressions, and so on.

1. Import Dependencies

 <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>5.0.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>5.0.2.RELEASE</version> </dependency> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.1</version> </dependency> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz-jobs</artifactId> <version>2.2.1</version> </dependency> </dependencies>

2. Write configuration file

 <? 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 " xmlns:dubbo=" http://code.alibabatech.com/schema/dubbo " xmlns:mvc=" http://www.springframework.org/schema/mvc " xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd "> <!-- Enable spring annotation to use --> <context:annotation-config></context:annotation-config> <!--  Register a custom job --> <bean id="jobDemo" class="com.itheima.jobs.JobDemo"></bean> <!--  Register JobDetail, which is responsible for calling the specified job -->through reflection <bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <!--  Inject target object --> <property name="targetObject" ref="jobDemo"/> <!--  Injection target method --> <property name="targetMethod" value="run"/> </bean> <!--  Register a trigger and specify the time when the task is triggered --> <bean id="myTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <!--  Inject JobDetail --> <property name="jobDetail" ref="jobDetail"/> <!--  Specify the trigger time, based on Cron expression --> <property name="cronExpression"> <value>0/10 * * * * ?</ value> </property> </bean> <!--  Register a unified scheduling factory to schedule tasks --> <bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <!--  Inject multiple triggers --> <property name="triggers"> <list> <ref bean="myTrigger"/> </list> </property> </bean> </beans>

3. Write task demo

 import java.util. Date; /** *Custom Job */ public class JobDemo { public void run(){ System. out. println ("Custom job executed..."+new Date()); } }

4. Test

 import org.springframework.context.support. ClassPathXmlApplicationContext; public class App { public static void main(String[] args) { new ClassPathXmlApplicationContext("spring-jobs.xml"); } }

Cron expressions are generated online:

http://cron.qqe2.com/
https://qqe2.com/cron
http://www.bejson.com/othertools/cron/
Cron expression is a string separated by 5 or 6 spaces and divided into 6 or 7 fields. Each field represents a meaning. The syntax format is as follows:
{seconds} {minutes} {hours} {dates} {months} {weeks} {years (can be empty)}
Example: "0 0 12? * WED" is executed every Wednesday at 12:00 p.m. (year is usually omitted)

Article Contents