Is Spring's built in Scheduler persistent.?

*爱你&永不变心* 提交于 2020-01-01 04:51:06

问题


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. :)


回答1:


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




回答2:


  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.


来源:https://stackoverflow.com/questions/19152486/is-springs-built-in-scheduler-persistent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!