Results 1 to 9 of 9

Thread: How to stop a job gracefully.

  1. #1
    Join Date
    Aug 2008
    Posts
    19

    Question How to stop a job gracefully.

    Hi

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

    This is how I start the job in the "bi.sh"

    java org.springframework.batch.core.launch.support.Comm 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. #2
    Join Date
    Jun 2005
    Posts
    4,230

    Default

    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 http://jira.springframework.org/browse/BATCH-773).

  3. #3
    Join Date
    Nov 2006
    Location
    Dallas, TX
    Posts
    15

    Default 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. #4
    Join Date
    Dec 2006
    Posts
    1,061

    Default

    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. #5

    Default

    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(
    getJob(collectionType),
    converter.getJobParameters(props));

    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. #6
    Join Date
    Dec 2006
    Posts
    1,061

    Default

    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. #7
    Join Date
    Nov 2006
    Posts
    3

    Default

    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. #8
    Join Date
    Dec 2006
    Posts
    1,061

    Default

    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. #9
    Join Date
    Nov 2006
    Posts
    3

    Default

    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

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •