Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Spring's built in Scheduler persistent.?

I have run into a case where I have to use a persistent Scheduler, since I have a web application that can crash or close due to some problems and might lose it job details if this happens . I have tried the following:

  • Use Quartz scheduler:

I used RAMJobStore first, but since it isn't persistent, it wasn't of much help. Can't setup JDBCJobStore because, this will require huge code changes to my existing code base. In light of such a scenario, I have the following queries:

  • If I use Spring's built in @Schedule annotation will my jobs be persistent..? I don't mind if the jobs get scheduled after the application starts. All I want is the jobs to not lose their details and triggers.?
  • If not, are there any other alternatives that can be followed , keeping in mind that I need to schedule multiple jobs with my scheduler.?
  • If yes, how can I achieve this.? My triggers are different each job. For e.g I might have a job that is scheduled at 9AM and another at 8.30AM and so on.
  • If not a scheduler, then can I have a mechanism to handle this.?

One thing, I found is that the documentation for Quartz isn't very descriptive. I mean it's fine for a top level config, but configuring it on your an application is a pain. This is just a side note. Nothing to do with the question.

Appreciate the help. :)

like image 684
user2339071 Avatar asked Oct 03 '13 06:10

user2339071


People also ask

How does Quartz Scheduler work internally?

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.

Can we have multiple scheduler in Spring boot?

We can choose to delay the first execution of the method by specifying the interval using the initialDelay attribute. We can deploy multiple Scheduler Instances using the ShedLock library which ensures only one instance to run at a time by using a locking mechanism in a shared database.

Is Spring scheduler single threaded?

By default, Spring uses a local single-threaded scheduler to run the tasks. As a result, even if we have multiple @Scheduled methods, they each need to wait for the thread to complete executing a previous task.

Is Spring Batch A scheduler?

Spring Batch is not a scheduling framework. There are many good enterprise schedulers (such as Quartz, Tivoli, Control-M, etc.) available in both the commercial and open source spaces.


2 Answers

  1. No, Spring's @Schedule-annotation will typically only instruct Spring at what times a certain task should be scheduled to run within the current VM. As far as I know there is not a context for the execution either. The schedule is static.

  2. I had a similar requirement and created db-scheduler (https://github.com/kagkarlsson/db-scheduler), a simple, persistent and cluster-friendly scheduler. It stores the next execution-time in the database, and triggers execution once it is reached.

A very simple example for a RecurringTask without context could look like this:

final RecurringTask myDailyTask = ComposableTask.recurringTask("my-daily-task", Schedules.daily(LocalTime.of(8, 0)),
                () -> System.out.println("Executed!"));

final Scheduler scheduler = Scheduler
        .create(dataSource)
        .startTasks(myDailyTask)
        .threads(5)
        .build();

scheduler.start();

It will execute the task named my-daily-task at 08:00 every day. It will be scheduled in the database when the scheduler is first started, unless it already exists in the database.

If you want to schedule an ad-hoc task some time in the future with context, you can use the OneTimeTask:

    final OneTimeTask oneTimeTask = ComposableTask.onetimeTask("my-onetime-task",
            (taskInstance, context) -> System.out.println("One-time task with identifier "+taskInstance.getId()+" executed!"));

    scheduler.scheduleForExecution(LocalDateTime.now().plusDays(1), oneTimeTask.instance("1001"));
  1. See the example above. Any number of tasks can be scheduled, as long as task-name and instanceIdentifier is unique.
like image 169
Gustav Karlsson Avatar answered Oct 11 '22 14:10

Gustav Karlsson


@Schedule has nothing to do with the actual executor. The default java executors aren't persistent (maybe there are some app-server specific ones that are), if you want persistence you have to use Quartz for job execution.

like image 33
M. Deinum Avatar answered Oct 11 '22 13:10

M. Deinum