文档章节

quartz2的example2

独钓渔
 独钓渔
发布于 2016/11/02 16:00
字数 687
阅读 11
收藏 1

quartz2的example2

simplejob:

package com.imddy.test.quartz2.example2;

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() );
	}

}

 

 

SimpleTiggerExample

package com.imddy.test.quartz2.example2;

import static org.quartz.DateBuilder.futureDate;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.JobKey.jobKey;
import static org.quartz.TriggerBuilder.newTrigger;
import static org.quartz.SimpleScheduleBuilder.simpleSchedule;

import java.util.Date;

import org.quartz.DateBuilder;
import org.quartz.DateBuilder.IntervalUnit;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SchedulerMetaData;
import org.quartz.SimpleTrigger;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SimpleTriggerExample {
	
	private static Logger log = LoggerFactory.getLogger(SimpleTriggerExample.class);
	

	public SimpleTriggerExample() {
		
	}
	
	public void run() throws SchedulerException {

		log.info("------- Initializing -------------------");

	    // First we must get a reference to a scheduler
	    SchedulerFactory sf = new StdSchedulerFactory();
	    Scheduler sched = sf.getScheduler();
	    
	    log.info("------- Scheduling Jobs ----------------");
	    
	    Date startTime = DateBuilder.nextGivenSecondDate(null, 15);
	    	    
	    JobDetail job = newJob(SimpleJob.class).withIdentity("job1", "group1").build();
	    SimpleTrigger trigger = (SimpleTrigger) newTrigger().withIdentity("trigger1", "group1").startAt(startTime).build();
	    
	    Date ft = sched.scheduleJob(job, trigger);
	    
	    log.info(job.getKey() + " will run at: " + ft + " and repeat: " + trigger.getRepeatCount() + " times, every "
	             + trigger.getRepeatInterval() / 1000 + " seconds");
	    
	    job = newJob(SimpleJob.class).withIdentity("job2", "group1").build();
	    trigger = (SimpleTrigger) newTrigger().withIdentity("trigger2", "group1").startAt(startTime).build();
	    
	    ft = sched.scheduleJob(job, trigger);
	    
	    log.info(job.getKey() + " will run at: " + ft + " and repeat: " + trigger.getRepeatCount() + " times, every "
	             + trigger.getRepeatInterval() / 1000 + " seconds");
	    
	    job = newJob(SimpleJob.class).withIdentity("job3", "group1").build();

	    trigger = newTrigger().withIdentity("trigger3", "group1").startAt(startTime)
	        .withSchedule(simpleSchedule().withIntervalInSeconds(10).withRepeatCount(10)).build();

	    ft = sched.scheduleJob(job, trigger);
	    log.info(job.getKey() + " will run at: " + ft + " and repeat: " + trigger.getRepeatCount() + " times, every "
	             + trigger.getRepeatInterval() / 1000 + " seconds");
	    
	    
	    
	    trigger = newTrigger().withIdentity("trigger3", "group2").startAt(startTime)
	            .withSchedule(simpleSchedule().withIntervalInSeconds(10).withRepeatCount(2)).forJob(job).build();

	        ft = sched.scheduleJob(trigger);
	        log.info(job.getKey() + " will [also] run at: " + ft + " and repeat: " + trigger.getRepeatCount()
	                 + " times, every " + trigger.getRepeatInterval() / 1000 + " seconds");

	        // job4 will run 6 times (run once and repeat 5 more times)
	        // job4 will repeat every 10 seconds
	        job = newJob(SimpleJob.class).withIdentity("job4", "group1").build();

	        trigger = newTrigger().withIdentity("trigger4", "group1").startAt(startTime)
	            .withSchedule(simpleSchedule().withIntervalInSeconds(10).withRepeatCount(5)).build();

	        ft = sched.scheduleJob(job, trigger);
	        log.info(job.getKey() + " will run at: " + ft + " and repeat: " + trigger.getRepeatCount() + " times, every "
	                 + trigger.getRepeatInterval() / 1000 + " seconds");

	        // job5 will run once, five minutes in the future
	        job = newJob(SimpleJob.class).withIdentity("job5", "group1").build();

	        trigger = (SimpleTrigger) newTrigger().withIdentity("trigger5", "group1")
	            .startAt(futureDate(5, IntervalUnit.MINUTE)).build();

	        ft = sched.scheduleJob(job, trigger);
	        log.info(job.getKey() + " will run at: " + ft + " and repeat: " + trigger.getRepeatCount() + " times, every "
	                 + trigger.getRepeatInterval() / 1000 + " seconds");

	        // job6 will run indefinitely, every 40 seconds
	        job = newJob(SimpleJob.class).withIdentity("job6", "group1").build();

	        trigger = newTrigger().withIdentity("trigger6", "group1").startAt(startTime)
	            .withSchedule(simpleSchedule().withIntervalInSeconds(40).repeatForever()).build();

	        ft = sched.scheduleJob(job, trigger);
	        log.info(job.getKey() + " will run at: " + ft + " and repeat: " + trigger.getRepeatCount() + " times, every "
	                 + trigger.getRepeatInterval() / 1000 + " seconds");
	        
	        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
	        sched.start();

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

	        // jobs can also be scheduled after start() has been called...
	        // job7 will repeat 20 times, repeat every five minutes
	        job = newJob(SimpleJob.class).withIdentity("job7", "group1").build();

	        trigger = newTrigger().withIdentity("trigger7", "group1").startAt(startTime)
	            .withSchedule(simpleSchedule().withIntervalInMinutes(5).withRepeatCount(20)).build();

	        ft = sched.scheduleJob(job, trigger);
	        log.info(job.getKey() + " will run at: " + ft + " and repeat: " + trigger.getRepeatCount() + " times, every "
	                 + trigger.getRepeatInterval() / 1000 + " seconds");

	        // jobs can be fired directly... (rather than waiting for a trigger)
	        job = newJob(SimpleJob.class).withIdentity("job8", "group1").storeDurably().build();

	        sched.addJob(job, true);

	        log.info("'Manually' triggering job8...");
	        sched.triggerJob(jobKey("job8", "group1"));

	        log.info("------- Waiting 30 seconds... --------------");

	        try {
	          // wait 33 seconds to show jobs
	          Thread.sleep(30L * 1000L);
	          // executing...
	        } catch (Exception e) {
	          //
	        }

	        // jobs can be re-scheduled...
	        // job 7 will run immediately and repeat 10 times for every second
	        log.info("------- Rescheduling... --------------------");
	        trigger = newTrigger().withIdentity("trigger7", "group1").startAt(startTime)
	            .withSchedule(simpleSchedule().withIntervalInMinutes(5).withRepeatCount(20)).build();

	        ft = sched.rescheduleJob(trigger.getKey(), trigger);
	        log.info("job7 rescheduled to run at: " + ft);

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

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

	        sched.shutdown(true);

	        log.info("------- Shutdown Complete -----------------");

	        // display some stats about the schedule that just ran
	        SchedulerMetaData metaData = sched.getMetaData();
	        log.info("Executed " + metaData.getNumberOfJobsExecuted() + " jobs.");
		
	}


	public static void main(String[] args) throws SchedulerException {
		SimpleTriggerExample example = new SimpleTriggerExample();
	    example.run();

	}

}

 

