Announcement Announcement Module
No announcement yet.
How to stop a job gracefully. Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to stop a job gracefully.


    I have a shell script which is responsible for starting the job.

    This is how I start the job in the ""

    java andLineJobRunner format077-context.xml format077Job batchcycledate=04-JUN-08

    Sometimes this job takes many hours to complete. So we need to stop the job to serve other priorities and then restart it at later time. I want to know is there a graceful way to stop this job ? Do I need to write another shell script for stopping the job?

  • #2
    With Spring Batch 1.x you have to keep a reference to the JobExecution to stop it gracefully, so the CommandLineJobRunner is not going to help because it either blocks until the job has finished or returns immediately without waiting. You could write your own wrapper or crib from the JMX sample (adhoc-joblauncher-context.xml in the samples project). In 2.0 you will be able to write your second shell script and pass it the JobExecution ID (see work in progress on


    • #3
      what if the job runs infinitely?

      I am using the CommandLineJobLauncher and i have a single job(with 2 basic steps).

      When i start the commandline launcher, it takes care of the read and write of the step and repeats them again and again.

      How do i tell through configuration to stop the job after running it only once?


      • #4
        Even if launching from the command line, you could register the JobExecution with an mBean server, which would then be able to stop the execution.


        • #5
          old post ... but as long as you are using a database to persist your executions you should be able to stop them, cant you lucas??

          I am using this code to pull off the status' right now

          BatchStatus status = BatchStatus.UNKNOWN;

          Properties props = new Properties();
          props.put("collectionId", collectionId);

          JobInstance jobInst = jobInstanceDao.getJobInstance(

          if (jobInst != null) {
          JobExecution jobExecution = jobExecutionDao.getLastJobExecution(jobInst);

          // check if we have a record of this job.
          // if not lets return unkown.
          if (jobExecution != null) {
          status = jobExecution.getStatus();
          So you have accss the execution up there. Of course this is assuming that this is your last job.


          • #6
            In 1.x, there aren't any checks made while executing to see if updates have been made. In 2.0, the check has been added, along with implementations to do the same thing you just described.


            • #7
              I just found this old thread, and I have a question related to it.

              lucasward said that in 1.x there are no checks made while executing to see if there have been updates. So, if I load a JobExecution via a DAO (like jnusaira's example) and call stop, will anything happen?

              The application I am creating with Spring Batch allows certain admin users to start a Batch job of their own. Since the servers are load balanced, I can't guarantee the user will send the request to stop the job to the same server that is executing it. I would like the user to be able to stop the execution. Will I have to implement something on my own to communicate the stop request? We aren't cleared to use Spring Batch 2 yet, so I have to do this with Batch 1.x.


              • #8
                Your first assumption is correct, if you use Spring Batch 1.x and just set the status, which can be easily done in 2.0 via JobOperator.stop(), nothing will happen. This is because Spring Batch 2.0 checks the database, while 1.1 does not. If you like, you can write this same type of funcationality into 1.1 by injected a custom StepInteruptionPolicy, that uses a Dao you code to check the database. It's a lot of extra work, but if you can't use 2.0 that's the only option.


                • #9
                  Thank you for the reply! I didn't know about the StepInterruptionPolicy. While it may be work to setup, it seems it will be a lot easier than what I had in mind. Thanks for saving me a few hours