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
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
add a comment |
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
add a comment |
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
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
spring-batch
edited Mar 7 at 18:27
Gabriel Moreira
asked Mar 7 at 17:13
Gabriel MoreiraGabriel Moreira
7517
7517
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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.
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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