spring4集成quartz2.2.1(maven)
2015-08-08 11:16
323 查看
由于很多大多项目会有一些定时任务执行的需求,quartz可以解决此问题,quartz的实现由两种方式:
(1) 采用配置文件的方式,此方法很好实现,可以百度搜索;
(2)采用quartz数据持久化的方式,可以动态修改任务和触发器的执行时间、删除触发器、暂停、恢复触发器等功能。对于我来说quartz之前用过,但是都是 别人集成好的,没有研究过,这次恰好有机会集成quartz,花了三天的时间,查了很多资料,才能实现.我建议在集成quartz之前先看Quartz_Job+Scheduling_Framework基础原理,如下是所需资料下载地址
Quartz_Job+Scheduling_Framework 下载地址: http://download.csdn.net/detail/fish_di/8966273#comment
参照文章地址:
/article/3851401.html
quartz发行包下载地址:
http://download.csdn.net/detail/fish_di/8976133
下面具体集成步骤:
一. 实现数据持久化需构建数据库,可到官网下载,也可到如上提供路径进行下载quartz2.2.1包,解压,根据不同数据库选择不同初始化表,路径quartz-2.2.1-distribution\quartz-2.2.1\docs\dbTables,mysql为tables_mysql.sql.
二. pom.xml中添加quartz相关jar包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</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>
三. 项目中数据源的配置applicationContext.xml,集成jetty
<beans profile="production">
<context:property-placeholder ignore-unresolvable="true"
location="classpath*:/application.properties" order="1"/>
<!-- 数据源配置, 使用Tomcat JDBC连接池 -->
<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close">
<!-- Connection Info -->
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- Connection Pooling Info -->
<property name="maxActive" value="${jdbc.pool.maxActive}" />
<property name="maxIdle" value="${jdbc.pool.maxIdle}" />
<property name="minIdle" value="0" />
<property name="defaultAutoCommit" value="false" />
</bean>
</beans>
quartz数据源后续配置
四. 实现动态配置定时任务
Quartz在Spring中的动态定时,发现中cronExpression是关键,如果可以动态设置cronExpression的 值,就可以顺利解决问题了。这样我们就不能直接使用 org.springframework.scheduling.quartz.CronTriggerFactoryBean,需要自己实现一个动态调 度服务类,在其中构建CronTrigger或SimpleTrigger,动态配置时间。
动态调度服务接口:
package io.platform.em.service;
import org.quartz.CronExpression;
import java.util.Date;
/**
* on 2015/8/3.
*/
public interface SchedulerService {
/**
* 根据 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 name Quartz CronTrigger名称
* @param group Quartz CronTrigger组
* @param cronExpression Quartz Cron 表达式,如 "0/10 * * ? * * *"等
*/
void schedule(String name, String group, 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);
/**
* 根据 Quartz Cron Expression 调试任务
*
* @param name Quartz CronTrigger名称
* @param group Quartz CronTrigger组
* @param cronExpression Quartz CronExpression
*/
void schedule(String name, String group, CronExpression cronExpression);
/**
* 在startTime时执行调试一次
*
* @param startTime 调度开始时间
*/
void schedule(Date startTime);
void schedule(Date startTime, String group);
/**
* 在startTime时执行调试一次
*
* @param name Quartz SimpleTrigger 名称
* @param startTime 调度开始时间
*/
void schedule(String name, Date startTime);
void schedule(String name, Date startTime, String group);
/**
* 在startTime时执行调试,endTime结束执行调度
*
* @param startTime 调度开始时间
* @param endTime 调度结束时间
*/
void schedule(Date startTime, Date endTime);
void schedule(Date startTime, Date endTime, String group);
/**
* 在startTime时执行调试,endTime结束执行调度
*
* @param name Quartz SimpleTrigger 名称
* @param startTime 调度开始时间
* @param endTime 调度结束时间
*/
void schedule(String name, Date startTime, Date endTime);
void schedule(String name, Date startTime, Date endTime, String group);
/**
* 在startTime时执行调试,endTime结束执行调度,重复执行repeatCount次
*
* @param startTime 调度开始时间
* @param repeatCount 重复执行次数
*/
void schedule(Date startTime, int repeatCount);
/**
* 在startTime时执行调试,endTime结束执行调度,重复执行repeatCount次
*
* @param startTime 调度开始时间
* @param endTime 调度结束时间
* @param repeatCount 重复执行次数
*/
void schedule(Date startTime, Date endTime, int repeatCount);
void schedule(Date startTime, Date endTime, int repeatCount, String group);
/**
* 在startTime时执行调试,endTime结束执行调度,重复执行repeatCount次
*
* @param name Quartz SimpleTrigger 名称
* @param startTime 调度开始时间
* @param endTime 调度结束时间
* @param repeatCount 重复执行次数
*/
void schedule(String name, Date startTime, Date endTime, int repeatCount);
void schedule(String name, Date startTime, Date endTime, int repeatCount, String group);
/**
* 在startTime时执行调试,endTime结束执行调度,重复执行repeatCount次,每隔repeatInterval秒执行一次
*
* @param startTime 调度开始时间
* @param repeatCount 重复执行次数
* @param repeatInterval 执行时间隔间
*/
void schedule(Date startTime, int repeatCount, long repeatInterval);
/**
* 在startTime时执行调试,endTime结束执行调度,重复执行repeatCount次,每隔repeatInterval秒执行一次
*
* @param startTime 调度开始时间
* @param endTime 调度结束时间
* @param repeatCount 重复执行次数
* @param repeatInterval 执行时间隔间
*/
void schedule(Date startTime, Date endTime, int repeatCount, long repeatInterval);
void schedule(Date startTime, Date endTime, int repeatCount, long repeatInterval, String group);
/**
* 在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);
void schedule(String name, Date startTime, Date endTime, int repeatCount, long repeatInterval, String group);
/**
* 暂停触发器
*
* @param triggerName 触发器名称
*/
void pauseTrigger(String triggerName);
/**
* 暂停触发器
*
* @param triggerName 触发器名称
* @param group 触发器组
*/
void pauseTrigger(String triggerName, String group);
/**
* 恢复触发器
*
* @param triggerName 触发器名称
*/
void resumeTrigger(String triggerName);
/**
* 恢复触发器
*
* @param triggerName 触发器名称
* @param group 触发器组
*/
void resumeTrigger(String triggerName, String group);
/**
* 删除触发器
*
* @param triggerName 触发器名称
* @return
*/
boolean removeTrigdger(String triggerName);
/**
* 删除触发器
*
* @param triggerName 触发器名称
* @param group 触发器组
* @return
*/
boolean removeTrigdger(String triggerName, String group);
}
实现类:
package io.platform.em.service;
import org.quartz.CronExpression;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.TriggerKey;
import org.quartz.impl.triggers.CronTriggerImpl;
import org.quartz.impl.triggers.SimpleTriggerImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.text.ParseException;
import java.util.Date;
import java.util.UUID;
/**
* Created by zon 2015/8/3.
*/
@Service("schedulerService")
public class SchedulerServiceImpl implements SchedulerService{
private static final String NULLSTRING = null;
private static final Date NULLDATE = null;
@Autowired
private Scheduler scheduler;
@Autowired
private JobDetail jobDetail;
@Override
public void schedule(String cronExpression) {
// System.out.print("222222222");
schedule(NULLSTRING, cronExpression);
}
@Override
public void schedule(String name, String cronExpression) {
schedule(name, NULLSTRING, cronExpression);
}
@Override
public void schedule(String name, String group, String cronExpression) {
try {
schedule(name, group, new CronExpression(cronExpression));
} catch (ParseException e) {
throw new IllegalArgumentException(e);
}
}
@Override
public void schedule(CronExpression cronExpression) {
schedule(NULLSTRING, cronExpression);
}
@Override
public void schedule(String name, CronExpression cronExpression) {
schedule(name, NULLSTRING, cronExpression);
}
@Override
public void schedule(String name, String group, CronExpression cronExpression) {
if (isValidExpression(cronExpression)) {
if (name == null || name.trim().equals("")) {
name = UUID.randomUUID().toString();
}
CronTriggerImpl trigger = new CronTriggerImpl();
trigger.setCronExpression(cronExpression);
TriggerKey triggerKey = new TriggerKey(name, group);
trigger.setJobName(jobDetail.getKey().getName());
trigger.setKey(triggerKey);
try {
scheduler.addJob(jobDetail, true);
if (scheduler.checkExists(triggerKey)) {
scheduler.rescheduleJob(triggerKey, trigger);
} else {
scheduler.scheduleJob(trigger);
}
} catch (SchedulerException e) {
throw new IllegalArgumentException(e);
}
}
}
@Override
public void schedule(Date startTime) {
schedule(startTime, NULLDATE);
}
@Override
public void schedule(Date startTime, String group) {
schedule(startTime, NULLDATE, group);
}
@Override
public void schedule(String name, Date startTime) {
schedule(name, startTime, NULLDATE);
}
@Override
public void schedule(String name, Date startTime, String group) {
schedule(name, startTime, NULLDATE, group);
}
@Override
public void schedule(Date startTime, Date endTime) {
schedule(startTime, endTime, 0);
}
@Override
public void schedule(Date startTime, Date endTime, String group) {
schedule(startTime, endTime, 0, group);
}
@Override
public void schedule(String name, Date startTime, Date endTime) {
schedule(name, startTime, endTime, 0);
}
@Override
public void schedule(String name, Date startTime, Date endTime, String group) {
schedule(name, startTime, endTime, 0, group);
}
@Override
public void schedule(Date startTime, int repeatCount) {
schedule(null, startTime, NULLDATE, 0);
}
@Override
public void schedule(Date startTime, Date endTime, int repeatCount) {
schedule(null, startTime, endTime, 0);
}
@Override
public void schedule(Date startTime, Date endTime, int repeatCount, String group) {
schedule(null, startTime, endTime, 0, group);
}
@Override
public void schedule(String name, Date startTime, Date endTime, int repeatCount) {
schedule(name, startTime, endTime, 0, 0L);
}
@Override
public void schedule(String name, Date startTime, Date endTime, int repeatCount, String group) {
schedule(name, startTime, endTime, 0, 0L, group);
}
@Override
public void schedule(Date startTime, int repeatCount, long repeatInterval) {
schedule(null, startTime, NULLDATE, repeatCount, repeatInterval);
}
@Override
public void schedule(Date startTime, Date endTime, int repeatCount, long repeatInterval) {
schedule(null, startTime, endTime, repeatCount, repeatInterval);
}
@Override
public void schedule(Date startTime, Date endTime, int repeatCount, long repeatInterval, String group) {
schedule(null, startTime, endTime, repeatCount, repeatInterval, group);
}
@Override
public void schedule(String name, Date startTime, Date endTime, int repeatCount, long repeatInterval) {
schedule(name, startTime, endTime, repeatCount, repeatInterval, NULLSTRING);
}
@Override
public void schedule(String name, Date startTime, Date endTime, int repeatCount, long repeatInterval, String group) {
if (this.isValidExpression(startTime)) {
if (name == null || name.trim().equals("")) {
name = UUID.randomUUID().toString();
}
TriggerKey triggerKey = new TriggerKey(name, group);
SimpleTriggerImpl trigger = new SimpleTriggerImpl();
trigger.setKey(triggerKey);
trigger.setJobName(jobDetail.getKey().getName());
trigger.setStartTime(startTime);
trigger.setEndTime(endTime);
trigger.setRepeatCount(repeatCount);
trigger.setRepeatInterval(repeatInterval);
try {
scheduler.addJob(jobDetail, true);
if (scheduler.checkExists(triggerKey)) {
scheduler.rescheduleJob(triggerKey, trigger);
} else {
scheduler.scheduleJob(trigger);
}
} catch (SchedulerException e) {
throw new IllegalArgumentException(e);
}
}
}
@Override
public void pauseTrigger(String triggerName) {
pauseTrigger(triggerName, NULLSTRING);
}
@Override
public void pauseTrigger(String triggerName, String group) {
try {
scheduler.pauseTrigger(new TriggerKey(triggerName, group));// 停止触发器
} catch (SchedulerException e) {
throw new RuntimeException(e);
}
}
@Override
public void resumeTrigger(String triggerName) {
resumeTrigger(triggerName, NULLSTRING);
}
@Override
public void resumeTrigger(String triggerName, String group) {
try {
scheduler.resumeTrigger(new TriggerKey(triggerName, group));// 重启触发器
} catch (SchedulerException e) {
throw new RuntimeException(e);
}
}
@Override
public boolean removeTrigdger(String triggerName) {
return removeTrigdger(triggerName, NULLSTRING);
}
@Override
public boolean removeTrigdger(String triggerName, String group) {
TriggerKey triggerKey = new TriggerKey(triggerName, group);
try {
scheduler.pauseTrigger(triggerKey);// 停止触发器
return scheduler.unscheduleJob(triggerKey);// 移除触发器
} catch (SchedulerException e) {
throw new RuntimeException(e);
}
}
private boolean isValidExpression(final CronExpression cronExpression) {
CronTriggerImpl trigger = new CronTriggerImpl();
trigger.setCronExpression(cronExpression);
Date date = trigger.computeFirstFireTime(null);
return date != null && date.after(new Date());
}
private boolean isValidExpression(final Date startTime) {
SimpleTriggerImpl trigger = new SimpleTriggerImpl();
trigger.setStartTime(startTime);
Date date = trigger.computeFirstFireTime(null);
return date != null && date.after(new Date());
}
}
SchedulerService 只有一个多态方法schedule,SchedulerServiceImpl实现SchedulerService接口,注入 org.quartz.Schedulert和org.quartz.JobDetail,schedule方法可以动态配置 org.quartz.CronExpression或org.quartz.SimpleTrigger调度时间。
五. 实现自己的org.quartz.JobDetail
package io.platform.em.web;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.PersistJobDataAfterExecution;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.quartz.QuartzJobBean;
/**
* Created by zon 2015/8/3.
*/
@PersistJobDataAfterExecution
//@DisallowConcurrentExecution// 不允许并发执行
public class MyQuartzJobBean extends QuartzJobBean{
@Override
protected void executeInternal(JobExecutionContext jobexecutioncontext) throws JobExecutionException {
Trigger trigger = jobexecutioncontext.getTrigger();
String triggerName = trigger.getKey().getName();
io.platform.em.service.SimpleService simpleService = getApplicationContext(jobexecutioncontext).getBean("simpleService",
io.platform.em.service.SimpleService.class);
simpleService.testMethod(triggerName);
}
private ApplicationContext getApplicationContext(final JobExecutionContext jobexecutioncontext) {
try {
return (ApplicationContext) jobexecutioncontext.getScheduler().getContext().get("applicationContextKey");
} catch (SchedulerException e) {
// logger.error("jobexecutioncontext.getScheduler().getContext() error!", e);
throw new RuntimeException(e);
}
}
}
调用servicebean如下:
package io.platform.em.service;
import io.platform.util.date.DateUtils;
import org.springframework.stereotype.Service;
import java.io.Serializable;
/**
* Created by zhi on 2015/8/3.
*/
@Service("simpleService") //执行定时任务业务
public class SimpleService implements Serializable{
public void testMethod(String triggerName) {
// 这里执行定时调度业务
System.out.println("1动态执行了"+ triggerName);
System.out.println(DateUtils.getDateTime());
}
public void testMethod2(String triggerName) {
// 这里执行定时调度业务
System.out.println("2动态执行了");
System.out.println(DateUtils.getDateTime());
}
}
SimpleService主要用于定时任务执行的业务
六. 配置applicationContext-quartz.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean name="quartzScheduler"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean" autowire="no"><!-- 容器启动随机启动lazy-init="false" autowire="no"--><property name="autoStartup" value="true"/><property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="applicationContextSchedulerContextKey" value="applicationContextKey"/>
<property name="waitForJobsToCompleteOnShutdown" value="true"/>
<property name="configLocation" value="classpath:quartz.properties"/>
</bean>
<bean id="jobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<property name="jobClass">
<value>io.platform.em.web.MyQuartzJobBean</value>
</property>
<property name="durability" value="true"/>
</bean>
</beans>
七. quartz中数据源配置quartz.properties (参数含义请参照Quartz_Job+Scheduling_Framework)
org.quartz.scheduler.instanceName = DefaultQuartzScheduler
org.quartz.scheduler.rmi.export = false
org.quartz.scheduler.rmi.proxy = false
org.quartz.scheduler.wrapJobExecutionInUserTransaction = false
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
#per JobStoreTX ram RAMJobStore
#org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
# StdJDBCDelegate tongyong
#org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.HSQLDBDelegate
org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.StdJDBCDelegate
org.quartz.jobStore.useProperties = false
org.quartz.jobStore.tablePrefix = QRTZ_
org.quartz.jobStore.isClustered = false
org.quartz.jobStore.maxMisfiresToHandleAtATime= 20000
org.quartz.jobStore.dataSource = myDS
org.quartz.dataSource.myDS.driver=com.mysql.jdbc.Driver
org.quartz.dataSource.myDS.URL=jdbc:mysql://*.*.*.*:3306/*?createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=utf-8
org.quartz.dataSource.myDS.user=root
org.quartz.dataSource.myDS.password=root
八。测试类
package io.platform.test;
/**
* Created by zh on 2015/8/4.
*/
import io.platform.em.service.SchedulerService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.text.SimpleDateFormat;
import java.util.Date;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath*:/applicationContext.xml", "classpath*:/applicationContext-shiro.xml","classpath*:/application-quartz.xml"})
@ActiveProfiles("production")
public class TestQuartz {
private static Logger LOGGER = LoggerFactory.getLogger(Test.class);
private static String JOB_GROUP_NAME = "zydGroup";
private static String TRIGGER_GROUP_NAME = "zydTriggerGroup";
private static SchedulerFactory sf = new StdSchedulerFactory();
@Autowired
private SchedulerService schedulerService;
@Test
public void test() throws SchedulerException {//,Exception,ParseException
//schedule ("", Date startTime, Date endTime,int repeatCount, long repeatInterval, String group)
Date startTime = parse("2015-08-05 13:55:00");
Date endTime = parse("2015-08-05 15:15:00");
// schedulerService.resumeTrigger("f64eee37-62f4-473f-b5b9-d7a76d86c443");
schedulerService.schedule("0 28 17 * * ? *");
// schedulerService.schedule("f64eee37-62f4-473f-b5b9-d7a76d86c443", "0 45 09 * * ? *");
//schedulerService.pauseTrigger("f64eee37-62f4-473f-b5b9-d7a76d86c443");
//schedulerService.resumeTrigger("0fdb7134-9701-494c-9879-bbf2e30120b8");
// schedulerService.schedule("0 17 16 * * ? *");
//schedulerService.schedule(startTime);
// schedulerService.pauseTrigger("f4a610a7-5b43-4738-949c-150eec96fb62");
// schedulerService.schedule(startTime, endTime,5,5);
// schedulerService.schedule("ZYD", startTime, endTime, 1, 10);
// schedulerService.schedule(startTime);
// schedulerService.removeTrigdger("f4a610a7-5b43-4738-949c-150eec96fb62");
int repeatCount = 10;
long repeatInterval = 2;
// String name="b870cf9b-c313-4fd6-87ad-e99de7580290";
// schedulerService.schedule(name, startTime, endTime, repeatCount, repeatInterval);
/* schedulerService.schedule("0/10 * * ? * * *");
// 2014-08-19 16:33:00开始执行调度
schedulerService.schedule(startTime);
// 2014-08-19 16:33:00开始执行调度,2014-08-22 21:10:00结束执行调试
schedulerService.schedule(startTime, endTime);
// 2014-08-19 16:33:00开始执行调度,执行5次结束
schedulerService.schedule(startTime, 5);*/
// 2014-08-19 16:33:00开始执行调度,每隔20秒执行一次,执行5次结束
// schedulerService.schedule(startTime, 5, 20);
}
/*
try {
LOGGER.info("33333333333");
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
LOGGER.info("Scheduler starting up...");
System.out.print("4444444444");
scheduler.start();
} catch (Exception exception) {
System.out.print(exception);
}
schedulerService.schedule("0/2 * * ? * * *");
Date startTime = parse("2015-08-05 10:15:00");
Date endTime = parse("2015-08-05 15:15:00");
int repeatCount=10;
long repeatInterval=2;
// String name="b870cf9b-c313-4fd6-87ad-e99de7580290";
// schedulerService.schedule(name, startTime, endTime, repeatCount, repeatInterval);
schedulerService.schedule("0/10 * * ? * * *");
// 2014-08-19 16:33:00开始执行调度
schedulerService.schedule(startTime);
// 2014-08-19 16:33:00开始执行调度,2014-08-22 21:10:00结束执行调试
schedulerService.schedule(startTime, endTime);
// 2014-08-19 16:33:00开始执行调度,执行5次结束
schedulerService.schedule(startTime, 5);
// 2014-08-19 16:33:00开始执行调度,每隔20秒执行一次,执行5次结束
schedulerService.schedule(startTime, 5, 20);
*/
// schedulerService.schedule("quartz1", "ZYD", "0/2 * * ? * * *");
private static Date parse(String dateStr) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
return format.parse(dateStr);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
查看数据库表运行结果,可按实际需要启动停止trigger.
由于很多大多项目会有一些定时任务执行的需求,quartz可以解决此问题,quartz的实现由两种方式:
(1) 采用配置文件的方式,此方法很好实现,可以百度搜索;
(2)采用quartz数据持久化的方式,可以动态修改任务和触发器的执行时间、删除触发器、暂停、恢复触发器等功能。对于我来说quartz之前用过,但是都是 别人集成好的,没有研究过,这次恰好有机会集成quartz,花了三天的时间,查了很多资料,才能实现.我建议在集成quartz之前先看Quartz_Job+Scheduling_Framework基础原理,如下是所需资料下载地址
Quartz_Job+Scheduling_Framework 下载地址: http://download.csdn.net/detail/fish_di/8966273#comment
参照文章地址:
/article/3851401.html
quartz发行包下载地址:
http://download.csdn.net/detail/fish_di/8976133
下面具体集成步骤:
一. 实现数据持久化需构建数据库,可到官网下载,也可到如上提供路径进行下载quartz2.2.1包,解压,根据不同数据库选择不同初始化表,路径quartz-2.2.1-distribution\quartz-2.2.1\docs\dbTables,mysql为tables_mysql.sql.
二. pom.xml中添加quartz相关jar包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</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>
三. 项目中数据源的配置applicationContext.xml,集成jetty
<beans profile="production">
<context:property-placeholder ignore-unresolvable="true"
location="classpath*:/application.properties" order="1"/>
<!-- 数据源配置, 使用Tomcat JDBC连接池 -->
<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close">
<!-- Connection Info -->
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- Connection Pooling Info -->
<property name="maxActive" value="${jdbc.pool.maxActive}" />
<property name="maxIdle" value="${jdbc.pool.maxIdle}" />
<property name="minIdle" value="0" />
<property name="defaultAutoCommit" value="false" />
</bean>
</beans>
quartz数据源后续配置
四. 实现动态配置定时任务
Quartz在Spring中的动态定时,发现中cronExpression是关键,如果可以动态设置cronExpression的 值,就可以顺利解决问题了。这样我们就不能直接使用 org.springframework.scheduling.quartz.CronTriggerFactoryBean,需要自己实现一个动态调 度服务类,在其中构建CronTrigger或SimpleTrigger,动态配置时间。
动态调度服务接口:
package io.platform.em.service;
import org.quartz.CronExpression;
import java.util.Date;
/**
* on 2015/8/3.
*/
public interface SchedulerService {
/**
* 根据 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 name Quartz CronTrigger名称
* @param group Quartz CronTrigger组
* @param cronExpression Quartz Cron 表达式,如 "0/10 * * ? * * *"等
*/
void schedule(String name, String group, 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);
/**
* 根据 Quartz Cron Expression 调试任务
*
* @param name Quartz CronTrigger名称
* @param group Quartz CronTrigger组
* @param cronExpression Quartz CronExpression
*/
void schedule(String name, String group, CronExpression cronExpression);
/**
* 在startTime时执行调试一次
*
* @param startTime 调度开始时间
*/
void schedule(Date startTime);
void schedule(Date startTime, String group);
/**
* 在startTime时执行调试一次
*
* @param name Quartz SimpleTrigger 名称
* @param startTime 调度开始时间
*/
void schedule(String name, Date startTime);
void schedule(String name, Date startTime, String group);
/**
* 在startTime时执行调试,endTime结束执行调度
*
* @param startTime 调度开始时间
* @param endTime 调度结束时间
*/
void schedule(Date startTime, Date endTime);
void schedule(Date startTime, Date endTime, String group);
/**
* 在startTime时执行调试,endTime结束执行调度
*
* @param name Quartz SimpleTrigger 名称
* @param startTime 调度开始时间
* @param endTime 调度结束时间
*/
void schedule(String name, Date startTime, Date endTime);
void schedule(String name, Date startTime, Date endTime, String group);
/**
* 在startTime时执行调试,endTime结束执行调度,重复执行repeatCount次
*
* @param startTime 调度开始时间
* @param repeatCount 重复执行次数
*/
void schedule(Date startTime, int repeatCount);
/**
* 在startTime时执行调试,endTime结束执行调度,重复执行repeatCount次
*
* @param startTime 调度开始时间
* @param endTime 调度结束时间
* @param repeatCount 重复执行次数
*/
void schedule(Date startTime, Date endTime, int repeatCount);
void schedule(Date startTime, Date endTime, int repeatCount, String group);
/**
* 在startTime时执行调试,endTime结束执行调度,重复执行repeatCount次
*
* @param name Quartz SimpleTrigger 名称
* @param startTime 调度开始时间
* @param endTime 调度结束时间
* @param repeatCount 重复执行次数
*/
void schedule(String name, Date startTime, Date endTime, int repeatCount);
void schedule(String name, Date startTime, Date endTime, int repeatCount, String group);
/**
* 在startTime时执行调试,endTime结束执行调度,重复执行repeatCount次,每隔repeatInterval秒执行一次
*
* @param startTime 调度开始时间
* @param repeatCount 重复执行次数
* @param repeatInterval 执行时间隔间
*/
void schedule(Date startTime, int repeatCount, long repeatInterval);
/**
* 在startTime时执行调试,endTime结束执行调度,重复执行repeatCount次,每隔repeatInterval秒执行一次
*
* @param startTime 调度开始时间
* @param endTime 调度结束时间
* @param repeatCount 重复执行次数
* @param repeatInterval 执行时间隔间
*/
void schedule(Date startTime, Date endTime, int repeatCount, long repeatInterval);
void schedule(Date startTime, Date endTime, int repeatCount, long repeatInterval, String group);
/**
* 在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);
void schedule(String name, Date startTime, Date endTime, int repeatCount, long repeatInterval, String group);
/**
* 暂停触发器
*
* @param triggerName 触发器名称
*/
void pauseTrigger(String triggerName);
/**
* 暂停触发器
*
* @param triggerName 触发器名称
* @param group 触发器组
*/
void pauseTrigger(String triggerName, String group);
/**
* 恢复触发器
*
* @param triggerName 触发器名称
*/
void resumeTrigger(String triggerName);
/**
* 恢复触发器
*
* @param triggerName 触发器名称
* @param group 触发器组
*/
void resumeTrigger(String triggerName, String group);
/**
* 删除触发器
*
* @param triggerName 触发器名称
* @return
*/
boolean removeTrigdger(String triggerName);
/**
* 删除触发器
*
* @param triggerName 触发器名称
* @param group 触发器组
* @return
*/
boolean removeTrigdger(String triggerName, String group);
}
实现类:
package io.platform.em.service;
import org.quartz.CronExpression;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.TriggerKey;
import org.quartz.impl.triggers.CronTriggerImpl;
import org.quartz.impl.triggers.SimpleTriggerImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.text.ParseException;
import java.util.Date;
import java.util.UUID;
/**
* Created by zon 2015/8/3.
*/
@Service("schedulerService")
public class SchedulerServiceImpl implements SchedulerService{
private static final String NULLSTRING = null;
private static final Date NULLDATE = null;
@Autowired
private Scheduler scheduler;
@Autowired
private JobDetail jobDetail;
@Override
public void schedule(String cronExpression) {
// System.out.print("222222222");
schedule(NULLSTRING, cronExpression);
}
@Override
public void schedule(String name, String cronExpression) {
schedule(name, NULLSTRING, cronExpression);
}
@Override
public void schedule(String name, String group, String cronExpression) {
try {
schedule(name, group, new CronExpression(cronExpression));
} catch (ParseException e) {
throw new IllegalArgumentException(e);
}
}
@Override
public void schedule(CronExpression cronExpression) {
schedule(NULLSTRING, cronExpression);
}
@Override
public void schedule(String name, CronExpression cronExpression) {
schedule(name, NULLSTRING, cronExpression);
}
@Override
public void schedule(String name, String group, CronExpression cronExpression) {
if (isValidExpression(cronExpression)) {
if (name == null || name.trim().equals("")) {
name = UUID.randomUUID().toString();
}
CronTriggerImpl trigger = new CronTriggerImpl();
trigger.setCronExpression(cronExpression);
TriggerKey triggerKey = new TriggerKey(name, group);
trigger.setJobName(jobDetail.getKey().getName());
trigger.setKey(triggerKey);
try {
scheduler.addJob(jobDetail, true);
if (scheduler.checkExists(triggerKey)) {
scheduler.rescheduleJob(triggerKey, trigger);
} else {
scheduler.scheduleJob(trigger);
}
} catch (SchedulerException e) {
throw new IllegalArgumentException(e);
}
}
}
@Override
public void schedule(Date startTime) {
schedule(startTime, NULLDATE);
}
@Override
public void schedule(Date startTime, String group) {
schedule(startTime, NULLDATE, group);
}
@Override
public void schedule(String name, Date startTime) {
schedule(name, startTime, NULLDATE);
}
@Override
public void schedule(String name, Date startTime, String group) {
schedule(name, startTime, NULLDATE, group);
}
@Override
public void schedule(Date startTime, Date endTime) {
schedule(startTime, endTime, 0);
}
@Override
public void schedule(Date startTime, Date endTime, String group) {
schedule(startTime, endTime, 0, group);
}
@Override
public void schedule(String name, Date startTime, Date endTime) {
schedule(name, startTime, endTime, 0);
}
@Override
public void schedule(String name, Date startTime, Date endTime, String group) {
schedule(name, startTime, endTime, 0, group);
}
@Override
public void schedule(Date startTime, int repeatCount) {
schedule(null, startTime, NULLDATE, 0);
}
@Override
public void schedule(Date startTime, Date endTime, int repeatCount) {
schedule(null, startTime, endTime, 0);
}
@Override
public void schedule(Date startTime, Date endTime, int repeatCount, String group) {
schedule(null, startTime, endTime, 0, group);
}
@Override
public void schedule(String name, Date startTime, Date endTime, int repeatCount) {
schedule(name, startTime, endTime, 0, 0L);
}
@Override
public void schedule(String name, Date startTime, Date endTime, int repeatCount, String group) {
schedule(name, startTime, endTime, 0, 0L, group);
}
@Override
public void schedule(Date startTime, int repeatCount, long repeatInterval) {
schedule(null, startTime, NULLDATE, repeatCount, repeatInterval);
}
@Override
public void schedule(Date startTime, Date endTime, int repeatCount, long repeatInterval) {
schedule(null, startTime, endTime, repeatCount, repeatInterval);
}
@Override
public void schedule(Date startTime, Date endTime, int repeatCount, long repeatInterval, String group) {
schedule(null, startTime, endTime, repeatCount, repeatInterval, group);
}
@Override
public void schedule(String name, Date startTime, Date endTime, int repeatCount, long repeatInterval) {
schedule(name, startTime, endTime, repeatCount, repeatInterval, NULLSTRING);
}
@Override
public void schedule(String name, Date startTime, Date endTime, int repeatCount, long repeatInterval, String group) {
if (this.isValidExpression(startTime)) {
if (name == null || name.trim().equals("")) {
name = UUID.randomUUID().toString();
}
TriggerKey triggerKey = new TriggerKey(name, group);
SimpleTriggerImpl trigger = new SimpleTriggerImpl();
trigger.setKey(triggerKey);
trigger.setJobName(jobDetail.getKey().getName());
trigger.setStartTime(startTime);
trigger.setEndTime(endTime);
trigger.setRepeatCount(repeatCount);
trigger.setRepeatInterval(repeatInterval);
try {
scheduler.addJob(jobDetail, true);
if (scheduler.checkExists(triggerKey)) {
scheduler.rescheduleJob(triggerKey, trigger);
} else {
scheduler.scheduleJob(trigger);
}
} catch (SchedulerException e) {
throw new IllegalArgumentException(e);
}
}
}
@Override
public void pauseTrigger(String triggerName) {
pauseTrigger(triggerName, NULLSTRING);
}
@Override
public void pauseTrigger(String triggerName, String group) {
try {
scheduler.pauseTrigger(new TriggerKey(triggerName, group));// 停止触发器
} catch (SchedulerException e) {
throw new RuntimeException(e);
}
}
@Override
public void resumeTrigger(String triggerName) {
resumeTrigger(triggerName, NULLSTRING);
}
@Override
public void resumeTrigger(String triggerName, String group) {
try {
scheduler.resumeTrigger(new TriggerKey(triggerName, group));// 重启触发器
} catch (SchedulerException e) {
throw new RuntimeException(e);
}
}
@Override
public boolean removeTrigdger(String triggerName) {
return removeTrigdger(triggerName, NULLSTRING);
}
@Override
public boolean removeTrigdger(String triggerName, String group) {
TriggerKey triggerKey = new TriggerKey(triggerName, group);
try {
scheduler.pauseTrigger(triggerKey);// 停止触发器
return scheduler.unscheduleJob(triggerKey);// 移除触发器
} catch (SchedulerException e) {
throw new RuntimeException(e);
}
}
private boolean isValidExpression(final CronExpression cronExpression) {
CronTriggerImpl trigger = new CronTriggerImpl();
trigger.setCronExpression(cronExpression);
Date date = trigger.computeFirstFireTime(null);
return date != null && date.after(new Date());
}
private boolean isValidExpression(final Date startTime) {
SimpleTriggerImpl trigger = new SimpleTriggerImpl();
trigger.setStartTime(startTime);
Date date = trigger.computeFirstFireTime(null);
return date != null && date.after(new Date());
}
}
SchedulerService 只有一个多态方法schedule,SchedulerServiceImpl实现SchedulerService接口,注入 org.quartz.Schedulert和org.quartz.JobDetail,schedule方法可以动态配置 org.quartz.CronExpression或org.quartz.SimpleTrigger调度时间。
五. 实现自己的org.quartz.JobDetail
package io.platform.em.web;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.PersistJobDataAfterExecution;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.quartz.QuartzJobBean;
/**
* Created by zon 2015/8/3.
*/
@PersistJobDataAfterExecution
//@DisallowConcurrentExecution// 不允许并发执行
public class MyQuartzJobBean extends QuartzJobBean{
@Override
protected void executeInternal(JobExecutionContext jobexecutioncontext) throws JobExecutionException {
Trigger trigger = jobexecutioncontext.getTrigger();
String triggerName = trigger.getKey().getName();
io.platform.em.service.SimpleService simpleService = getApplicationContext(jobexecutioncontext).getBean("simpleService",
io.platform.em.service.SimpleService.class);
simpleService.testMethod(triggerName);
}
private ApplicationContext getApplicationContext(final JobExecutionContext jobexecutioncontext) {
try {
return (ApplicationContext) jobexecutioncontext.getScheduler().getContext().get("applicationContextKey");
} catch (SchedulerException e) {
// logger.error("jobexecutioncontext.getScheduler().getContext() error!", e);
throw new RuntimeException(e);
}
}
}
调用servicebean如下:
package io.platform.em.service;
import io.platform.util.date.DateUtils;
import org.springframework.stereotype.Service;
import java.io.Serializable;
/**
* Created by zhi on 2015/8/3.
*/
@Service("simpleService") //执行定时任务业务
public class SimpleService implements Serializable{
public void testMethod(String triggerName) {
// 这里执行定时调度业务
System.out.println("1动态执行了"+ triggerName);
System.out.println(DateUtils.getDateTime());
}
public void testMethod2(String triggerName) {
// 这里执行定时调度业务
System.out.println("2动态执行了");
System.out.println(DateUtils.getDateTime());
}
}
SimpleService主要用于定时任务执行的业务
六. 配置applicationContext-quartz.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean name="quartzScheduler"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean" autowire="no"><!-- 容器启动随机启动lazy-init="false" autowire="no"--><property name="autoStartup" value="true"/><property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="applicationContextSchedulerContextKey" value="applicationContextKey"/>
<property name="waitForJobsToCompleteOnShutdown" value="true"/>
<property name="configLocation" value="classpath:quartz.properties"/>
</bean>
<bean id="jobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<property name="jobClass">
<value>io.platform.em.web.MyQuartzJobBean</value>
</property>
<property name="durability" value="true"/>
</bean>
</beans>
七. quartz中数据源配置quartz.properties (参数含义请参照Quartz_Job+Scheduling_Framework)
org.quartz.scheduler.instanceName = DefaultQuartzScheduler
org.quartz.scheduler.rmi.export = false
org.quartz.scheduler.rmi.proxy = false
org.quartz.scheduler.wrapJobExecutionInUserTransaction = false
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
#per JobStoreTX ram RAMJobStore
#org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
# StdJDBCDelegate tongyong
#org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.HSQLDBDelegate
org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.StdJDBCDelegate
org.quartz.jobStore.useProperties = false
org.quartz.jobStore.tablePrefix = QRTZ_
org.quartz.jobStore.isClustered = false
org.quartz.jobStore.maxMisfiresToHandleAtATime= 20000
org.quartz.jobStore.dataSource = myDS
org.quartz.dataSource.myDS.driver=com.mysql.jdbc.Driver
org.quartz.dataSource.myDS.URL=jdbc:mysql://*.*.*.*:3306/*?createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=utf-8
org.quartz.dataSource.myDS.user=root
org.quartz.dataSource.myDS.password=root
八。测试类
package io.platform.test;
/**
* Created by zh on 2015/8/4.
*/
import io.platform.em.service.SchedulerService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.text.SimpleDateFormat;
import java.util.Date;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath*:/applicationContext.xml", "classpath*:/applicationContext-shiro.xml","classpath*:/application-quartz.xml"})
@ActiveProfiles("production")
public class TestQuartz {
private static Logger LOGGER = LoggerFactory.getLogger(Test.class);
private static String JOB_GROUP_NAME = "zydGroup";
private static String TRIGGER_GROUP_NAME = "zydTriggerGroup";
private static SchedulerFactory sf = new StdSchedulerFactory();
@Autowired
private SchedulerService schedulerService;
@Test
public void test() throws SchedulerException {//,Exception,ParseException
//schedule ("", Date startTime, Date endTime,int repeatCount, long repeatInterval, String group)
Date startTime = parse("2015-08-05 13:55:00");
Date endTime = parse("2015-08-05 15:15:00");
// schedulerService.resumeTrigger("f64eee37-62f4-473f-b5b9-d7a76d86c443");
schedulerService.schedule("0 28 17 * * ? *");
// schedulerService.schedule("f64eee37-62f4-473f-b5b9-d7a76d86c443", "0 45 09 * * ? *");
//schedulerService.pauseTrigger("f64eee37-62f4-473f-b5b9-d7a76d86c443");
//schedulerService.resumeTrigger("0fdb7134-9701-494c-9879-bbf2e30120b8");
// schedulerService.schedule("0 17 16 * * ? *");
//schedulerService.schedule(startTime);
// schedulerService.pauseTrigger("f4a610a7-5b43-4738-949c-150eec96fb62");
// schedulerService.schedule(startTime, endTime,5,5);
// schedulerService.schedule("ZYD", startTime, endTime, 1, 10);
// schedulerService.schedule(startTime);
// schedulerService.removeTrigdger("f4a610a7-5b43-4738-949c-150eec96fb62");
int repeatCount = 10;
long repeatInterval = 2;
// String name="b870cf9b-c313-4fd6-87ad-e99de7580290";
// schedulerService.schedule(name, startTime, endTime, repeatCount, repeatInterval);
/* schedulerService.schedule("0/10 * * ? * * *");
// 2014-08-19 16:33:00开始执行调度
schedulerService.schedule(startTime);
// 2014-08-19 16:33:00开始执行调度,2014-08-22 21:10:00结束执行调试
schedulerService.schedule(startTime, endTime);
// 2014-08-19 16:33:00开始执行调度,执行5次结束
schedulerService.schedule(startTime, 5);*/
// 2014-08-19 16:33:00开始执行调度,每隔20秒执行一次,执行5次结束
// schedulerService.schedule(startTime, 5, 20);
}
/*
try {
LOGGER.info("33333333333");
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
LOGGER.info("Scheduler starting up...");
System.out.print("4444444444");
scheduler.start();
} catch (Exception exception) {
System.out.print(exception);
}
schedulerService.schedule("0/2 * * ? * * *");
Date startTime = parse("2015-08-05 10:15:00");
Date endTime = parse("2015-08-05 15:15:00");
int repeatCount=10;
long repeatInterval=2;
// String name="b870cf9b-c313-4fd6-87ad-e99de7580290";
// schedulerService.schedule(name, startTime, endTime, repeatCount, repeatInterval);
schedulerService.schedule("0/10 * * ? * * *");
// 2014-08-19 16:33:00开始执行调度
schedulerService.schedule(startTime);
// 2014-08-19 16:33:00开始执行调度,2014-08-22 21:10:00结束执行调试
schedulerService.schedule(startTime, endTime);
// 2014-08-19 16:33:00开始执行调度,执行5次结束
schedulerService.schedule(startTime, 5);
// 2014-08-19 16:33:00开始执行调度,每隔20秒执行一次,执行5次结束
schedulerService.schedule(startTime, 5, 20);
*/
// schedulerService.schedule("quartz1", "ZYD", "0/2 * * ? * * *");
private static Date parse(String dateStr) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
return format.parse(dateStr);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
查看数据库表运行结果,可按实际需要启动停止trigger.
相关文章推荐
- SpringMVC 的<mvc:resources>使用映射路径展示文件服务器上的图片
- java自定义注解以及注解的简单介绍
- Default DispatcherServlet Configuration
- JAVA实现Excel——Excel单元格设计
- Java容器
- Java基础-IO-其它流(笔记)
- 使用JavaMail连接带有TLS的SMTP
- Workspace defines a VM that does not contain a valid jre/lib/rt.jar: C:\Program Files\Java\jre7
- JAVA 中两种判断输入的是否是数字的方法__正则化_
- JAVA Zero Copy
- Java基础-IO-字符输入输出流(笔记)
- java基础教程-多线程编程
- Spring3.x和Hibernate4的整合问题
- JAVA 同步器CountDownLatch CyclicBarrier
- Java基础-IO-字节输入输出流(笔记)
- 高效率的文件复制的Java代码
- JAVA8新特性
- JAVA8新特性
- 用eclipse联合adb截取android设备的log
- 编码剖析Spring依赖注入的原理