简体   繁体   English

在集群环境中避免Spring Batch作业中的并发

[英]Avoiding concurrency in Spring Batch jobs in a cluster environment

I want to ensure that a Spring job is not started a second time while it still runs. 我想确保Spring作业仍在运行时不会再次启动。 This would be trivial in a single JVM environment. 在单个JVM环境中,这是微不足道的。

However, how can I achieve this in a cluster environment (more specifically, in JBoss 5.1 - I know a bit antiquated; if solutions exist for later versions, I'd be interested in those as well). 但是,如何在集群环境中实现这一点(更具体地说,在JBoss 5.1中-我有点过时了;如果存在更高版本的解决方案,我也会对此感兴趣。)

So, it should be kind of a Singleton pattern across all cluster nodes. 因此,它应该是跨所有群集节点的Singleton模式。

I am considering using database locks or a message queue. 我正在考虑使用数据库锁或消息队列。 Is there a simpler / better performing solution? 有没有更简单/性能更好的解决方案?

You need to synchronize threads that doesn't know nothing each other, so the easiest way is to share some information on a common place. 您需要同步彼此一无所知的线程,因此最简单的方法是在一个公共位置共享一些信息。 Valid alternatives are: 有效的替代方法是:

  • A shared database 共享数据库
  • A shared file 共享文件
  • An external web service holding the status of the batch process 拥有批处理状态的外部Web服务

If you prefer to use a shared database try to use a database like Redis to improve your performance. 如果您喜欢使用共享数据库,请尝试使用Redis之类的数据库来提高性能。 It is an in memory database with persistence on disk, so accessing the status of the batch process should be enough fast. 它是一个内存中数据库,在磁盘上具有持久性,因此访问批处理状态应该足够快。

这为时已晚,但是对于将来的查找而言:spring batch使用jpa存储库来同步作业,因此可以避免并发。

You can add a Job Listener and in the before step and use JobExecutionDao in it to find all JobExecutions . 您可以添加一个Job Listener,然后在上一步中使用JobExecutionDao来查找所有JobExecutions If there are more than one running - throw an exception and exit the job. 如果有多个运行-抛出异常并退出作业。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM