1. Get the quartz details jar
<!-- quartz 的jar -->
<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>
2. Related code cases
//Main.java startup class
public class Main {
public static void main(String[] args) {
new ClassPathXmlApplicationContext("classpath:applicationContext-scheduler.xml");
}
}
//MyJob task class
public class MyJob extends QuartzJobBean {
@Override
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
System.out.println("myJob执行了............" + context.getTrigger().getKey().getName());
ApplicationContext applicationContext = (ApplicationContext) context.getJobDetail().getJobDataMap()
.get("applicationContext");
System.out.println( "The obtained Spring container is " + applicationContext);
}
}
//applicationContext-scheduler.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 定义任务bean -->
<bean name="myJobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<!-- 指定具体的job类 -->
<property name="jobClass" value="com.bky_lzw.quartz.MyJob" />
<!-- 指定job的名称 -->
<property name="name" value="myJob" />
<!-- 指定job分组 -->
<property name="group" value="jobs" />
<!--Must be set to true, if false, the task will be deleted in the scheduler when no active trigger is associated with it -->
< property name ="durability" value ="true" />
<!-- specify spring The key of the container, if it is not set in the jobmap in the job, the spring container cannot be obtained -->
< property name = "applicationContextJobDataKey" value = = "applicationContext" />
</ bean >
<!-- Define Trigger -->
< bean id ="cronTrigger" class ="org.springframework.scheduling.quartz.CronTriggerFactoryBean" >
< property name ="jobDetail" ref ="myJobDetail" />
<!-- each Execute once a minute -->
< property name ="cronExpression" value ="0/5 * * * * ?" />
</ bean >
<!-- 定义调度器 -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronTrigger" />
</list>
</property>
</bean>
</beans>