Search code examples
javaspringconcurrencyjboss

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. This would be trivial in a single JVM environment.

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).

So, it should be kind of a Singleton pattern across all cluster nodes.

I am considering using database locks or a message queue. Is there a simpler / better performing solution?


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

    If you prefer to use a shared database try to use a database like Redis to improve your performance. It is an in memory database with persistence on disk, so accessing the status of the batch process should be enough fast.