了解quartz基础,看上一篇
Maven导包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
定义一个任务类MyJob.java
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.quartz.QuartzJobBean;
public class Myjob extends QuartzJobBean{
private static int count=1;
@Override
protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {
System.out.println("Myjob开始执行了。。。。"+arg0.getTrigger().getKey().getName());
ApplicationContext applicationContext=(ApplicationContext)arg0.getJobDetail().getJobDataMap()
.get("applicationContext");
System.out.println("获取到的容器是:"+(count++)+"|"+applicationContext);
}
}
增加一个spring的配置文件,applicationContext.xml
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 定义任务Bean -->
<bean name="myJobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<!-- 指定具体的job类 -->
<property name="jobClass" value="com.quartz.Myjob"></property>
<!-- 指定job的名称 -->
<property name="name" value="myJob"></property>
<!-- 指定job的分组 -->
<property name="group" value="jobs"></property>
<!-- 必须设置为true,如果为false,当没有活动的触发器与之关联的时候回在调度器中删除该任务 -->
<property name="durability" value="true"></property>
<!-- 指定spring容器的可以,如果不设定在job中的jobmap中是获取不到spring容器的 -->
<property name="applicationContextJobDataKey" value="applicationContext"></property>
</bean>
<!-- 定义触发器 -->
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="myJobDetail"></property>
<!-- 定义定时表达式 -->
<property name="cronExpression" value="/5 * * * * ?"></property>
</bean>
<!-- 定义调度器 -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronTrigger"/>
</list>
</property>
</bean>
</beans>
测试类
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
}
}
测试结果
八月 09, 2017 7:11:40 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@49c2faae: startup date [Wed Aug 09 19:11:40 GMT+08:00 2017]; root of context hierarchy
八月 09, 2017 7:11:40 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.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.
八月 09, 2017 7:11:40 下午 org.springframework.context.support.DefaultLifecycleProcessor start
信息: Starting beans in phase 2147483647
八月 09, 2017 7:11:40 下午 org.springframework.scheduling.quartz.SchedulerFactoryBean startScheduler
信息: Starting Quartz Scheduler now
Myjob开始执行了。。。。cronTrigger
获取到的容器是:1|org.springframework.context.support.ClassPathXmlApplicationContext@49c2faae: startup date [Wed Aug 09 19:11:40 GMT+08:00 2017]; root of context hierarchy
Myjob开始执行了。。。。cronTrigger
获取到的容器是:2|org.springframework.context.support.ClassPathXmlApplicationContext@49c2faae: startup date [Wed Aug 09 19:11:40 GMT+08:00 2017]; root of context hierarchy
Myjob开始执行了。。。。cronTrigger
获取到的容器是:3|org.springframework.context.support.ClassPathXmlApplicationContext@49c2faae: startup date [Wed Aug 09 19:11:40 GMT+08:00 2017]; root of context hierarchy