Search code examples
javaspringtimerscheduled-tasksquartz-scheduler

looking for persistent timers for a spring application


I'm looking for a lib that allow me to do

  1. define a worker that will be invoked once on a specific time in the future (not need the re-schedule / cron like featrure) i.e. a Timer
  2. The worker should accept a context which withe some parameters / inputs
  3. all should be persistent in the DB (or file) the worker
  4. worker should be managed by spring -- spring should instantiate the worker so it can be injected with dependencies
  5. be able to create timers dynamically via API and not just statically via spring XML beans

nice to have:

  1. support a cluster i.e. have several nodes that can host a worker. each store jobn in the DB will cause invokaction of ONE work on one of the nods

I've examined several alternatives none meets the requirements:

  • Quartz

when using org.springframework.scheduling.quartz.JobDetailBean makes quartz create your worker instance (and not by spring) so you can't get dependecy ijection, (which will lead me to use Service Locator which I want to avoid)

while using org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean you can't get a context. your Worker expose one public method that accepts no arguments.In addition when using MethodInvokingJobDetailFactoryBean you can't use persistence (form the Javadoc)

Note: JobDetails created via this FactoryBean are not serializable and thus not suitable for persistent job stores. You need to implement your own Quartz Job as a thin wrapper for each case where you want a persistent job to delegate to a specific service method.

  • Spring's Timer and simple JDK Timers does not support the persistence / cluster feature

I know I can impl thing myself using a DB and Spring (or even JDK) Timers but I prefer to use an a 3r party lib for that.

Any suggestions?


Solution

  • If you want to create the job details to generate triggers/job-details at runtime and still be able to use Spring DI on your beans you can refer to this blog post, it shows how to use SpringBeanJobFactory in conjunction with ObjectFactoryCreatingFactoryBean to create Quartz triggering objects at runtime with Spring injected beans.