QUARTZ
【小城贝尔】
定时调度更高级,开源组织在雄起。
初学抓瞎难自己,多看案例勿放弃。
quartz:石英
下载网址:http://www.quartz-scheduler.org/
// 需要定时运行的资源
public class QUARTZB implements Job {
public void execute(JobExecutionContext context)
throws JobExecutionException {
for (int i = 0 ; i < 10; i ++){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("我是内部类 i am learning QUARTZ......."+i);
}
}
}
SimpleExample {
public void run() throws Exception {
//首先根据工厂获得调度
// First we must get a reference to a scheduler
SchedulerFactory sf = new StdSchedulerFactory();
Scheduler sched = sf.getScheduler();
//任务调度运行的时间
// computer a time that is on the next round minute
Date runTime = evenMinuteDate(new Date());
//将时间改到五秒后运行
runTime = new Date(System.currentTimeMillis() + 1000*5);
//需要运行的任务
// define the job and tie it to our HelloJob class
JobDetail job = newJob(QUARTZB.class).withIdentity("job2", "group2").build();
//触发器 触发在指定的时间运行
// Trigger the job to run on the next round minute
Trigger trigger = newTrigger().withIdentity("trigger2", "group2").startAt(runTime).build();
// Tell quartz to schedule the job using our trigger
sched.scheduleJob(job, trigger);
//调度执行
// Start up the scheduler (nothing can actually run until the
// scheduler has been started)
sched.start();
//等待调度任务执行 一段时间后关闭调度
// wait long enough so that the scheduler as an opportunity to
// run the job!
try {
// wait 65 seconds to show job
Thread.sleep(65L * 1000L);
// executing...
} catch (Exception e) {
//
}
//关闭调度
// shut down the scheduler
sched.shutdown(true);
}
public static void main(String[] args) throws Exception {
SimpleExample example = new SimpleExample();
example.run();
/*
我是内部类 i am learning QUARTZ.......0
我是内部类 i am learning QUARTZ.......1
我是内部类 i am learning QUARTZ.......2
我是内部类 i am learning QUARTZ.......3
我是内部类 i am learning QUARTZ.......4
我是内部类 i am learning QUARTZ.......5
我是内部类 i am learning QUARTZ.......6
我是内部类 i am learning QUARTZ.......7
我是内部类 i am learning QUARTZ.......8
我是内部类 i am learning QUARTZ.......9
*/
}
}