Search code examples
javaspringservletsschedulerlong-running-processes

Best way to schedule very long running process from Spring MVC servlet


I have a Java Spring (MVC) servlet and I need to create a slow, long running process that would create entries in the database over time. The servlet should provide view to the database content, also some information about the status of the process.

The process is not computation intensive but it takes ages. It is very likely that I will need to restart the servlet even multiple times while it is running. The process is capable of making check points, but a known code must be called to make and apply them. The process creates entries in the database, and it is possible and required to monitor its activity this way.

So far I consider the following ideas:

  • A separate Java program, controlled by Linux Cron.
  • An ExecutorService, attached to the static field inside the servlet class.
  • A Spring bean that starts activities from the @PostConstruct method.
  • Spring Batch framework may be possible, but I am not sure if it is not too heavy for that I need.

I do not know, maybe this is "opinion based", but the situation could be frequent and I would like to know a typical good solution that should be considered professionally implemented.


Solution

  • Since you might restart your Servlet, in fact application or even the whole container process -- you should schedule your long running job outside of your current Servlet/application/container.

    The best way to do that is to schedule the job in another process. You could roll your own solution but there are platforms that already implement exactly that, Gearman would be one example, Spring XD another.

    Basic idea is that you handover your job to a job queue and have (ideally) distributed job scheduler process the queue. That scheduler would in turn offer API or event source to inform (i.e. publish-subscribe queue) your application of the progress.

    The job itself would ideally be implemented using a batching framework such as Spring Batch or JSR 352 Batch Applications. Both offer checkpointing so your job can be restarted from a checkpoint.