org.springframework.batch.core.jsr.launch

## Class JsrJobOperator

- java.lang.Object
- - org.springframework.batch.core.jsr.launch.JsrJobOperator

- All Implemented Interfaces:
    javax.batch.operations.JobOperator, org.springframework.beans.factory.InitializingBean

      public class JsrJobOperator
      extends java.lang.Object
      implements javax.batch.operations.JobOperator, org.springframework.beans.factory.InitializingBean

  The entrance for executing batch jobs as defined by JSR-352. This class provides a single base `ApplicationContext` that is the equivalent to the following: <beans> <batch:job-repository id="jobRepository" ... /> <bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher"> ... </bean> <bean id="batchJobOperator" class="org.springframework.batch.core.launch.support.SimpleJobOperator"> ... </bean> <bean id="jobExplorer" class="org.springframework.batch.core.explore.support.JobExplorerFactoryBean"> ... </bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> ... </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> ... </bean> <bean id="jobParametersConverter" class="org.springframework.batch.core.jsr.JsrJobParametersConverter"/> <bean id="jobRegistry" class="org.springframework.batch.core.configuration.support.MapJobRegistry"/> <bean id="placeholderProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> ... </bean> </beans> Calls to `JobOperator.start(String, Properties)` will provide a child context to the above context using the job definition and batch.xml if provided. By default, calls to start/restart will result in asynchronous execution of the batch job (via an asynchronous `TaskExecutor`. For synchronous behavior or customization of thread behavior, a different `TaskExecutor` implementation is required to be provided. Note: This class is intended to only be used for JSR-352 configured jobs. Use of this `JobOperator` to start/stop/restart Spring Batch jobs may result in unexpected behaviors due to how job instances are identified differently.
  Since:
    3.0
  Author:
    Michael Minella, Chris Schaefer

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `JsrJobOperator()` Public constructor used by `BatchRuntime.getJobOperator()`.
    `JsrJobOperator(JobExplorer jobExplorer, JobRepository jobRepository, JobParametersConverter jobParametersConverter)` The no-arg constructor is used by the `BatchRuntime.getJobOperator()` and so bootstraps an `ApplicationContext`.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `abandon(long jobExecutionId)`
    `void` | `afterPropertiesSet()`
    `javax.batch.runtime.JobExecution` | `getJobExecution(long executionId)`
    `java.util.List<javax.batch.runtime.JobExecution>` | `getJobExecutions(javax.batch.runtime.JobInstance jobInstance)`
    `javax.batch.runtime.JobInstance` | `getJobInstance(long executionId)`
    `int` | `getJobInstanceCount(java.lang.String jobName)`
    `java.util.List<javax.batch.runtime.JobInstance>` | `getJobInstances(java.lang.String jobName, int start, int count)`
    `java.util.Set<java.lang.String>` | `getJobNames()`
    `protected java.util.Properties` | `getJobRestartProperties(java.util.Properties params, JobExecution previousJobExecution)`
    `java.util.Properties` | `getParameters(long executionId)`
    `java.util.List<java.lang.Long>` | `getRunningExecutions(java.lang.String name)`
    `java.util.List<javax.batch.runtime.StepExecution>` | `getStepExecutions(long executionId)`
    `protected org.springframework.core.task.TaskExecutor` | `getTaskExecutor()`
    `long` | `restart(long executionId, java.util.Properties params)` Creates a child `ApplicationContext` for the job being requested based upon the /META-INF/batch.xml (if exists) and the /META-INF/batch-jobs/<jobName>.xml configuration and restart the job.
    `void` | `setJobExplorer(JobExplorer jobExplorer)`
    `void` | `setJobParametersConverter(JobParametersConverter converter)` Used to convert the `Properties` objects used by JSR-352 to the `JobParameters` objects used in Spring Batch.
    `void` | `setJobRepository(JobRepository jobRepository)`
    `void` | `setTaskExecutor(org.springframework.core.task.TaskExecutor taskExecutor)`
    `long` | `start(java.lang.String jobName, java.util.Properties params)` Creates a child `ApplicationContext` for the job being requested based upon the /META-INF/batch.xml (if exists) and the /META-INF/batch-jobs/<jobName>.xml configuration and launches the job.
    `void` | `stop(long executionId)` Stops the running job execution if it is currently running.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### JsrJobOperator

          public JsrJobOperator()

      Public constructor used by `BatchRuntime.getJobOperator()`. This will bootstrap a singleton ApplicationContext if one has not already been created (and will utilize the existing one if it has) to populate itself.

    - #### JsrJobOperator

          public JsrJobOperator(JobExplorer jobExplorer,
                                JobRepository jobRepository,
                                JobParametersConverter jobParametersConverter)

      The no-arg constructor is used by the `BatchRuntime.getJobOperator()` and so bootstraps an `ApplicationContext`. This constructor does not and is therefore dependency injection friendly. Also useful for unit testing.
      Parameters:
        `jobExplorer` - an instance of Spring Batch's `JobExplorer`
        `jobRepository` - an instance of Spring Batch's `JobOperator`
        `jobParametersConverter` - an instance of Spring Batch's `JobParametersConverter`

  - ### Method Detail

    - #### setJobExplorer

          public void setJobExplorer(JobExplorer jobExplorer)

    - #### setJobRepository

          public void setJobRepository(JobRepository jobRepository)

    - #### setTaskExecutor

          public void setTaskExecutor(org.springframework.core.task.TaskExecutor taskExecutor)

    - #### getTaskExecutor

          protected org.springframework.core.task.TaskExecutor getTaskExecutor()

    - #### afterPropertiesSet

          public void afterPropertiesSet()
                                  throws java.lang.Exception

      Specified by:
        `afterPropertiesSet` in interface `org.springframework.beans.factory.InitializingBean`
      Throws:
        `java.lang.Exception`

    - #### setJobParametersConverter

          public void setJobParametersConverter(JobParametersConverter converter)

      Used to convert the `Properties` objects used by JSR-352 to the `JobParameters` objects used in Spring Batch. The default implementation used will configure all parameters to be non-identifying (per the JSR).
      Parameters:
        `converter` - A `Converter` implementation used to convert `Properties` to `JobParameters`

    - #### abandon

          public void abandon(long jobExecutionId)
                       throws javax.batch.operations.NoSuchJobExecutionException,
                              javax.batch.operations.JobExecutionIsRunningException,
                              javax.batch.operations.JobSecurityException

      Specified by:
        `abandon` in interface `javax.batch.operations.JobOperator`
      Throws:
        `javax.batch.operations.NoSuchJobExecutionException`
        `javax.batch.operations.JobExecutionIsRunningException`
        `javax.batch.operations.JobSecurityException`

    - #### getJobExecution

          public javax.batch.runtime.JobExecution getJobExecution(long executionId)
                                                           throws javax.batch.operations.NoSuchJobExecutionException,
                                                                  javax.batch.operations.JobSecurityException

      Specified by:
        `getJobExecution` in interface `javax.batch.operations.JobOperator`
      Throws:
        `javax.batch.operations.NoSuchJobExecutionException`
        `javax.batch.operations.JobSecurityException`

    - #### getJobExecutions

          public java.util.List<javax.batch.runtime.JobExecution> getJobExecutions(javax.batch.runtime.JobInstance jobInstance)
                                                                            throws javax.batch.operations.NoSuchJobInstanceException,
                                                                                   javax.batch.operations.JobSecurityException

      Specified by:
        `getJobExecutions` in interface `javax.batch.operations.JobOperator`
      Throws:
        `javax.batch.operations.NoSuchJobInstanceException`
        `javax.batch.operations.JobSecurityException`

    - #### getJobInstance

          public javax.batch.runtime.JobInstance getJobInstance(long executionId)
                                                         throws javax.batch.operations.NoSuchJobExecutionException,
                                                                javax.batch.operations.JobSecurityException

      Specified by:
        `getJobInstance` in interface `javax.batch.operations.JobOperator`
      Throws:
        `javax.batch.operations.NoSuchJobExecutionException`
        `javax.batch.operations.JobSecurityException`

    - #### getJobInstanceCount

          public int getJobInstanceCount(java.lang.String jobName)
                                  throws javax.batch.operations.NoSuchJobException,
                                         javax.batch.operations.JobSecurityException

      Specified by:
        `getJobInstanceCount` in interface `javax.batch.operations.JobOperator`
      Throws:
        `javax.batch.operations.NoSuchJobException`
        `javax.batch.operations.JobSecurityException`

    - #### getJobInstances

          public java.util.List<javax.batch.runtime.JobInstance> getJobInstances(java.lang.String jobName,
                                                                                 int start,
                                                                                 int count)
                                                                          throws javax.batch.operations.NoSuchJobException,
                                                                                 javax.batch.operations.JobSecurityException

      Specified by:
        `getJobInstances` in interface `javax.batch.operations.JobOperator`
      Throws:
        `javax.batch.operations.NoSuchJobException`
        `javax.batch.operations.JobSecurityException`

    - #### getJobNames

          public java.util.Set<java.lang.String> getJobNames()
                                                      throws javax.batch.operations.JobSecurityException

      Specified by:
        `getJobNames` in interface `javax.batch.operations.JobOperator`
      Throws:
        `javax.batch.operations.JobSecurityException`

    - #### getParameters

          public java.util.Properties getParameters(long executionId)
                                             throws javax.batch.operations.NoSuchJobExecutionException,
                                                    javax.batch.operations.JobSecurityException

      Specified by:
        `getParameters` in interface `javax.batch.operations.JobOperator`
      Throws:
        `javax.batch.operations.NoSuchJobExecutionException`
        `javax.batch.operations.JobSecurityException`

    - #### getRunningExecutions

          public java.util.List<java.lang.Long> getRunningExecutions(java.lang.String name)
                                                              throws javax.batch.operations.NoSuchJobException,
                                                                     javax.batch.operations.JobSecurityException

      Specified by:
        `getRunningExecutions` in interface `javax.batch.operations.JobOperator`
      Throws:
        `javax.batch.operations.NoSuchJobException`
        `javax.batch.operations.JobSecurityException`

    - #### getStepExecutions

          public java.util.List<javax.batch.runtime.StepExecution> getStepExecutions(long executionId)
                                                                              throws javax.batch.operations.NoSuchJobExecutionException,
                                                                                     javax.batch.operations.JobSecurityException

      Specified by:
        `getStepExecutions` in interface `javax.batch.operations.JobOperator`
      Throws:
        `javax.batch.operations.NoSuchJobExecutionException`
        `javax.batch.operations.JobSecurityException`

    - #### restart

          public long restart(long executionId,
                              java.util.Properties params)
                       throws javax.batch.operations.JobExecutionAlreadyCompleteException,
                              javax.batch.operations.NoSuchJobExecutionException,
                              javax.batch.operations.JobExecutionNotMostRecentException,
                              javax.batch.operations.JobRestartException,
                              javax.batch.operations.JobSecurityException

      Creates a child `ApplicationContext` for the job being requested based upon the /META-INF/batch.xml (if exists) and the /META-INF/batch-jobs/<jobName>.xml configuration and restart the job.
      Specified by:
        `restart` in interface `javax.batch.operations.JobOperator`
      Parameters:
        `executionId` - the database id of the job execution to be restarted.
        `params` - any job parameters to be used during the execution of this job.
      Throws:
        `javax.batch.operations.JobExecutionAlreadyCompleteException` - thrown if the requested job execution has a status of COMPLETE
        `javax.batch.operations.NoSuchJobExecutionException` - throw if the requested job execution does not exist in the repository
        `javax.batch.operations.JobExecutionNotMostRecentException` - thrown if the requested job execution is not the most recent attempt for the job instance it's related to.
        `javax.batch.operations.JobRestartException` - thrown for any general errors during the job restart process
        `javax.batch.operations.JobSecurityException`

    - #### getJobRestartProperties

          protected java.util.Properties getJobRestartProperties(java.util.Properties params,
                                                                 JobExecution previousJobExecution)

    - #### start

          public long start(java.lang.String jobName,
                            java.util.Properties params)
                     throws javax.batch.operations.JobStartException,
                            javax.batch.operations.JobSecurityException

      Creates a child `ApplicationContext` for the job being requested based upon the /META-INF/batch.xml (if exists) and the /META-INF/batch-jobs/<jobName>.xml configuration and launches the job. Per JSR-352, calls to this method will always create a new `JobInstance` (and related `JobExecution`).
      Specified by:
        `start` in interface `javax.batch.operations.JobOperator`
      Parameters:
        `jobName` - the name of the job XML file without the .xml that is located within the /META-INF/batch-jobs directory.
        `params` - any job parameters to be used during the execution of this job.
      Throws:
        `javax.batch.operations.JobStartException`
        `javax.batch.operations.JobSecurityException`

    - #### stop

          public void stop(long executionId)
                    throws javax.batch.operations.NoSuchJobExecutionException,
                           javax.batch.operations.JobExecutionNotRunningException,
                           javax.batch.operations.JobSecurityException

      Stops the running job execution if it is currently running.
      Specified by:
        `stop` in interface `javax.batch.operations.JobOperator`
      Parameters:
        `executionId` - the database id for the `JobExecution` to be stopped.
      Throws:
        `javax.batch.operations.NoSuchJobExecutionException`
        `javax.batch.operations.JobExecutionNotRunningException`
        `javax.batch.operations.JobSecurityException`