Announcement Announcement Module
Collapse
No announcement yet.
Starting or Stopping Spring Batch Jobs - Programmatically Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Starting or Stopping Spring Batch Jobs - Programmatically

    Hi,

    i am using Quartz 1.6.4 as the scheduler for Spring Batch 1.1.4 jobs in an enterprise web application, which uses struts + spring (including spring batch) + quartz. This web application allows users to "reschedule" (via cron expression) jobs (persisted in quartz jdbc job store) and to start/stop jobs based on business requirements.


    A single quartz job is used to start the spring batch jobs based on its schedule with the web applciation running. Whenever there are batch jobs running, users of the web application can "stop" or "start" batch jobs that are running.


    i've researched that it is currently not possible for an enterprise scheduler e.g. Quartz, to interact with the batch launcher to start/stop a batch job. Further, the interface JobOperator is not available in Spring Batch 1.1.4 to start/stop the batch jobs programmatically.


    I've tried to 1) to start/stop by using the JobExecution runtime instance, which is failing, as the JobExecution is always null (?), unless the job is completed. 2) Besides using the JobExecution, i've also tried to delete/change (job status) jobs instance/excution related records in the database (job repository) but to no avail.

    How can spring batch jobs be started/stopped programmatically?

    Thanks in advance. Your help is much appreciated.

  • #2
    I'm not sure about your first point, it shouldn't be null, unless you're launching synchronously, although then you wouldn't even get back the execution until it's complete.

    Without upgrading to 2.0, the only option I can think of is either the StepInterruptionPolicy (I think that's the name) or a ChunkListener (afterChunk) that checks either a flag in a database or something else, causing the job to stop or fail respectively.

    Comment


    • #3
      Managed to get batch jobs stopped

      thanks for the advice, i've managed to "stop" a batch job (in spring batch 1.1.4) via setting either the JobExecution/StepExecution instance in the WebApplicationContext's ServletContext as a session attribute, and calling the respective stop method.

      the JobExecution/StepExecution instance is obtained via the beforeStep method of a tasklet that extends StepExecutionListenerSupport. This instance is then set into the ServletContext in execute method of the same tasklet (when stepExecution.getJobExecution().isRunning() is true)

      the following is what i've observed for the job execution DB table when "stopping" batch jobs via either JobExecution or StepExecution:

      1) Stopping via JobExecution instance

      Job Execution Status: COMPLETED
      Outcome: No effect
      Exit Code: Completed

      Job Execution Status: STARTED
      Outcome: Status changed to STOPPED after completing current execution
      Exit Code: JOB_INTERRUPTED

      2) Stopping via StepExecution instance

      Job Execution Status: COMPLETED
      Outcome: No effect
      Exit Code: Completed

      Job Execution Status: STARTED
      Outcome: Status changed to COMPLETED after completing current execution, throws JobInterruptedException.
      Exit Code: Completed

      Comment

      Working...
      X