一.固定xml调用quartz
准备工作:
1.导入quartz-1.x.x.x.jar到lib,可能还需要lib/sh4j-api-1.x.x.jar,log4g,sh4j.api,1,x,x,jar,sh4j.nop,1,x,x,jar
2.在src目录下创建quartz.properties(压缩quartz-1.x.x.x.jar 下的org/quarz下可得)
- # 配置主调度器属性
- org.quartz.scheduler.instanceName = DefaultQuartzScheduler
- org.quartz.scheduler.rmi.export = false
- org.quartz.scheduler.rmi.proxy = false
- org.quartz.scheduler.wrapJobExecutionInUserTransaction = false
- # 配置线程池
- # Quartz线程池的实现类
- org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
- # 线程池的线程数量
- org.quartz.threadPool.threadCount = 10
- # 线程池里线程的优先级
- org.quartz.threadPool.threadPriority = 5
- org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread = true
- # 配置作业存储
- org.quartz.jobStore.misfireThreshold = 60000
- org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
- #org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
- #org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.HSQLDBDelegate
- #org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.StdJDBCDelegate
- #org.quartz.jobStore.useProperties = true
- #org.quartz.jobStore.tablePrefix = QRTZ_
- #org.quartz.jobStore.isClustered = false
- #org.quartz.jobStore.maxMisfiresToHandleAtATime=1
3.编写触发时要执行的类
- package com.quartz;
- import org.quartz.JobExecutionContext;
- import org.quartz.JobExecutionException;
- import org.quartz.Trigger;
- import org.springframework.scheduling.quartz.QuartzJobBean;
- public class TestQuartzJob extends QuartzJobBean{
- @Override
- protected void executeInternal(JobExecutionContext jobexecutioncontext) throws JobExecutionException {
- // TODO Auto-generated method stub
- Trigger trigger = jobexecutioncontext.getTrigger();
- String triggerName = trigger.getName();
- System.out.println("MyQuartzJobBean"+triggerName);
- }
- }
4.配置sping管理的配置文件quartzContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">
<!-- 自定义工作类 -->
<bean id="jobDetail" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass">
<value>com.quartz.TestQuartzJob</value>
</property>
<property name="jobDataAsMap">
<map>
<!--
<entry key="upVersionDevService"> <ref bean="upVersionDevService"
/> </entry>
-->
</map>
</property>
</bean>
<!-- 注入的service的工作类 -->
<bean id="methodInvokingJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 指定定时任务调度类 -->
<property name="targetObject">
<ref bean="userService" />
</property>
<!-- 指定定时任务调度方法 -->
<property name="targetMethod">
<value>save</value>
</property>
</bean>
<!-- 每隔10秒自动调用 配置模板 使用复杂的cronTrigger-->
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="jobDetail" />
<property name="cronExpression" value="0/10 * * ? * * *" />
</bean>
<bean id="schedulerTrigger" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronTrigger"/>
</list>
</property>
</bean>
<!-- 延迟1秒后,每隔10秒自动调用 配置模板 使用简单的SimpleTriggerBean-->
<bean id="st01SimpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail" ref="checkSt01Job"></property>
<property name="startDelay" value="1000"></property>
<property name="repeatInterval" value="10000"></property>
</bean>
</beans>
- cronTrigger表达式含义:
- <property name="cronExpression" value="0/10 * * ? * * *" />
- <!-- 0 0 0 1 * ?
- # 1.秒(0-59)
- # 2.分钟(0-59)
- # 3.小时(0-23)
- # 4.月份中的日期(1-31)
- # 5.月份(1-12或SUN-DEC)
- # 6.星期中的日期(1-7或SUN-SAT)
- # 7.年份(1970-2099)
- 表达式意义
- "0 0 12 * * ?" 每天中午12点触发
- "0 15 10 ? * *" 每天上午10:15触发
- "0 15 10 * * ?" 每天上午10:15触发
- "0 15 10 * * ? *" 每天上午10:15触发
- "0 15 10 * * ? 2005" 2005年的每天上午10:15触发
- "0 * 14 * * ?" 在每天下午2点到下午2:59期间的每1分钟触发
- "0 0/5 14 * * ?" 在每天下午2点到下午2:55期间的每5分钟触发
- "0 0/5 14,18 * * ?" 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发
- "0 0-5 14 * * ?" 在每天下午2点到下午2:05期间的每1分钟触发
- "0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:10和2:44触发
- "0 15 10 ? * MON-FRI" 周一至周五的上午10:15触发
- "0 15 10 15 * ?" 每月15日上午10:15触发
- "0 15 10 L * ?" 每月最后一日的上午10:15触发
- "0 15 10 ? * 6L" 每月的最后一个星期五上午10:15触发
- "0 15 10 ? * 6L 2002-2005" 2002年至2005年的每月的最后一个星期五上午10:15触发
- "0 15 10 ? * 6#3" 每月的第三个星期五上午10:15触发
- 每天早上6点
- 0 6 * * *
- 每两个小时
- 0 */2 * * *
- 晚上11点到早上8点之间每两个小时,早上八点
- 0 23-7/2,8 * * *
- 每个月的4号和每个礼拜的礼拜一到礼拜三的早上11点
- 0 11 4 * 1-3
- 1月1日早上4点
- 0 4 1 1 *
二程序动态调用quartz(动态设置job和定时器)
1.导入quartz-1.x.x.x.jar到lib,可能还需要lib/sh4j-api-1.x.x.jar,log4g,sh4j.api,1,x,x,jar,sh4j.nop,1,x,x,jar
2.在src目录下创建quartz.properties(压缩quartz-1.x.x.x.jar 下的org/quarz下可得)
3.配置sping管理的配置文件quartzContext.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:aop="http://www.springframework.org/schema/aop"
- xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
- xmlns:jee="http://www.springframework.org/schema/jee"
- xsi:schemaLocation="
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
- http://www.springframework.org/schema/jee
- <a target="_blank" href="http://www.springframework.org/schema/jee/spring-jee-2.5.xsd'>">http://www.springframework.org/schema/jee/spring-jee-2.5.xsd"></a>
- <u><span style="color:#0000ff"><!--要注入的包名--></span></u>
- <context:component-scan base-package="com"/>
- <u><span style="color:#0000ff"><!--要注入的时间触发器--></span></u>
- <bean name="quartzScheduler"
- class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
- <property name="applicationContextSchedulerContextKey" value="applicationContextKey" />
- <property name="configLocation" value="classpath:quartz.properties" />
- </bean>
4.spring中导入QuartzServiceImpl和IQuartzService2个类 作用是动态时间执行
- package com.quartz;
- import java.util.Date;
- import org.quartz.CronExpression;
- public interface IQuartzService {
- /**
- * 根据 Quartz Cron Expression 调试任务
- * @param cronExpression Quartz Cron 表达式,如 "0/10 * * ? * * *"等
- */
- void schedule(String cronExpression);
- /**
- * 根据 Quartz Cron Expression 调试任务
- * @param name Quartz CronTrigger名称
- * @param cronExpression Quartz Cron 表达式,如 "0/10 * * ? * * *"等
- */
- void schedule(String name,String cronExpression);
- /**
- * 根据 Quartz Cron Expression 调试任务
- * @param cronExpression Quartz CronExpression
- */
- void schedule(CronExpression cronExpression);
- /**
- * 根据 Quartz Cron Expression 调试任务
- * @param name Quartz CronTrigger名称
- * @param cronExpression Quartz CronExpression
- */
- void schedule(String name,CronExpression cronExpression);
- /**
- * 在startTime时执行调试一次
- * @param startTime 调度开始时间
- */
- void schedule(Date startTime);
- /**
- * 在startTime时执行调试一次
- * @param name Quartz SimpleTrigger 名称
- * @param startTime 调度开始时间
- */
- void schedule(String name,Date startTime);
- /**
- * 在startTime时执行调试,endTime结束执行调度
- * @param startTime 调度开始时间
- * @param endTime 调度结束时间
- */
- void schedule(Date startTime,Date endTime);
- /**
- * 在startTime时执行调试,endTime结束执行调度
- * @param name Quartz SimpleTrigger 名称
- * @param startTime 调度开始时间
- * @param endTime 调度结束时间
- */
- void schedule(String name,Date startTime,Date endTime);
- /**
- * 在startTime时执行调试,endTime结束执行调度,重复执行repeatCount次
- * @param startTime 调度开始时间
- * @param endTime 调度结束时间
- * @param repeatCount 重复执行次数
- */
- void schedule(Date startTime,Date endTime,int repeatCount);
- /**
- * 在startTime时执行调试,endTime结束执行调度,重复执行repeatCount次
- * @param name Quartz SimpleTrigger 名称
- * @param startTime 调度开始时间
- * @param endTime 调度结束时间
- * @param repeatCount 重复执行次数
- */
- void schedule(String name,Date startTime,Date endTime,int repeatCount);
- /**
- * 在startTime时执行调试,endTime结束执行调度,重复执行repeatCount次,每隔repeatInterval秒执行一次
- * @param startTime 调度开始时间
- * @param endTime 调度结束时间
- * @param repeatCount 重复执行次数
- * @param repeatInterval 执行时间隔间
- */
- void schedule(Date startTime,Date endTime,int repeatCount,long repeatInterval) ;
- /**
- * 在startTime时执行调试,endTime结束执行调度,重复执行repeatCount次,每隔repeatInterval秒执行一次
- * @param name Quartz SimpleTrigger 名称
- * @param startTime 调度开始时间
- * @param endTime 调度结束时间
- * @param repeatCount 重复执行次数
- * @param repeatInterval 执行时间隔间
- */
- void schedule(String name,Date startTime,Date endTime,int repeatCount,long repeatInterval);
- /**
- * 移除定时任务
- * @param name Quartz SimpleTrigger 名称
- */
- void removeSchedule(String name);
- }
- package com.quartz;
- import java.text.ParseException;
- import java.util.Date;
- import java.util.UUID;
- import org.quartz.CronExpression;
- import org.quartz.CronTrigger;
- import org.quartz.JobDetail;
- import org.quartz.Scheduler;
- import org.quartz.SchedulerException;
- import org.quartz.SimpleTrigger;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Qualifier;
- import org.springframework.stereotype.Service;
- @Service("schedulerService")
- public class QuartzServiceImpl implements IQuartzService {
- private Scheduler scheduler;
- private JobDetail jobDetail;
- @Autowired
- public void setJobDetail(@Qualifier("jobDetail") JobDetail jobDetail) {
- this.jobDetail = jobDetail;
- }
- @Autowired
- public void setScheduler(@Qualifier("quartzScheduler") Scheduler scheduler) {
- this.scheduler = scheduler;
- }
- @Override
- public void schedule(String cronExpression) {
- schedule(null, cronExpression);
- }
- @Override
- public void schedule(String name, String cronExpression) {
- try {
- schedule(name, new CronExpression(cronExpression));
- } catch (ParseException e) {
- throw new RuntimeException(e);
- }
- }
- @Override
- public void schedule(CronExpression cronExpression) {
- schedule(null, cronExpression);
- }
- @Override
- public void schedule(String name, CronExpression cronExpression) {
- if (name == null || name.trim().equals("")) {
- name = UUID.randomUUID().toString();
- }
- try {
- scheduler.addJob(jobDetail, true);
- CronTrigger cronTrigger = new CronTrigger(name, Scheduler.DEFAULT_GROUP, jobDetail.getName(),
- Scheduler.DEFAULT_GROUP);
- cronTrigger.setCronExpression(cronExpression);
- scheduler.scheduleJob(cronTrigger);
- scheduler.rescheduleJob(name, Scheduler.DEFAULT_GROUP, cronTrigger);
- } catch (SchedulerException e) {
- throw new RuntimeException(e);
- }
- }
- @Override
- public void schedule(Date startTime) {
- schedule(startTime, null);
- }
- @Override
- public void schedule(String name, Date startTime) {
- schedule(name, startTime, null);
- }
- @Override
- public void schedule(Date startTime, Date endTime) {
- schedule(startTime, endTime, 0);
- }
- @Override
- public void schedule(String name, Date startTime, Date endTime) {
- schedule(name, startTime, endTime, 0);
- }
- @Override
- public void schedule(Date startTime, Date endTime, int repeatCount) {
- schedule(null, startTime, endTime, 0);
- }
- @Override
- public void schedule(String name, Date startTime, Date endTime, int repeatCount) {
- schedule(name, startTime, endTime, 0, 0L);
- }
- @Override
- public void schedule(Date startTime, Date endTime, int repeatCount, long repeatInterval) {
- schedule(null, startTime, endTime, repeatCount, repeatInterval);
- }
- @Override
- <span style="color:#ff0000">//根据传入时间触发注入的jobDetail
- span> public void schedule(String name, Date startTime, Date endTime, int repeatCount, long repeatInterval) {
- if (name == null || name.trim().equals("")) {
- name = UUID.randomUUID().toString();
- }
- try {
- scheduler.addJob(<span style="color:#ff0000">jobDetail</span>, true);
- System.out.println("startTime="+startTime);
- SimpleTrigger SimpleTrigger = new SimpleTrigger(name, Scheduler.DEFAULT_GROUP, jobDetail.getName(),
- Scheduler.DEFAULT_GROUP, startTime, endTime, repeatCount, repeatInterval);
- SimpleTrigger.getJobDaaMap().put('传入参数key','传入参数值'); <span style="color:#ff0000">//启动计划任务时可以调用此处传入的参数</span>
- scheduler.scheduleJob(SimpleTrigger);
- scheduler.rescheduleJob(name, Scheduler.DEFAULT_GROUP, SimpleTrigger);
- } catch (SchedulerException e) {
- throw new RuntimeException(e);
- }
- }
- @Override
- <span style="color:#ff6600">//删除暂停计划</span>
- public void removeSchedule(String name) {
- // TODO Auto-generated method stub
- try {
- scheduler.pauseTrigger(name,Scheduler.DEFAULT_GROUP);//ֹͣ������
- scheduler.unscheduleJob(name,Scheduler.DEFAULT_GROUP);//�Ƴ���
- scheduler.deleteJob(name,Scheduler.DEFAULT_GROUP);//ɾ������
- } catch (SchedulerException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- 在xml中注入
- <bean name="quartzService" class="com.quartz.QuartzServiceImpl" />
5.如何调用
ApplicationContext springContext = new ClassPathXmlApplicationContext(new String[]{"classpath:com/springResource/*.xml"});
QuartzServiceImpl quartzService = (QuartzServiceImpl)springContext.getBean("quartzService");
quartzService.setJobDetail(jobDetail); //动态设置任务
quartzService.schedule("name=testQuartz2",DateUtil.parse("2012-12-12 23:42:00")); //动态设置定时器时间
6设置不并发执行 <property name="concurrent" value="false"/>
- <bean id="demoJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean" depends-on="demoService">
- <property name="targetObject" ref="demoService"/>
- <property name="targetMethod" value="demoQuartz">
- </property>
- <property name="concurrent" value="false"/>
- </bean>
三.最简单的方法,动态设置定时器
<property name=" jobDetail">
<!-- 要执行的bean -->
</property>
<!-- cron表达式 -->
<property name=" cronExpression">
<!-- 每隔90分钟执行一次-->
<value>0 0/90 * * * ?</value>
</property>
</bean>
<property name=" jobDetail">
<!-- 要执行的bean -->
</property>
</bean>