java Quartz Job 和 Spring 调度任务的区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38564101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between Quartz Job and Scheduling Tasks with Spring?
提问by Xelian
I am new to Spring-boot(version 1.3.6) and Quartz and I am wondering what is the difference between making a task with Spring-scheduler:
我是 Spring-boot(版本 1.3.6)和 Quartz 的新手,我想知道使用Spring-scheduler制作任务有什么区别:
@Scheduled(fixedRate = 40000)
public void reportCurrentTime() {
System.out.println("Hello World");
}
And the Quartz way:
和石英方式:
0. Create sheduler.
1. Job which implements Job interface.
2. Create JobDetail which is instance of the job using the builder org.quartz.JobBuilder.newJob(MyJob.class)
3. Create a Triger
4. Finally set the job and the trigger to the scheduler
In code:
在代码中:
public class HelloJob implements Job {
public HelloJob() {
}
public void execute(JobExecutionContext context)
throws JobExecutionException
{
System.err.println("Hello!");
}
}
and the sheduler:
和调度器:
SchedulerFactory schedFact = new org.quartz.impl.StdSchedulerFactory();
Scheduler sched = schedFact.getScheduler();
sched.start();
// define the job and tie it to our HelloJob class
JobDetail job = newJob(HelloJob.class)
.withIdentity("myJob", "group1")
.build();
// Trigger the job to run now, and then every 40 seconds
Trigger trigger = newTrigger()
.withIdentity("myTrigger", "group1")
.startNow()
.withSchedule(simpleSchedule()
.withIntervalInSeconds(40)
.repeatForever())
.build();
// Tell quartz to schedule the job using our trigger
sched.scheduleJob(job, trigger);
Does Quartz provide more flexible way to define Jobs, Triggers and Schedulers or Spring Scheduler has something else which is better?
Quartz 是否提供了更灵活的方式来定义 Jobs、Triggers 和 Scheduler,或者 Spring Scheduler 有其他更好的方法吗?
采纳答案by shazin
Spring Scheduler is an abstraction layer written to hide the implementations of Executors in different JDKs like Java SE 1.4, Java SE 5 and Java EE environments, which have their own specific implementations.
Spring Scheduler 是一个抽象层,用于隐藏不同 JDK 中 Executor 的实现,例如 Java SE 1.4、Java SE 5 和 Java EE 环境,它们有自己的特定实现。
Quartz Scheduler is a fully fledged scheduling framework which allows CRON based or Simple periodic task execution.
Quartz Scheduler 是一个成熟的调度框架,它允许基于 CRON 或简单的周期性任务执行。
Spring Scheduler does provide integration with Quartz scheduler in the form of a Trigger
to use the full functionality of the Quartz scheduler.
Spring Scheduler 确实以 a 的形式提供了与 Quartz 调度器的集成,Trigger
以使用 Quartz 调度器的全部功能。
Advantage of using Spring Scheduler without directly using the Quartz Scheduler specific classes is that the abstraction layer provides flexibility and loose coupling.
在不直接使用 Quartz Scheduler 特定类的情况下使用 Spring Scheduler 的优点是抽象层提供了灵活性和松散耦合。