Restart step (or job) after timeout occours2019 Community Moderator ElectionSpring batch: Retry job if does not complete in particular timeSpring Batch job throwing ItemStreamException when restarting; has a processor that skips records and the skip listener writes to a flat fileSpring Batch IllegalArgumentException while executing step with chunk after step with taskletSpring Batch - late binding of commit interval not working with skip policySpring batch default retry policyHandling job/step exceptions with asynchronous TaskManagerSpring Batch Default Handling of Step/Chunk When ItemReader throws ItemStreamExceptionHow to prevent Spring Batch from resubmitting chunk items in the event of a skippable exception thrown by an item writer?Spring job not processing all records but exits with a status of CompleteAborting a Batch Job during a chunk-based stepSpring Batch restart functionality not working when using @StepScope

What are substitutions for coconut in curry?

Help rendering a complicated sum/product formula

Why is there so much iron?

Tikz: place node leftmost of two nodes of different widths

Do native speakers use "ultima" and "proxima" frequently in spoken English?

Would it be believable to defy demographics in a story?

Do US professors/group leaders only get a salary, but no group budget?

Usage and meaning of "up" in "...worth at least a thousand pounds up in London"

If "dar" means "to give", what does "daros" mean?

Unfrosted light bulb

Optimising a list searching algorithm

Geography in 3D perspective

How are passwords stolen from companies if they only store hashes?

Is there a hypothetical scenario that would make Earth uninhabitable for humans, but not for (the majority of) other animals?

How do hiring committees for research positions view getting "scooped"?

I got the following comment from a reputed math journal. What does it mean?

Can other pieces capture a threatening piece and prevent a checkmate?

What does Jesus mean regarding "Raca," and "you fool?" - is he contrasting them?

What does Deadpool mean by "left the house in that shirt"?

What is the relationship between relativity and the Doppler effect?

What can I do if I am asked to learn different programming languages very frequently?

Generic TVP tradeoffs?

Comment Box for Substitution Method of Integrals

Describing a chess game in a novel



Restart step (or job) after timeout occours



2019 Community Moderator ElectionSpring batch: Retry job if does not complete in particular timeSpring Batch job throwing ItemStreamException when restarting; has a processor that skips records and the skip listener writes to a flat fileSpring Batch IllegalArgumentException while executing step with chunk after step with taskletSpring Batch - late binding of commit interval not working with skip policySpring batch default retry policyHandling job/step exceptions with asynchronous TaskManagerSpring Batch Default Handling of Step/Chunk When ItemReader throws ItemStreamExceptionHow to prevent Spring Batch from resubmitting chunk items in the event of a skippable exception thrown by an item writer?Spring job not processing all records but exits with a status of CompleteAborting a Batch Job during a chunk-based stepSpring Batch restart functionality not working when using @StepScope










0















It's possible restart a job or step automatically when timeout occurs?
I tried retry and skip (skip, because job re-run every 30 minutes, provided no error has occurred) a step, like this:



<step id="jobTest.step1">
<tasklet>
<transaction-attributes timeout="120"/>
<chunk reader="testReader" processor="testProcessor" writer="testWriter" commit-interval="10" retry-limit="3" >
<retryable-exception-classes>
<include class="org.springframework.transaction.TransactionTimedOutException"/>
</retryable-exception-classes>
</chunk>
<listeners>
<listener ref="stepListener" />
</listeners>
</tasklet>
</step>


I tried with skip-policy too, but I did not get satisfactory results.
I just need restart this step (or entire job) when occurs timeout .



UPDATE



I've tried this too, but without sucess:
Spring batch: Retry job if does not complete in particular time










