要使用Quartz,必须要引入以下这几个包:
1、log4j-1.2.16
2、quartz-2.1.7
3、slf4j-api-1.6.1.jar
4、slf4j-log4j12-1.6.1.jar
这些包都在下载的Quartz包里面包含着,因此没有必要为寻找这几个包而头疼。
第二步:创建要被定执行的任务类
这一步也很简单,只需要创建一个实现了org.quartz.Job接口的类,并实现这个接口的唯一一个方法execute(JobExecutionContext arg0) throws JobExecutionException即可。如:
import java.text.SimpleDateFormat
import java.util.Date
import org.quartz.Job
import org.quartz.JobExecutionContext
import org.quartz.JobExecutionException
public class myJob implements Job {
@Override
public void execute(JobExecutionContext arg0) throws JobExecutionException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS")
System.out.println(sdf.format(new Date()))
}
}
import java.text.SimpleDateFormat
import java.util.Date
import org.quartz.Job
import org.quartz.JobExecutionContext
import org.quartz.JobExecutionException
public class myJob implements Job {
@Override
public void execute(JobExecutionContext arg0) throws JobExecutionException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS")
System.out.println(sdf.format(new Date()))
}
}
这个例子很简单,就不用解说了。
第三步:创建任务调度,并执行
这一步应该算是最难的一步的,但其实是非常简单的,直接上代码
import static org.quartz.CronScheduleBuilder.cronSchedule
import static org.quartz.JobBuilder.newJob
import static org.quartz.TriggerBuilder.newTrigger
import java.text.SimpleDateFormat
import java.util.Date
import org.quartz.CronTrigger
import org.quartz.JobDetail
import org.quartz.Scheduler
import org.quartz.SchedulerFactory
import org.quartz.impl.StdSchedulerFactory
public class Test {
public void go() throws Exception {
// 首先,必需要取得一个Scheduler的引用
SchedulerFactory sf = new StdSchedulerFactory()
Scheduler sched = sf.getScheduler()
//jobs可以在scheduled的sched.start()方法前被调用
//job 1将每隔20秒执行一次
JobDetail job = newJob(myJob.class).withIdentity("job1", "group1").build()
CronTrigger trigger = newTrigger().withIdentity("trigger1", "group1").withSchedule(cronSchedule("0/20 * * * * ?")).build()
Date ft = sched.scheduleJob(job, trigger)
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS")
System.out.println(job.getKey() + " 已被安排执行于: " + sdf.format(ft) + ",并且以如下重复规则重复执行: " + trigger.getCronExpression())
// job 2将每2分钟执行一次(在该分钟的第15秒)
job = newJob(myJob.class).withIdentity("job2", "group1").build()
trigger = newTrigger().withIdentity("trigger2", "group1").withSchedule(cronSchedule("15 0/2 * * * ?")).build()
ft = sched.scheduleJob(job, trigger)
System.out.println(job.getKey() + " 已被安排执行于: " + sdf.format(ft) + ",并且以如下重复规则重复执行: "+ trigger.getCronExpression())
// 开始执行,start()方法被调用后,计时器就开始工作,计时调度中允许放入N个Job
sched.start()
try {
//主线程等待一分钟
Thread.sleep(60L * 1000L)
} catch (Exception e) {}
//关闭定时调度,定时器不再工作
sched.shutdown(true)
}
public static void main(String[] args) throws Exception {
Test test = new Test()
test.go()
}
}
import static org.quartz.CronScheduleBuilder.cronSchedule
import static org.quartz.JobBuilder.newJob
import static org.quartz.TriggerBuilder.newTrigger
import java.text.SimpleDateFormat
import java.util.Date
import org.quartz.CronTrigger
import org.quartz.JobDetail
import org.quartz.Scheduler
import org.quartz.SchedulerFactory
import org.quartz.impl.StdSchedulerFactory
public class Test {
public void go() throws Exception {
// 首先,必需要取得一个Scheduler的引用
SchedulerFactory sf = new StdSchedulerFactory()
Scheduler sched = sf.getScheduler()
//jobs可以在scheduled的sched.start()方法前被调用
//job 1将每隔20秒执行一次
JobDetail job = newJob(myJob.class).withIdentity("job1", "group1").build()
CronTrigger trigger = newTrigger().withIdentity("trigger1", "group1").withSchedule(cronSchedule("0/20 * * * * ?")).build()
Date ft = sched.scheduleJob(job, trigger)
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS")
System.out.println(job.getKey() + " 已被安排执行于: " + sdf.format(ft) + ",并且以如下重复规则重复执行: " + trigger.getCronExpression())
// job 2将每2分钟执行一次(在该分钟的第15秒)
job = newJob(myJob.class).withIdentity("job2", "group1").build()
trigger = newTrigger().withIdentity("trigger2", "group1").withSchedule(cronSchedule("15 0/2 * * * ?")).build()
ft = sched.scheduleJob(job, trigger)
System.out.println(job.getKey() + " 已被安排执行于: " + sdf.format(ft) + ",并且以如下重复规则重复执行: "+ trigger.getCronExpression())
// 开始执行,start()方法被调用后,计时器就开始工作,计时调度中允许放入N个Job
sched.start()
try {
//主线程等待一分钟
Thread.sleep(60L * 1000L)
} catch (Exception e) {}
//关闭定时调度,定时器不再工作
sched.shutdown(true)
}
public static void main(String[] args) throws Exception {
Test test = new Test()
test.go()
}
}
OK了,Job1和Job2就会被安排为定时执行了。此时程序是可以执行的了,但是可能会输出WARN级别日志,这是因为没有加log4j的配置文件,加上配置文件,就OK了。这里需要说明的地方只有一个,其它的可以直接Copy到您的项目里面。看代码:
CronTrigger trigger = newTrigger().withIdentity("trigger1", "group1").withSchedule(cronSchedule("0/20 * * * * ?")).build()
CronTrigger trigger = newTrigger().withIdentity("trigger1", "group1").withSchedule(cronSchedule("0/20 * * * * ?")).build()
在java中使用quartz/** *//**
* 添加一个定时任务,使用默认的任务组名,触发器名,触发器组名
* @param jobName 任务名
* @param job 任务
* @param time时间设置,参考quartz说明文档
* @throws SchedulerException
* @throws ParseException
*/
public static void addJob(String jobName,Job job,String time)
throws SchedulerException, ParseException{
Scheduler sched = sf.getScheduler()
JobDetail jobDetail = new JobDetail(jobName, JOB_GROUP_NAME, job.getClass())//任务名,任务组,任务执行类
//触发器
CronTrigger trigger =
new CronTrigger(jobName, TRIGGER_GROUP_NAME)//触发器名,触发器组
trigger.setCronExpression(time)//触发器时间设定
sched.scheduleJob(jobDetail,trigger)
//启动
if(!sched.isShutdown())
sched.start()
}
/** *//**
* 添加一个定时任务
* @param jobName 任务名
* @param jobGroupName 任务组名
* @param triggerName 触发器名
* @param triggerGroupName 触发器组名
* @param job 任务
* @param time时间设置,参考quartz说明文档
* @throws SchedulerException
* @throws ParseException
*/
public static void addJob(String jobName,String jobGroupName,
String triggerName,String triggerGroupName,
Job job,String time)
throws SchedulerException, ParseException{
Scheduler sched = sf.getScheduler()
JobDetail jobDetail = new JobDetail(jobName, jobGroupName, job.getClass())//任务名,任务组,任务执行类
//触发器
CronTrigger trigger =
new CronTrigger(triggerName, triggerGroupName)//触发器名,触发器组
trigger.setCronExpression(time)//触发器时间设定
sched.scheduleJob(jobDetail,trigger)
if(!sched.isShutdown())
sched.start()
}
/** *//**
* 修改一个任务的触发时间(使用默认的任务组名,触发器名,触发器组名)
* @param jobName
* @param time
* @throws SchedulerException
* @throws ParseException
*/
public static void modifyJobTime(String jobName,String time)
throws SchedulerException, ParseException{
Scheduler sched = sf.getScheduler()
Trigger trigger = sched.getTrigger(jobName,TRIGGER_GROUP_NAME)
if(trigger != null){
CronTrigger ct = (CronTrigger)trigger
ct.setCronExpression(time)
sched.resumeTrigger(jobName,TRIGGER_GROUP_NAME)
}
}
/** *//**
* 修改一个任务的触发时间
* @param triggerName
* @param triggerGroupName
* @param time
* @throws SchedulerException
* @throws ParseException
*/
public static void modifyJobTime(String triggerName,String triggerGroupName,
String time)
throws SchedulerException, ParseException{
Scheduler sched = sf.getScheduler()
Trigger trigger = sched.getTrigger(triggerName,triggerGroupName)
if(trigger != null){
CronTrigger ct = (CronTrigger)trigger
//修改时间
ct.setCronExpression(time)
//重启触发器
sched.resumeTrigger(triggerName,triggerGroupName)
}
}
/** *//**
* 移除一个任务(使用默认的任务组名,触发器名,触发器组名)
* @param jobName
* @throws SchedulerException
*/
public static void removeJob(String jobName)
throws SchedulerException{
Scheduler sched = sf.getScheduler()
sched.pauseTrigger(jobName,TRIGGER_GROUP_NAME)//停止触发器
sched.unscheduleJob(jobName,TRIGGER_GROUP_NAME)//移除触发器
sched.deleteJob(jobName,JOB_GROUP_NAME)//删除任务
}
/** *//**
* 移除一个任务
* @param jobName
* @param jobGroupName
* @param triggerName
* @param triggerGroupName
* @throws SchedulerException
*/
public static void removeJob(String jobName,String jobGroupName,
String triggerName,String triggerGroupName)
throws SchedulerException{
Scheduler sched = sf.getScheduler()
sched.pauseTrigger(triggerName,triggerGroupName)//停止触发器
sched.unscheduleJob(triggerName,triggerGroupName)//移除触发器
sched.deleteJob(jobName,jobGroupName)//删除任务
}
这么写就可以了,使用spring的定时任务实现很简单,代码如下:
import org.springframework.scheduling.annotation.Scheduledimport org.springframework.stereotype.Component
@Component
public class ScheduleTask {
@Scheduled(cron = "0 0/5 8-19 * * ? *")
public void doSomething() {
// TODO
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)