Example usage for org.springframework.batch.core.job SimpleJob setJobExecutionListeners

List of usage examples for org.springframework.batch.core.job SimpleJob setJobExecutionListeners

Introduction

In this page you can find the example usage for org.springframework.batch.core.job SimpleJob setJobExecutionListeners.

Prototype

public void setJobExecutionListeners(JobExecutionListener[] listeners) 

Source Link

Document

Public setter for injecting JobExecutionListener s.

Usage

From source file:trionsoft.test.StepRunner.java

/**
 * Launch just the specified step as its own job. An IllegalStateException
 * is thrown if there is no Step with the given name.
 * //  w  w w  . j  a v a  2 s  .c o  m
 * @param stepName The name of the step to launch
 * @param jobParameters The JobParameters to use during the launch
 * @param jobExecutionContext An ExecutionContext whose values will be
 * loaded into the Job ExecutionContext prior to launching the step.
 * @return JobExecution
 */
public JobExecution launchStep(Step step, JobParameters jobParameters,
        final ExecutionContext jobExecutionContext) {
    //
    // Create a fake job
    //
    SimpleJob job = new SimpleJob();
    job.setName("TestJob");
    job.setJobRepository(this.jobRepository);

    List<Step> stepsToExecute = new ArrayList<Step>();
    stepsToExecute.add(step);
    job.setSteps(stepsToExecute);

    //
    // Dump the given Job ExecutionContext using a listener
    //
    if (jobExecutionContext != null && !jobExecutionContext.isEmpty()) {
        job.setJobExecutionListeners(new JobExecutionListener[] { new JobExecutionListenerSupport() {
            public void beforeJob(JobExecution jobExecution) {
                ExecutionContext jobContext = jobExecution.getExecutionContext();
                for (Map.Entry<String, Object> entry : jobExecutionContext.entrySet()) {
                    jobContext.put(entry.getKey(), entry.getValue());
                }
            }
        } });
    }

    //
    // Launch the job
    //
    return this.launchJob(job, jobParameters);
}

From source file:org.springframework.batch.test.StepRunner.java

/**
 * Launch just the specified step as its own job. An IllegalStateException
 * is thrown if there is no Step with the given name.
 *
 * @param step The step to launch//from w ww  .  j a v  a 2s  . com
 * @param jobParameters The JobParameters to use during the launch
 * @param jobExecutionContext An ExecutionContext whose values will be
 * loaded into the Job ExecutionContext prior to launching the step.
 * @return JobExecution
 */
public JobExecution launchStep(Step step, JobParameters jobParameters,
        final ExecutionContext jobExecutionContext) {
    //
    // Create a fake job
    //
    SimpleJob job = new SimpleJob();
    job.setName("TestJob");
    job.setJobRepository(this.jobRepository);

    List<Step> stepsToExecute = new ArrayList<Step>();
    stepsToExecute.add(step);
    job.setSteps(stepsToExecute);

    //
    // Dump the given Job ExecutionContext using a listener
    //
    if (jobExecutionContext != null && !jobExecutionContext.isEmpty()) {
        job.setJobExecutionListeners(new JobExecutionListener[] { new JobExecutionListenerSupport() {
            @Override
            public void beforeJob(JobExecution jobExecution) {
                ExecutionContext jobContext = jobExecution.getExecutionContext();
                for (Map.Entry<String, Object> entry : jobExecutionContext.entrySet()) {
                    jobContext.put(entry.getKey(), entry.getValue());
                }
            }
        } });
    }

    //
    // Launch the job
    //
    return this.launchJob(job, jobParameters);
}