从Spring MVC servlet安排非常长的运行过程的最佳方法

时间:2021-08-17 02:16:51

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.

我有一个Java Spring(MVC)servlet,我需要创建一个缓慢,长时间运行的进程,该进程将随着时间的推移在数据库中创建条目。 servlet应提供数据库内容的视图,以及有关进程状态的一些信息。

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.

这个过程不是计算密集型的,但需要很长时间。我很可能需要在运行时多次重启servlet。该过程能够制作检查点,但必须调用已知代码来制作和应用它们。该过程在数据库中创建条目,并且可以并且需要以这种方式监视其活动。

So far I consider the following ideas:

到目前为止,我考虑以下想法:

  • A separate Java program, controlled by Linux Cron.
  • 一个单独的Java程序,由Linux Cron控制。
  • An ExecutorService, attached to the static field inside the servlet class.
  • 一个ExecutorService,附加到servlet类中的静态字段。
  • A Spring bean that starts activities from the @PostConstruct method.
  • 一个Spring bean,它从@PostConstruct方法启动活动。
  • Spring Batch framework may be possible, but I am not sure if it is not too heavy for that I need.
  • Spring批处理框架可能是可能的,但我不确定它是否对我所需要的不太重。

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.

我不知道,也许这是“基于意见的”,但情况可能很频繁,我想知道一个应该被认为是专业实施的典型的好解决方案。

1 个解决方案

#1


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.

由于您可能会重新启动Servlet,实际上是应用程序甚至是整个容器进程 - 您应该在当前的Servlet /应用程序/容器之外安排长时间运行的作业。

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.

最好的方法是在另一个过程中安排作业。您可以推出自己的解决方案,但有些平台已经完全实现了,Gearman就是一个例子,Spring XD是另一个例子。

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.

基本思想是将作业切换到作业队列,并且(理想情况下)分布式作业调度程序处理队列。该调度程序将依次提供API或事件源来通知(即发布 - 订阅队列)您的进度应用程序。

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.

理想情况下,作业本身可以使用批处理框架(如Spring Batch或JSR 352 Batch Applications)来实现。两者都提供检查点,因此您可以从检查点重新启动您的工作。

#1


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.

由于您可能会重新启动Servlet,实际上是应用程序甚至是整个容器进程 - 您应该在当前的Servlet /应用程序/容器之外安排长时间运行的作业。

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.

最好的方法是在另一个过程中安排作业。您可以推出自己的解决方案,但有些平台已经完全实现了,Gearman就是一个例子,Spring XD是另一个例子。

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.

基本思想是将作业切换到作业队列,并且(理想情况下)分布式作业调度程序处理队列。该调度程序将依次提供API或事件源来通知(即发布 - 订阅队列)您的进度应用程序。

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.

理想情况下,作业本身可以使用批处理框架(如Spring Batch或JSR 352 Batch Applications)来实现。两者都提供检查点,因此您可以从检查点重新启动您的工作。