同一个job任务,可以建立多tigger调度

 

 

 

© 著作权归作者所有

独钓渔
粉丝 48
博文 389
码字总数 148158
作品 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
maven下整合spring和quartz

最近在研究Spring中的定时任务功能,最好的办法当然是使用Quartz来实现。对于一个新手来说,花了我不少时间,这里我写个笔记,给大家参考。 我使用的是Maven来管理项目,需要的Jar包我给大家...

明舞
2014/07/28
0
4
opencv菜鸟求问android的opencv编译方法

如题,如《学习opencv》书里的打开摄像机的代码 [code][/code]#include "cv.h" #include "highgui.h" int main( int argc, char** argv ) { cvNamedWindow( "Example2_9", CV_WINDOW_AUTOSIZ......

Isablanca
2013/03/02
1K
1

没有更多内容

加载失败,请刷新页面

加载更多

重定向与转发,绝对路径

/** 1. 请求和响应 * 请求:客户端向服务器 * 类型:HttpServletRequest request * 定义:代表了客户端向服务器发送的请求报文,该对象由服务器(web容器|Servlet容器)...

architect刘源源
36分钟前
2
0
node后端使用jwt实现跨域认证--生成token、验证token是否无效和过期

JSON Web Token(缩写 JWT)是目前最流行的跨域认证解决方案 引入jwt var jwt = require('jsonwebtoken'); 生成token 在登录时,如果用户名和密码正确,则使用jwt生成token,返回给前端 le...

祖达
49分钟前
3
0
Oauth2 单点登陆

少年已不再年少
55分钟前
3
0
怎么做到有效沟通?

关于有效沟通,用下面两个场景来讲述一下: 情景1: 开发A:tickets库连不上,帮忙看一下。 运维:tikets库?国内、国外?什么环境?(测试、线上) 开发A:国外、测试 运维:在哪连接的?(...

阿dai学长
56分钟前
3
0
idea如何部署tomcat

最近在学习使用idea,有很多不习惯,这里记录一下tomcat的部署。 首先需要借助工具栏,需要在view(视图)中勾选工具栏和工具按钮,如下图: 然后点击选择[运行/调试]配置的按钮,如图所示: ...

我叫小糖主
今天
2
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部