4

I was wondering if there's a way to enable/disable all of defined spring-batch jobs programmatically? For instance when I deploy my app, the database is empty and at that moment my jobs are running and throwing exceptions. I would like to have the jobs disabled until some data is populated in the database (until certain tables appear). Is this possible?

Petar Tahchiev
  • 4,336
  • 4
  • 35
  • 48
  • This is really an orchestration question. How are your jobs being launched in the first place? Whatever that is needs to delay startup until the appropriate conditions occur... – Michael Minella Oct 29 '14 at 14:13
  • Hi Michael, thanks for your reply. My jobs are launched with a cron-trigger. The one exports products to SOLR every minute (it finds only the changed products) and there's one exporting ALL products to SOLR which gets triggered once a day at midnight. – Petar Tahchiev Oct 29 '14 at 14:15
  • I'm assuming the cron-trigger is created at startup right now which is causing you the issue. You may want to use something like a `Poller` from Spring Integration to poll for the condition that it's safe to run the jobs for, when the condition occurs, then programatically schedule the jobs. – Michael Minella Oct 29 '14 at 14:23
  • Cool, I didn't know about `Poller`. This could be one option. However i think it's quite expensive to have a poller poll the database every minute. I don't want to have the jobs running when the database is empty. But once the db is initialized, I want to have those cronjobs always running. And the poller seems like will add extra stress to my db. For me the real solution would be to have a method `isRunnable` or `isActive` in `Job` so I can override it, and check the db (or `Environment` property) whether i should run the job. – Petar Tahchiev Oct 29 '14 at 14:43
  • So what you're looking for is kind of available in Spring XD. Spring Batch explicitly avoids the job orchestration issue (which this really is) where as job orchestration is something core to Spring XD. With regards to the isRunnable/isActive flag idea, how would that be set? I was assuming it would still be a programatic thing which goes back to at some point, you need to be notified that the data is in the db which typically goes back to polling of some kind (unless the loading system will notify the batch system). – Michael Minella Oct 29 '14 at 17:12
  • Nice, I'll check the Spring XD :) .. I don't think you need polling mechanism. When the server is started and the application context is created you check if the db is initialized and set an environment property `is_inited` to true or false. Then inside the job's `isActive` method check for the value of the property. If someone decides to initialize the db while the server is running set the `is_inited` property to false, this will stop the jobs, and you can also have an interceptor to show maintenance page. Once the init is done, set the `is_inited` back to true. This will start the jobs. – Petar Tahchiev Oct 29 '14 at 17:26
  • Well, it's still polling, isn't it? Just moving the polling mechanism from an external poller to launching the job. You could probably accomplish the check in a `JobExecutionListener` if you wanted. – Michael Minella Oct 29 '14 at 17:45
  • I don't think it's polling ... the quartz scheduler is calling the job anyways, but with the poller, I'll have the quartz scheduler call the job + the poller polling the database... Anyways I like the `JobExecutionListener` idea, so I'll give it a try :) .. Thank you for that :) – Petar Tahchiev Oct 29 '14 at 21:08

1 Answers1

7

Have you take a look at this question? How Spring Boot run batch jobs You can disable the job at startup by adding spring.batch.job.enabled=false to application.propertiesfile.
Then you can use the JobLauncher to run the job when your database is initialised.

Community
  • 1
  • 1
Saman
  • 141
  • 2
  • 7