2

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.

Audrius Meškauskas
  • 20,936
  • 12
  • 75
  • 93
  • If you want to restart your servlet without affecting the long-running process, then, you have only one choice - create another Java program that runs either using Spring batch or Linux Cron. (Question is opinion based though) – Wand Maker Nov 14 '15 at 14:24
  • If you want to hear an opinion: I think a separete app would be good because you can take down or restart your spring mvc app without worries or attempts to hack the system by using `static` (which I think wouldn't work). That could be depending on your environment a second app deployed to the application server, a new standalone (server/cron) process locally on the machine or a new microservice in your worker cluster. "your environment" is typically so different from anybody else's that there isn't 1 typical good solution. – zapl Nov 14 '15 at 14:32

1 Answers1

3

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.

Zoran Regvart
  • 4,630
  • 22
  • 35