share|improve this question




























    0















    It's possible restart a job or step automatically when timeout occurs?
    I tried retry and skip (skip, because job re-run every 30 minutes, provided no error has occurred) a step, like this:



    <step id="jobTest.step1">
    <tasklet>
    <transaction-attributes timeout="120"/>
    <chunk reader="testReader" processor="testProcessor" writer="testWriter" commit-interval="10" retry-limit="3" >
    <retryable-exception-classes>
    <include class="org.springframework.transaction.TransactionTimedOutException"/>
    </retryable-exception-classes>
    </chunk>
    <listeners>
    <listener ref="stepListener" />
    </listeners>
    </tasklet>
    </step>


    I tried with skip-policy too, but I did not get satisfactory results.
    I just need restart this step (or entire job) when occurs timeout .



    UPDATE



    I've tried this too, but without sucess:
    Spring batch: Retry job if does not complete in particular time










    share|improve this question


























      0












      0








      0


      1






      It's possible restart a job or step automatically when timeout occurs?
      I tried retry and skip (skip, because job re-run every 30 minutes, provided no error has occurred) a step, like this:



      <step id="jobTest.step1">
      <tasklet>
      <transaction-attributes timeout="120"/>
      <chunk reader="testReader" processor="testProcessor" writer="testWriter" commit-interval="10" retry-limit="3" >
      <retryable-exception-classes>
      <include class="org.springframework.transaction.TransactionTimedOutException"/>
      </retryable-exception-classes>
      </chunk>
      <listeners>
      <listener ref="stepListener" />
      </listeners>
      </tasklet>
      </step>


      I tried with skip-policy too, but I did not get satisfactory results.
      I just need restart this step (or entire job) when occurs timeout .



      UPDATE



      I've tried this too, but without sucess:
      Spring batch: Retry job if does not complete in particular time










      share|improve this question
















      It's possible restart a job or step automatically when timeout occurs?
      I tried retry and skip (skip, because job re-run every 30 minutes, provided no error has occurred) a step, like this:



      <step id="jobTest.step1">
      <tasklet>
      <transaction-attributes timeout="120"/>
      <chunk reader="testReader" processor="testProcessor" writer="testWriter" commit-interval="10" retry-limit="3" >
      <retryable-exception-classes>
      <include class="org.springframework.transaction.TransactionTimedOutException"/>
      </retryable-exception-classes>
      </chunk>
      <listeners>
      <listener ref="stepListener" />
      </listeners>
      </tasklet>
      </step>


      I tried with skip-policy too, but I did not get satisfactory results.
      I just need restart this step (or entire job) when occurs timeout .



      UPDATE



      I've tried this too, but without sucess:
      Spring batch: Retry job if does not complete in particular time







      spring-batch






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 7 at 18:27







      Gabriel Moreira

















      asked Mar 7 at 17:13









      Gabriel MoreiraGabriel Moreira

      7517




      7517






















          1 Answer
          1






          active

          oldest

          votes


















          1














          Retry/Skip features are applicable to items within a chunk in a fault-tolerant chunk-oriented step, not at the step level or the job level. There are actually two distinct things in your requirement:



          1. How to stop a job after a given timeout?



          Apart from externally calling JobOperator#stop after a time out occurs, you can stop a job from within the job itself by sending a stop signal through the StepExecution#isTerminateOnly flag. The idea is to have access to the step execution in order to set that flag after a certain timeout. This depends on the tasklet type of the step:



          Simple Tasklet



          For a simple tasklet, you can access the step execution through the ChunkContext. Here is an example:



          import java.time.Duration;
          import java.util.Date;

          import org.springframework.batch.core.StepContribution;
          import org.springframework.batch.core.scope.context.ChunkContext;
          import org.springframework.batch.core.step.tasklet.Tasklet;
          import org.springframework.batch.repeat.RepeatStatus;

          public class MyTasklet implements Tasklet

          private static final int TIMEOUT = 120; // in minutes (can be turned into a configurable field through a constructor)

          @Override
          public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception
          if (timeout(chunkContext))
          chunkContext.getStepContext().getStepExecution().setTerminateOnly();

          // do some work
          if (moreWork())
          return RepeatStatus.CONTINUABLE;
          else
          return RepeatStatus.FINISHED;



          private boolean timeout(ChunkContext chunkContext)
          Date startTime = chunkContext.getStepContext().getStepExecution().getJobExecution().getStartTime();
          Date now = new Date();
          return Duration.between(startTime.toInstant(), now.toInstant()).toMinutes() > TIMEOUT;


          private boolean moreWork()
          return false; // TODO implement logic




          This tasklet will regularly check if the timeout is exceeded and stop the step (and hence the surrounding job) accordingly.



          Chunk-oriented tasklet



          In this case, you can use a step listener and set the terminateOnly flag in one of the lifecycle methods (afterRead, afterWrite, etc). Here is an example:



          import java.time.Duration;
          import java.util.Date;

          import org.springframework.batch.core.StepExecution;
          import org.springframework.batch.core.listener.StepListenerSupport;
          import org.springframework.batch.core.scope.context.ChunkContext;

          public class StopListener extends StepListenerSupport

          private static final int TIMEOUT = 120; // in minutes (can be made configurable through constructor)

          private StepExecution stepExecution;

          @Override
          public void beforeStep(StepExecution stepExecution)
          this.stepExecution = stepExecution;


          @Override
          public void afterChunk(ChunkContext context) // or afterRead, or afterWrite, etc.
          if (timeout(context))
          this.stepExecution.setTerminateOnly();



          private boolean timeout(ChunkContext chunkContext)
          Date startTime = chunkContext.getStepContext().getStepExecution().getJobExecution().getStartTime();
          Date now = new Date();
          return Duration.between(startTime.toInstant(), now.toInstant()).toMinutes() > TIMEOUT;




          The idea is the same, you need to check the time regularly and set the flag when appropriate.



          Both ways will leave your job in a STOPPED status which is a restartable status. Batch jobs used to be executed in a batch window and a common requirement was to stop them (gracefully) when the window is closed. The previous technique is the way to go.



          The answer in Spring batch: Retry job if does not complete in particular time is also an option, however it will leave your job in a FAILED status (which is a restartable status as well). But you said it didn't work for you.



          2. How to restart the job automatically after the timeout?



          Now that you know how to stop a job after a timeout, you can use a RetryTemplate around the job launcher and re-launch the job when appropriate. Here is an example:



          public static void main(String[] args) throws Throwable 
          RetryTemplate retryTemplate = new RetryTemplate();
          retryTemplate.setRetryPolicy(new SimpleRetryPolicy(3));

          ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyJob.class);
          JobLauncher jobLauncher = applicationContext.getBean(JobLauncher.class);
          Job job = applicationContext.getBean(Job.class);
          JobParameters jobParameters = new JobParametersBuilder()
          .addDate("runtime", new Date())
          .toJobParameters();

          retryTemplate.execute((RetryCallback<JobExecution, Throwable>) retryContext ->
          JobExecution jobExecution = jobLauncher.run(job, jobParameters);
          if (jobExecution.getExitStatus().getExitCode().equals(ExitStatus.STOPPED.getExitCode()))
          throw new Exception("Job timeout");

          return jobExecution;
          );



          This will automatically re-run the job at most 3 times if it finishes with the status STOPPED (for example due to a timeout as shown previously).



          Hope this helps.






          share|improve this answer























          • This alternative worked fine for me. However, the ideal one for my case, would be to force retry (or skip) when specifically the timeout occurs by TransactionTimedOutException (with timeout on tasklet).

            – Gabriel Moreira
            Mar 11 at 13:26










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55049424%2frestart-step-or-job-after-timeout-occours%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Retry/Skip features are applicable to items within a chunk in a fault-tolerant chunk-oriented step, not at the step level or the job level. There are actually two distinct things in your requirement:



          1. How to stop a job after a given timeout?



          Apart from externally calling JobOperator#stop after a time out occurs, you can stop a job from within the job itself by sending a stop signal through the StepExecution#isTerminateOnly flag. The idea is to have access to the step execution in order to set that flag after a certain timeout. This depends on the tasklet type of the step:



          Simple Tasklet



          For a simple tasklet, you can access the step execution through the ChunkContext. Here is an example:



          import java.time.Duration;
          import java.util.Date;

          import org.springframework.batch.core.StepContribution;
          import org.springframework.batch.core.scope.context.ChunkContext;
          import org.springframework.batch.core.step.tasklet.Tasklet;
          import org.springframework.batch.repeat.RepeatStatus;

          public class MyTasklet implements Tasklet

          private static final int TIMEOUT = 120; // in minutes (can be turned into a configurable field through a constructor)

          @Override
          public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception
          if (timeout(chunkContext))
          chunkContext.getStepContext().getStepExecution().setTerminateOnly();

          // do some work
          if (moreWork())
          return RepeatStatus.CONTINUABLE;
          else
          return RepeatStatus.FINISHED;



          private boolean timeout(ChunkContext chunkContext)
          Date startTime = chunkContext.getStepContext().getStepExecution().getJobExecution().getStartTime();
          Date now = new Date();
          return Duration.between(startTime.toInstant(), now.toInstant()).toMinutes() > TIMEOUT;


          private boolean moreWork()
          return false; // TODO implement logic




          This tasklet will regularly check if the timeout is exceeded and stop the step (and hence the surrounding job) accordingly.



          Chunk-oriented tasklet



          In this case, you can use a step listener and set the terminateOnly flag in one of the lifecycle methods (afterRead, afterWrite, etc). Here is an example:



          import java.time.Duration;
          import java.util.Date;

          import org.springframework.batch.core.StepExecution;
          import org.springframework.batch.core.listener.StepListenerSupport;
          import org.springframework.batch.core.scope.context.ChunkContext;

          public class StopListener extends StepListenerSupport

          private static final int TIMEOUT = 120; // in minutes (can be made configurable through constructor)

          private StepExecution stepExecution;

          @Override
          public void beforeStep(StepExecution stepExecution)
          this.stepExecution = stepExecution;


          @Override
          public void afterChunk(ChunkContext context) // or afterRead, or afterWrite, etc.
          if (timeout(context))
          this.stepExecution.setTerminateOnly();



          private boolean timeout(ChunkContext chunkContext)
          Date startTime = chunkContext.getStepContext().getStepExecution().getJobExecution().getStartTime();
          Date now = new Date();
          return Duration.between(startTime.toInstant(), now.toInstant()).toMinutes() > TIMEOUT;




          The idea is the same, you need to check the time regularly and set the flag when appropriate.



          Both ways will leave your job in a STOPPED status which is a restartable status. Batch jobs used to be executed in a batch window and a common requirement was to stop them (gracefully) when the window is closed. The previous technique is the way to go.



          The answer in Spring batch: Retry job if does not complete in particular time is also an option, however it will leave your job in a FAILED status (which is a restartable status as well). But you said it didn't work for you.



          2. How to restart the job automatically after the timeout?



          Now that you know how to stop a job after a timeout, you can use a RetryTemplate around the job launcher and re-launch the job when appropriate. Here is an example:



          public static void main(String[] args) throws Throwable 
          RetryTemplate retryTemplate = new RetryTemplate();
          retryTemplate.setRetryPolicy(new SimpleRetryPolicy(3));

          ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyJob.class);
          JobLauncher jobLauncher = applicationContext.getBean(JobLauncher.class);
          Job job = applicationContext.getBean(Job.class);
          JobParameters jobParameters = new JobParametersBuilder()
          .addDate("runtime", new Date())
          .toJobParameters();

          retryTemplate.execute((RetryCallback<JobExecution, Throwable>) retryContext ->
          JobExecution jobExecution = jobLauncher.run(job, jobParameters);
          if (jobExecution.getExitStatus().getExitCode().equals(ExitStatus.STOPPED.getExitCode()))
          throw new Exception("Job timeout");

          return jobExecution;
          );



          This will automatically re-run the job at most 3 times if it finishes with the status STOPPED (for example due to a timeout as shown previously).



          Hope this helps.






          share|improve this answer























          • This alternative worked fine for me. However, the ideal one for my case, would be to force retry (or skip) when specifically the timeout occurs by TransactionTimedOutException (with timeout on tasklet).

            – Gabriel Moreira
            Mar 11 at 13:26















          1














          Retry/Skip features are applicable to items within a chunk in a fault-tolerant chunk-oriented step, not at the step level or the job level. There are actually two distinct things in your requirement:



          1. How to stop a job after a given timeout?



          Apart from externally calling JobOperator#stop after a time out occurs, you can stop a job from within the job itself by sending a stop signal through the StepExecution#isTerminateOnly flag. The idea is to have access to the step execution in order to set that flag after a certain timeout. This depends on the tasklet type of the step:



          Simple Tasklet



          For a simple tasklet, you can access the step execution through the ChunkContext. Here is an example:



          import java.time.Duration;
          import java.util.Date;

          import org.springframework.batch.core.StepContribution;
          import org.springframework.batch.core.scope.context.ChunkContext;
          import org.springframework.batch.core.step.tasklet.Tasklet;
          import org.springframework.batch.repeat.RepeatStatus;

          public class MyTasklet implements Tasklet

          private static final int TIMEOUT = 120; // in minutes (can be turned into a configurable field through a constructor)

          @Override
          public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception
          if (timeout(chunkContext))
          chunkContext.getStepContext().getStepExecution().setTerminateOnly();

          // do some work
          if (moreWork())
          return RepeatStatus.CONTINUABLE;
          else
          return RepeatStatus.FINISHED;



          private boolean timeout(ChunkContext chunkContext)
          Date startTime = chunkContext.getStepContext().getStepExecution().getJobExecution().getStartTime();
          Date now = new Date();
          return Duration.between(startTime.toInstant(), now.toInstant()).toMinutes() > TIMEOUT;


          private boolean moreWork()
          return false; // TODO implement logic




          This tasklet will regularly check if the timeout is exceeded and stop the step (and hence the surrounding job) accordingly.



          Chunk-oriented tasklet



          In this case, you can use a step listener and set the terminateOnly flag in one of the lifecycle methods (afterRead, afterWrite, etc). Here is an example:



          import java.time.Duration;
          import java.util.Date;

          import org.springframework.batch.core.StepExecution;
          import org.springframework.batch.core.listener.StepListenerSupport;
          import org.springframework.batch.core.scope.context.ChunkContext;

          public class StopListener extends StepListenerSupport

          private static final int TIMEOUT = 120; // in minutes (can be made configurable through constructor)

          private StepExecution stepExecution;

          @Override
          public void beforeStep(StepExecution stepExecution)
          this.stepExecution = stepExecution;


          @Override
          public void afterChunk(ChunkContext context) // or afterRead, or afterWrite, etc.
          if (timeout(context))
          this.stepExecution.setTerminateOnly();



          private boolean timeout(ChunkContext chunkContext)
          Date startTime = chunkContext.getStepContext().getStepExecution().getJobExecution().getStartTime();
          Date now = new Date();
          return Duration.between(startTime.toInstant(), now.toInstant()).toMinutes() > TIMEOUT;




          The idea is the same, you need to check the time regularly and set the flag when appropriate.



          Both ways will leave your job in a STOPPED status which is a restartable status. Batch jobs used to be executed in a batch window and a common requirement was to stop them (gracefully) when the window is closed. The previous technique is the way to go.



          The answer in Spring batch: Retry job if does not complete in particular time is also an option, however it will leave your job in a FAILED status (which is a restartable status as well). But you said it didn't work for you.



          2. How to restart the job automatically after the timeout?



          Now that you know how to stop a job after a timeout, you can use a RetryTemplate around the job launcher and re-launch the job when appropriate. Here is an example:



          public static void main(String[] args) throws Throwable 
          RetryTemplate retryTemplate = new RetryTemplate();
          retryTemplate.setRetryPolicy(new SimpleRetryPolicy(3));

          ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyJob.class);
          JobLauncher jobLauncher = applicationContext.getBean(JobLauncher.class);
          Job job = applicationContext.getBean(Job.class);
          JobParameters jobParameters = new JobParametersBuilder()
          .addDate("runtime", new Date())
          .toJobParameters();

          retryTemplate.execute((RetryCallback<JobExecution, Throwable>) retryContext ->
          JobExecution jobExecution = jobLauncher.run(job, jobParameters);
          if (jobExecution.getExitStatus().getExitCode().equals(ExitStatus.STOPPED.getExitCode()))
          throw new Exception("Job timeout");

          return jobExecution;
          );



          This will automatically re-run the job at most 3 times if it finishes with the status STOPPED (for example due to a timeout as shown previously).



          Hope this helps.






          share|improve this answer























          • This alternative worked fine for me. However, the ideal one for my case, would be to force retry (or skip) when specifically the timeout occurs by TransactionTimedOutException (with timeout on tasklet).

            – Gabriel Moreira
            Mar 11 at 13:26













          1












          1








          1







          Retry/Skip features are applicable to items within a chunk in a fault-tolerant chunk-oriented step, not at the step level or the job level. There are actually two distinct things in your requirement:



          1. How to stop a job after a given timeout?



          Apart from externally calling JobOperator#stop after a time out occurs, you can stop a job from within the job itself by sending a stop signal through the StepExecution#isTerminateOnly flag. The idea is to have access to the step execution in order to set that flag after a certain timeout. This depends on the tasklet type of the step:



          Simple Tasklet



          For a simple tasklet, you can access the step execution through the ChunkContext. Here is an example:



          import java.time.Duration;
          import java.util.Date;

          import org.springframework.batch.core.StepContribution;
          import org.springframework.batch.core.scope.context.ChunkContext;
          import org.springframework.batch.core.step.tasklet.Tasklet;
          import org.springframework.batch.repeat.RepeatStatus;

          public class MyTasklet implements Tasklet

          private static final int TIMEOUT = 120; // in minutes (can be turned into a configurable field through a constructor)

          @Override
          public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception
          if (timeout(chunkContext))
          chunkContext.getStepContext().getStepExecution().setTerminateOnly();

          // do some work
          if (moreWork())
          return RepeatStatus.CONTINUABLE;
          else
          return RepeatStatus.FINISHED;



          private boolean timeout(ChunkContext chunkContext)
          Date startTime = chunkContext.getStepContext().getStepExecution().getJobExecution().getStartTime();
          Date now = new Date();
          return Duration.between(startTime.toInstant(), now.toInstant()).toMinutes() > TIMEOUT;


          private boolean moreWork()
          return false; // TODO implement logic




          This tasklet will regularly check if the timeout is exceeded and stop the step (and hence the surrounding job) accordingly.



          Chunk-oriented tasklet



          In this case, you can use a step listener and set the terminateOnly flag in one of the lifecycle methods (afterRead, afterWrite, etc). Here is an example:



          import java.time.Duration;
          import java.util.Date;

          import org.springframework.batch.core.StepExecution;
          import org.springframework.batch.core.listener.StepListenerSupport;
          import org.springframework.batch.core.scope.context.ChunkContext;

          public class StopListener extends StepListenerSupport

          private static final int TIMEOUT = 120; // in minutes (can be made configurable through constructor)

          private StepExecution stepExecution;

          @Override
          public void beforeStep(StepExecution stepExecution)
          this.stepExecution = stepExecution;


          @Override
          public void afterChunk(ChunkContext context) // or afterRead, or afterWrite, etc.
          if (timeout(context))
          this.stepExecution.setTerminateOnly();



          private boolean timeout(ChunkContext chunkContext)
          Date startTime = chunkContext.getStepContext().getStepExecution().getJobExecution().getStartTime();
          Date now = new Date();
          return Duration.between(startTime.toInstant(), now.toInstant()).toMinutes() > TIMEOUT;




          The idea is the same, you need to check the time regularly and set the flag when appropriate.



          Both ways will leave your job in a STOPPED status which is a restartable status. Batch jobs used to be executed in a batch window and a common requirement was to stop them (gracefully) when the window is closed. The previous technique is the way to go.



          The answer in Spring batch: Retry job if does not complete in particular time is also an option, however it will leave your job in a FAILED status (which is a restartable status as well). But you said it didn't work for you.



          2. How to restart the job automatically after the timeout?



          Now that you know how to stop a job after a timeout, you can use a RetryTemplate around the job launcher and re-launch the job when appropriate. Here is an example:



          public static void main(String[] args) throws Throwable 
          RetryTemplate retryTemplate = new RetryTemplate();
          retryTemplate.setRetryPolicy(new SimpleRetryPolicy(3));

          ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyJob.class);
          JobLauncher jobLauncher = applicationContext.getBean(JobLauncher.class);
          Job job = applicationContext.getBean(Job.class);
          JobParameters jobParameters = new JobParametersBuilder()
          .addDate("runtime", new Date())
          .toJobParameters();

          retryTemplate.execute((RetryCallback<JobExecution, Throwable>) retryContext ->
          JobExecution jobExecution = jobLauncher.run(job, jobParameters);
          if (jobExecution.getExitStatus().getExitCode().equals(ExitStatus.STOPPED.getExitCode()))
          throw new Exception("Job timeout");

          return jobExecution;
          );



          This will automatically re-run the job at most 3 times if it finishes with the status STOPPED (for example due to a timeout as shown previously).



          Hope this helps.






          share|improve this answer













          Retry/Skip features are applicable to items within a chunk in a fault-tolerant chunk-oriented step, not at the step level or the job level. There are actually two distinct things in your requirement:



          1. How to stop a job after a given timeout?



          Apart from externally calling JobOperator#stop after a time out occurs, you can stop a job from within the job itself by sending a stop signal through the StepExecution#isTerminateOnly flag. The idea is to have access to the step execution in order to set that flag after a certain timeout. This depends on the tasklet type of the step:



          Simple Tasklet



          For a simple tasklet, you can access the step execution through the ChunkContext. Here is an example:



          import java.time.Duration;
          import java.util.Date;

          import org.springframework.batch.core.StepContribution;
          import org.springframework.batch.core.scope.context.ChunkContext;
          import org.springframework.batch.core.step.tasklet.Tasklet;
          import org.springframework.batch.repeat.RepeatStatus;

          public class MyTasklet implements Tasklet

          private static final int TIMEOUT = 120; // in minutes (can be turned into a configurable field through a constructor)

          @Override
          public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception
          if (timeout(chunkContext))
          chunkContext.getStepContext().getStepExecution().setTerminateOnly();

          // do some work
          if (moreWork())
          return RepeatStatus.CONTINUABLE;
          else
          return RepeatStatus.FINISHED;



          private boolean timeout(ChunkContext chunkContext)
          Date startTime = chunkContext.getStepContext().getStepExecution().getJobExecution().getStartTime();
          Date now = new Date();
          return Duration.between(startTime.toInstant(), now.toInstant()).toMinutes() > TIMEOUT;


          private boolean moreWork()
          return false; // TODO implement logic




          This tasklet will regularly check if the timeout is exceeded and stop the step (and hence the surrounding job) accordingly.



          Chunk-oriented tasklet



          In this case, you can use a step listener and set the terminateOnly flag in one of the lifecycle methods (afterRead, afterWrite, etc). Here is an example:



          import java.time.Duration;
          import java.util.Date;

          import org.springframework.batch.core.StepExecution;
          import org.springframework.batch.core.listener.StepListenerSupport;
          import org.springframework.batch.core.scope.context.ChunkContext;

          public class StopListener extends StepListenerSupport

          private static final int TIMEOUT = 120; // in minutes (can be made configurable through constructor)

          private StepExecution stepExecution;

          @Override
          public void beforeStep(StepExecution stepExecution)
          this.stepExecution = stepExecution;


          @Override
          public void afterChunk(ChunkContext context) // or afterRead, or afterWrite, etc.
          if (timeout(context))
          this.stepExecution.setTerminateOnly();



          private boolean timeout(ChunkContext chunkContext)
          Date startTime = chunkContext.getStepContext().getStepExecution().getJobExecution().getStartTime();
          Date now = new Date();
          return Duration.between(startTime.toInstant(), now.toInstant()).toMinutes() > TIMEOUT;




          The idea is the same, you need to check the time regularly and set the flag when appropriate.



          Both ways will leave your job in a STOPPED status which is a restartable status. Batch jobs used to be executed in a batch window and a common requirement was to stop them (gracefully) when the window is closed. The previous technique is the way to go.



          The answer in Spring batch: Retry job if does not complete in particular time is also an option, however it will leave your job in a FAILED status (which is a restartable status as well). But you said it didn't work for you.



          2. How to restart the job automatically after the timeout?



          Now that you know how to stop a job after a timeout, you can use a RetryTemplate around the job launcher and re-launch the job when appropriate. Here is an example:



          public static void main(String[] args) throws Throwable 
          RetryTemplate retryTemplate = new RetryTemplate();
          retryTemplate.setRetryPolicy(new SimpleRetryPolicy(3));

          ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyJob.class);
          JobLauncher jobLauncher = applicationContext.getBean(JobLauncher.class);
          Job job = applicationContext.getBean(Job.class);
          JobParameters jobParameters = new JobParametersBuilder()
          .addDate("runtime", new Date())
          .toJobParameters();

          retryTemplate.execute((RetryCallback<JobExecution, Throwable>) retryContext ->
          JobExecution jobExecution = jobLauncher.run(job, jobParameters);
          if (jobExecution.getExitStatus().getExitCode().equals(ExitStatus.STOPPED.getExitCode()))
          throw new Exception("Job timeout");

          return jobExecution;
          );



          This will automatically re-run the job at most 3 times if it finishes with the status STOPPED (for example due to a timeout as shown previously).



          Hope this helps.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 8 at 16:04









          Mahmoud Ben HassineMahmoud Ben Hassine

          5,2551717




          5,2551717












          • This alternative worked fine for me. However, the ideal one for my case, would be to force retry (or skip) when specifically the timeout occurs by TransactionTimedOutException (with timeout on tasklet).

            – Gabriel Moreira
            Mar 11 at 13:26

















          • This alternative worked fine for me. However, the ideal one for my case, would be to force retry (or skip) when specifically the timeout occurs by TransactionTimedOutException (with timeout on tasklet).

            – Gabriel Moreira
            Mar 11 at 13:26
















          This alternative worked fine for me. However, the ideal one for my case, would be to force retry (or skip) when specifically the timeout occurs by TransactionTimedOutException (with timeout on tasklet).

          – Gabriel Moreira
          Mar 11 at 13:26





          This alternative worked fine for me. However, the ideal one for my case, would be to force retry (or skip) when specifically the timeout occurs by TransactionTimedOutException (with timeout on tasklet).

          – Gabriel Moreira
          Mar 11 at 13:26



















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55049424%2frestart-step-or-job-after-timeout-occours%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

          Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

          2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived