Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Quartz Job and Scheduling Tasks with Spring?

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:

    @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?

like image 778
Xelian Avatar asked Jul 25 '16 09:07

Xelian


People also ask

Does Spring scheduler use Quartz?

Based on the docs, Spring provides three way of scheduling: @Scheduled. Via Quartz. Via JDK Timer.

What is a job in Quartz?

Jobs. A Job is a class that implements the Job interface. It has only one simple method: public class SimpleJob implements Job { public void execute(JobExecutionContext arg0) throws JobExecutionException { System.out.println("This is a quartz job!" ); } }

How does Quartz scheduling work?

Quartz scheduler allows an enterprise to schedule a job at a specified date and time. It allows us to perform the operations to schedule or unschedule the jobs. It provides operations to start or stop or pause the scheduler. It also provides reminder services.

How do you schedule multiple jobs using Quartz?

If you want to schedule multiple jobs in your console application you can simply call Scheduler. ScheduleJob (IScheduler) passing the job and the trigger you've previously created: IJobDetail firstJob = JobBuilder. Create<FirstJob>() .


1 Answers

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.

Quartz Scheduler is a fully fledged scheduling framework which allows CRON based or Simple periodic task execution.

Spring Scheduler does provide integration with Quartz scheduler in the form of a Trigger to use the full functionality of the Quartz scheduler.

Advantage of using Spring Scheduler without directly using the Quartz Scheduler specific classes is that the abstraction layer provides flexibility and loose coupling.

like image 109
shazin Avatar answered Sep 21 '22 15:09

shazin