Simple and quick use of scheduled tasks Quartz
<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>
<? 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>
import java.util. Date; /** *Custom Job */ public class JobDemo { public void run(){ System. out. println ("Custom job executed..."+new Date()); } }
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: