文档章节

quartz2的example3--CronTriggerExample

独钓渔
 独钓渔
发布于 2016/11/04 17:18
字数 546
阅读 47
收藏 1

 

SimpleJob:

package com.imddy.test.quartz2.example3;


import java.util.Date;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SimpleJob implements Job {
	
	private static Logger logger = LoggerFactory.getLogger(SimpleJob.class);

	public SimpleJob() {
	}

	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
		// TODO Auto-generated method stub
		JobKey jobKey = context.getJobDetail().getKey();
		logger.info("SimpleJob : " + jobKey + "工作时间在:" + new Date() );
	}

}

 

CronTriggerExample :

package com.imddy.test.quartz2.example3;

import static org.quartz.CronScheduleBuilder.cronSchedule;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;

import java.util.Date;

import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CronTriggerExample {

	private void run() throws SchedulerException {
		// TODO Auto-generated method stub
		Logger log = LoggerFactory.getLogger(CronTriggerExample.class);
		
		log.info("------- Initializing -------------------");
		SchedulerFactory sf = new StdSchedulerFactory();
		Scheduler scheduler = sf.getScheduler();
		log.info("------- Initialization Complete --------");

	    log.info("------- Scheduling Jobs ----------------");
	    // job 1 will run every 20 seconds
	    JobDetail job = newJob(SimpleJob.class).withIdentity("job1", "group1").build();
	    CronTrigger trigger = newTrigger().withIdentity("trigger1", "group1").withSchedule(cronSchedule("0/20 * * * * ?")).build();
		
	    Date ft = scheduler.scheduleJob(job, trigger);
	    log.info(job.getKey() + " has been scheduled to run at: " + ft + " and repeat based on expression: "
	             + trigger.getCronExpression());
	    
	    
	    // job 2 will run every other minute (at 15 seconds past the minute)
	    job = newJob(SimpleJob.class).withIdentity("job2", "group1").build();

	    trigger = newTrigger().withIdentity("trigger2", "group1").withSchedule(cronSchedule("15 0/2 * * * ?")).build();

	    ft = scheduler.scheduleJob(job, trigger);
	    log.info(job.getKey() + " has been scheduled to run at: " + ft + " and repeat based on expression: "
	             + trigger.getCronExpression());

	    // job 3 will run every other minute but only between 8am and 5pm
	    job = newJob(SimpleJob.class).withIdentity("job3", "group1").build();

	    trigger = newTrigger().withIdentity("trigger3", "group1").withSchedule(cronSchedule("0 0/2 8-17 * * ?")).build();

	    ft = scheduler.scheduleJob(job, trigger);
	    log.info(job.getKey() + " has been scheduled to run at: " + ft + " and repeat based on expression: "
	             + trigger.getCronExpression());

	    // job 4 will run every three minutes but only between 5pm and 11pm
	    job = newJob(SimpleJob.class).withIdentity("job4", "group1").build();

	    trigger = newTrigger().withIdentity("trigger4", "group1").withSchedule(cronSchedule("0 0/3 17-23 * * ?")).build();

	    ft = scheduler.scheduleJob(job, trigger);
	    log.info(job.getKey() + " has been scheduled to run at: " + ft + " and repeat based on expression: "
	             + trigger.getCronExpression());

	    // job 5 will run at 10am on the 1st and 15th days of the month
	    job = newJob(SimpleJob.class).withIdentity("job5", "group1").build();

	    trigger = newTrigger().withIdentity("trigger5", "group1").withSchedule(cronSchedule("0 0 10am 1,15 * ?")).build();

	    ft = scheduler.scheduleJob(job, trigger);
	    log.info(job.getKey() + " has been scheduled to run at: " + ft + " and repeat based on expression: "
	             + trigger.getCronExpression());

	    // job 6 will run every 30 seconds but only on Weekdays (Monday through Friday)
	    job = newJob(SimpleJob.class).withIdentity("job6", "group1").build();

	    trigger = newTrigger().withIdentity("trigger6", "group1").withSchedule(cronSchedule("0,30 * * ? * MON-FRI"))
	        .build();

	    ft = scheduler.scheduleJob(job, trigger);
	    log.info(job.getKey() + " has been scheduled to run at: " + ft + " and repeat based on expression: "
	             + trigger.getCronExpression());

	    // job 7 will run every 30 seconds but only on Weekends (Saturday and Sunday)
	    job = newJob(SimpleJob.class).withIdentity("job7", "group1").build();

	    trigger = newTrigger().withIdentity("trigger7", "group1").withSchedule(cronSchedule("0,30 * * ? * SAT,SUN"))
	        .build();

	    ft = scheduler.scheduleJob(job, trigger);
	    log.info(job.getKey() + " has been scheduled to run at: " + ft + " and repeat based on expression: "
	             + trigger.getCronExpression());
	    
	    
	    
	    
	    
	    log.info("------- Starting Scheduler ----------------");

	    // All of the jobs have been added to the scheduler, but none of the
	    // jobs
	    // will run until the scheduler has been started
	    scheduler.start();

	    log.info("------- Started Scheduler -----------------");

	    log.info("------- Waiting five minutes... ------------");
	    try {
	      // wait five minutes to show jobs
	      Thread.sleep(300L * 1000L);
	      // executing...
	    } catch (Exception e) {
	      //
	    }

	    log.info("------- Shutting Down ---------------------");

	    scheduler.shutdown(true);

	    log.info("------- Shutdown Complete -----------------");
	}
	
	
	
	
	public static void main(String[] args) throws SchedulerException {
		CronTriggerExample cronTriggerExample = new CronTriggerExample();
		cronTriggerExample.run();
	}


}

 

 

 

 

 

 

 

© 著作权归作者所有

独钓渔
粉丝 48
博文 389
码字总数 148158
作品 0
沙坪坝
系统管理员
私信 提问
quartz2.2 官方例子3

quartz 官方例子3 所需jar: quartz-2.2.0.jar quartz-jobs-2.2.0.jar slf4j-api-1.7.5.jar slf4j-log4j12-1.7.5.jar log4j-1.2.16.jar quartz.properties 配置: org.quartz.scheduler.inst......

fanson59
2013/08/14
0
0
quartz2的example2

quartz2的example2 simplejob: SimpleTiggerExample 同一个job任务,可以建立多tigger调度

独钓渔
2016/11/02
8
0
quartz 1.6 如何判断job是否存在

quartz2 可以利用checkexists(JobKey jobKey) 。 但是在quartz1.6版本中怎么判断job是否存在呢?

zhitang
2014/07/04
560
2
quartz2的example1

quartz2的example1 http://www.quartz-scheduler.org/documentation/quartz-2.2.x/examples/ 先写任务job HelloJob 再写任务调度demo 执行如下...

独钓渔
2016/11/02
5
0
spring定时任务包Quartz版本不兼容

使用spring定时任务包Quartz时,必须使用Quartz1.85以下版本的。 查看发现spring3.0.5中org.springframework.scheduling.quartz.CronTriggerBean继承了 org.quartz.CronTrigger(public clas......

-wangming-
2015/05/22
0
0

没有更多内容

加载失败,请刷新页面

加载更多

0.01-Win10安装linux子系统

一、安装Debian子系统 -1、控制面板设置: -1.1、打开“控制面板” —— “程序” —— “启用或关闭Windows功能” —— 勾选 “适用于Linux的Windows子系统” -2、设置: -2.1、打开“设置”...

静以修身2025
昨天
2
0
init 0-6 (启动级别:init 0,1,2,3,4,5,6)

启动级别: init 0,1,2,3,4,5,6 这是个很久的知识点了,只是自己一直都迷迷糊糊的,今天在翻出来好好理解下。。 0: 停机 1:单用户形式,只root进行维护 2:多用户,不能使用net file system...

圣洁之子
昨天
2
0
Android Camera HAL浅析

1、Camera成像原理介绍 Camera工作流程图 Camera的成像原理可以简单概括如下: 景物(SCENE)通过镜头(LENS)生成的光学图像投射到图像传感器(Sensor)表面上,然后转为电信号,经过A/D(模数转...

天王盖地虎626
昨天
2
0
聊聊Elasticsearch的ProcessProbe

序 本文主要研究一下Elasticsearch的ProcessProbe ProcessProbe elasticsearch-7.0.1/server/src/main/java/org/elasticsearch/monitor/process/ProcessProbe.java public class ProcessProb......

go4it
昨天
3
0
mysql PL(procedure language)流程控制语句

在MySQL中,常见的过程式SQL语句可以用在存储体中。其中包括IF语句、CASE语句、LOOP语句、WHILE语句、ITERATE语句和LEAVE语句,它们可以进行流程控制。 IF语句相当于Java中的if()...else if(...

edison_kwok
昨天
2
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部