Stopping a Job in the beforeStep in Spring Batch


I want to be able to stop a job when a timing threshold is met. There are 2 approaches I was thinking about. First was to stop the job in the afterStep. However, I do not want it to have a Stopped status if it is at the completion of the last step. Therefore, I am going with stopping it in the beforeStep. I tried experimenting with

public void beforeStep(StepExecution stepExecution) { stepExecution.setStatus(BatchStatus.STOPPED); return; }


public void beforeStep(StepExecution stepExecution) { stepExecution.setExitStatus(ExitStatus.STOPPED); return; }

Neither of these worked. Any suggestions?


The problem with the above code is that stops the currently running step, not the entire job. Perhaps if you add and adjust the following code in the job-current-step configuration will terminate the job when the ExitStatus of the step is STOPPED and when runned again the job will start from the next step.<br />(Taken from <a href="http://docs.spring.io/spring-batch/reference/html/configureStep.html#configuringForStop" rel="nofollow">here</a>)

<step id="step1" parent="s1"> <stop on="STOPPED" restart="step2"/> </step>

<br /><a href="http://docs.spring.io/spring-batch/reference/html/configureStep.html#batchStatusVsExitStatus" rel="nofollow">This</a> might also help solve the confusion between ExitStatus/BatchStatus.

About the timing threshold... As I understand so far you plan to measure time of the <strong>current step only</strong> (beforestep - afterstep), no matter how long previous steps were running. To calculate and the previous steps try adding a timestamp in JobParameters and check when that threshold is passed.


In my case a needed to stop job from afterStep. So I returned ExitStatus.Unknown and catch from job like that:


So the job finish without error and I can restart the entire job when i want, whitout "restartable" option.

Hope this can help you.


i suggest looking at <a href="http://docs.spring.io/spring-batch/reference/html/repeat.html#completionPolicies" rel="nofollow">CompletionPolicies</a> especially the <a href="http://docs.spring.io/spring-batch/apidocs/org/springframework/batch/repeat/policy/TimeoutTerminationPolicy.html" rel="nofollow">TimeoutTerminationPolicy</a>


You can set a timer (or something similar) and when receive time-limit notitication stop the job as described in <a href="http://docs.spring.io/spring-batch/reference/html-single/index.html#stoppingAJob" rel="nofollow">Stopping a job</a>.<br /> If you want the job is completed normally even if a time-alarm were broadcasted during last step, you can do check (using <a href="http://docs.spring.io/spring-batch/reference/html-single/index.html#JobOperator" rel="nofollow">JobOperator</a>) to query job progress.


  • Python: Respond to Command Line Prompts
  • QProcess not emitting its signals when not waitForFinished()
  • how to auto center objects in a form in access 2007?
  • Linear gradient not applying in Webkit with d3 generated SVG
  • Jhipster: How to create relationships with User entity using supplied tools?
  • Can I use worksheet_change for a specific column only?
  • How to change default stop edit behavior in jtable
  • What is this strange character in chrome's resource css viewer?
  • Time out Error in send mail
  • quiver not drawing arrows just lots of blue, matlab
  • Suppressing passwd when calling sqlplus from shell script
  • How to run “Deployd” on port 80 instead of port 5000 in webserver.
  • Magento Fatal error: Maximum execution error solution, on WAMP
  • Nant, Vault & Windows Integrated Authentication
  • Regex thinks I'm nesting, but I'm not
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • Fetching methods from BroadcastReceiver to update UI
  • Bug in WPF DataGrid
  • TFS: Get latest causes slow project reloading
  • Controls, properties, events and timers running in design time
  • Running a C# exe file
  • Join two tables and save into third-sql
  • vba code to select only visible cells in specific column except heading
  • How to model a transition system with SPIN
  • ActionScript 2 vs ActionScript 3 performance
  • ORA-29908: missing primary invocation for ancillary operator
  • How can I estimate amount of memory left with calling System.gc()?
  • Apache 2.4 - remove | delete | uninstall
  • log4net write single file for each call to log.info
  • Acquiring multiple attributes from .xml file in c#
  • Angular 2 constructor injection vs direct access
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Java static initializers and reflection
  • Exception on Android 4.0 `android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode)`
  • Android Google Maps API OnLocationChanged only called once
  • How can I remove ASP.NET Designer.cs files?
  • UserPrincipal.Current returns apppool on IIS
  • Converting MP3 duration time
  • java string with new operator and a literal