通过quartz实现任务调度
quartz是java的一个作业调度框架,可以轻松实现定时任务
quartz由以下几个部分构成:
job:在execute方法中定义需要完成的任务,context提柜调度上下文的信息
jobdetail:接受job实现类,实例化job
trigger:提供触发规则
calendar:与trigger关联,代表日期集合
scheduler:提供工厂,执行整个定时任务
第一步:加依赖
<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>
第二步:
Job实例:
public class DealDataJob implements Job{
private static final Logger log = Logger.getLogger(DealDataJob.class);
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
log.info("--------------");
log.info("预处理数据...");
RecordDao recordDao = new RecordDao();
recordDao.preDealData();
recordDao = null;
log.info(new Date());
log.info("处理数据结束");
log.info("--------------");
}
}
配置Scheduler:
public class QuartzManage {
private static final Logger log = Logger.getLogger(QuartzManage.class);
private static final String JOB_NAME = "deal_data";
private static final String GROUP_NAME = "group";
private static final String TRIGGER_NAME = "daily";
public static void manageJob() {
try {
JobDetail jobDetail = configJobDetail();
CronTrigger cronTrigger = configTrigger();
Scheduler scheduler = configScheduler(jobDetail, cronTrigger);
scheduler.start();
} catch (Exception e) {
e.printStackTrace();
log.info("manage failed");
log.error(e.getMessage());
}
}
/**
* 配置JobDetail
* @return
*/
private static JobDetail configJobDetail(){
JobDetailImpl jobDetail = new JobDetailImpl();
jobDetail.setName(JOB_NAME);
jobDetail.setGroup(GROUP_NAME);
jobDetail.setJobClass(DealDataJob.class);
return jobDetail;
}
/**
* 配置调度时间点Trigger
* @return
* @throws ParseException
*/
private static CronTrigger configTrigger() throws ParseException{
CronTriggerImpl cronTrigger = new CronTriggerImpl();
cronTrigger.setName(TRIGGER_NAME);
cronTrigger.setGroup(GROUP_NAME);
CronExpression expression = new CronExpression("0 0 6 * * ? *"); //每天早上六点触发操作
cronTrigger.setCronExpression(expression);
return cronTrigger;
}
/**
* 配置运行容器,并将工作和调度时间注册到运行容器中
* @param jobDetail
* @param trigger
* @return
* @throws SchedulerException
*/
private static Scheduler configScheduler(JobDetail jobDetail,Trigger trigger)
throws SchedulerException{
SchedulerFactory schedulerFactory = new StdSchedulerFactory();
Scheduler scheduler = schedulerFactory.getScheduler();
scheduler.scheduleJob(jobDetail, trigger);
return scheduler;
}
public static void main(String[]args){
manageJob();
}
第三步:在javaweb中需要定义Listener
public class ContextListener implements ServletContextListener{
@Override
public void contextDestroyed(ServletContextEvent sce) {
// TODO Auto-generated method stub
System.out.println("context destroyed");
}
@Override
public void contextInitialized(ServletContextEvent sce) {
// TODO Auto-generated method stub
System.out.println("context initialized");
QuartzManage.manageJob();
}
}
第四步:配置listener
<listener>
<listener-class>
com.bydota.product.listener.ContextListener
</listener-class>
</listener>