1

I am looking for a simple cron-type solution for java-application.

I looked at cron4j, where you can set up a method to execute, for example every 5 minutes.

Only, I have a custom requirement that, if the previous method call is still running, it should not start another simultaneous call, but just skip that call and try again in 5 mins, etc --- i.e. I want to make sure never to run 2 threads for the same job in parallel. Only if the previous method-call has completed, should a new one start.

What is the easiest/most stable way to implement this requirement in Java? (doesn't have to be cron4j if there is a better way)

Rop
  • 3,359
  • 3
  • 38
  • 59

1 Answers1

0

I prefer Cron4j because I believe it's lightweight and easy to use. So I would use an application-scoped method to perform this synchronization.

For instance, if your application is distributed, then your jobs details must be stored on some shared storage, say a database. Then in this database, you can set a flag (i.e. a column value for each job details) that indicates if a this job is already being executed. And then, the next execution time (i.e. another column value, and I find having a column holding an next execution flag very helpful, you should give it a try if you haven't already) should be modified by adding whatever offset you like.

If your application isn't distributed, then you can easily use a synchronized static method or a static ReentrantLock instance to control the synchronization.

Muhammad Gelbana
  • 3,890
  • 3
  • 43
  • 81