Job:是一个接口只有一个方法void execute(JobExecutionContext context),开发者实现该接口定义运行任务,JobExecutionContext类提供了调度上下文的各种信息。Job运行时的信息保存在 JobDataMap实例中
第一种,作业类继承自特定的基 1.8测试成功,2.0不行类:org.springframework.scheduling.quartz.QuartzJobBean。
java类代码
package com.ncs.hj; import java.util.Date; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.springframework.scheduling.quartz.QuartzJobBean; public class SpringQtz extends QuartzJobBean{ private static int counter = 0; protected void executeInternal(JobExecutionContext context) throws JobExecutionException { System.out.println(Thread.currentThread().getName()); long ms = System.currentTimeMillis(); System.out.println( new Date(ms)); System.out.println(ms); System.out.println("(" + counter++ + ")"); String s = (String) context.getMergedJobDataMap().get("service"); System.out.println(s); } }
spring配置文件
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:task="http://www.springframework.org/schema/task" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd" default-autowire="byName" default-lazy-init="false"> <!-- 配置调度程序quartz ,其中配置JobDetail有两种方式 方式一:使用JobDetailBean,任务类必须实现Job接口 --> <bean id="myjob" class="org.springframework.scheduling.quartz.JobDetailBean"> <property name="jobClass" value="com.ncs.hj.SpringQtz"></property> <property name="jobDataAsMap"> <map> <entry key="service"><value>simple is the beat</value></entry> </map> </property> </bean> <!-- ======================== 调度触发器 ======================== --> <bean id="CronTriggerBean" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail" ref="myjob"></property> <property name="cronExpression" value="0/5 * * * * ?"></property> </bean> <!-- ======================== 调度工厂 ======================== --> <bean id="SpringJobSchedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="CronTriggerBean"/> </list> </property> </bean> </beans>
第二种,作业类不继承特定基类。1.8和2.0均测试成功, spring 4以上版本
java类
package com.ncs.hj; import java.util.Date; public class SpringQtz2 { private static int counter = 0; protected void execute() { System.out.println(Thread.currentThread().getName()); long ms = System.currentTimeMillis(); System.out.println( new Date(ms)); System.out.println("(" + counter++ + ")"); } }
配置文件
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:task="http://www.springframework.org/schema/task" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd" default-autowire="byName" default-lazy-init="false"> <!-- 需要定时执行的类 --> <bean id="linkConsumeController" class="com.jikexueyuancrm.controller.LinkConsumeController"/> <!-- 配置job,类中每个方法对应一个job --> <bean id="consumeJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref ="linkConsumeController"/> <property name="targetMethod" value="consume" /> <!--要执行的方法名称 --> <!--禁止并发 --> <!-- 是否允许任务并发执行。当值为false时,表示必须等到前一个线程处理完毕后才再启一个新的线程 --> <property name="concurrent" value="false "/> </bean> <!-- 每个job需要单独配置任务的定时执行(触发器) CronTriggerFactoryBean或者SimpleTriggerFactoryBean --> <bean id="consumeTimer" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail" ref="consumeJob"></property> <!--对应job --> <property name="cronExpression" value="0/5 * * * * ?"></property> </bean> <!-- 最终的启动工厂 list里面配置多个定时触发器--> <bean id="SpringJobSchedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="consumeTimer"/> </list> </property> </bean> </beans>
quartz默认是多线程的
定时任务的注解配置可参考这篇文章:
http://blog.csdn.net/xpsharp/article/details/8189212
参考文章:
http://my.oschina.net/u/559635/blog/389558
http://kevin19900306.iteye.com/blog/1397744
http://blog.csdn.net/huihuimimi17/article/details/8215779
本文出自 “点滴积累” 博客,请务必保留此出处http://tianxingzhe.blog.51cto.com/3390077/1741169