0

My requirement:

I have a spring batch job with fixed delay as 5 sec. So my job will poll a web service for every 5 sec and based on the response(case when I get 200 ok status ) I need to put the current job to sleep for 30 seconds and after that it has to resume polling the web service every 5 seconds

Is there any way to configure this dynamically during run time ?? I need to change the fixed delay property in Reader part.

My sample TestJob config:

@Scheduled(fixedDelay = 5000L) // I need to change this property dynamically @ runtime 
    public void TestEventScheduler() {
        JobParameters jobParameters = new JobParametersBuilder().addLong("TestDataJobTime", System.currentTimeMillis()) 
                .toJobParameters();
        try {
            jobLauncher.run(TestDataJob, jobParameters);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
Satheesh K
  • 132
  • 2
  • 16

1 Answers1

0

There are many approaches you could do here:

  1. Register Trigger bean and update it every time with your delay. You can also look into CompoundTrigger to simplify your logic.

  2. Instead of using @Scheduled annotation at the end of your batch job you can have a listener or output channel which will add your batchJob once more to execution channel with specific delay.

Also you can check similar problem's answer: Scheduling a job with Spring programmatically (with fixedRate set dynamically)

Community
  • 1
  • 1
Igor Konoplyanko
  • 9,176
  • 6
  • 57
  • 100
  • Thanks Igor Konoplyanko, I will try your suggested answer :) – Satheesh K Jun 12 '18 at 09:25
  • I guess Option 2 will be easier, I will use either **StepExecutionListener or ItemReadListener**. But how I can add delay ? can I use **Thread.sleep(some delay)** within the @AfterStep annotated method or do we have any other way to do this ? – Satheesh K Jun 15 '18 at 13:43