quartz基本使用
文章目录
创建一个任务调度
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
//Schedulers can be immediately used to schedule jobs, but they will not start executing any until the .start()
scheduler.start();
//And then schedule those jobs with triggers that define at what time(s) the job should run.
JobDetail job = newJob(Myjob.class)
.withIdentity("job1", "group1")
.build();
// Trigger the job to run now, and then repeat every 40 seconds
Trigger trigger = newTrigger()
.withIdentity("trigger1", "group1")
.startNow()
.withSchedule(simpleSchedule()
.withIntervalInSeconds(2)
.repeatForever())
.build();
// Tell quartz to schedule the job using our trigger
scheduler.scheduleJob(job, trigger);
任务类
public class Myjob implements Job {
public Myjob() {
System.out.println("do");
}
// you can implement Jobs - which have an .execute(..) method.
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
System.err.println("Hello World! MyJob is executing.");
System.out.println(this);
}
}
如果是Cron表达式
Trigger trigger = newTrigger()
.withIdentity("trigger1", "group1")
.startNow()
//Build a trigger that will fire every other second, between 8am and 5pm, every day:
.withSchedule(cronSchedule("0/2 * 8-17 * * ?"))
.build();
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论