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

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

Introduction

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

Prototype

public void setJobRepository(JobRepository jobRepository) 

Source Link

Document

Public setter for the JobRepository that is needed to manage the state of the batch meta domain (jobs, steps, executions) during the life of a job.

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 va2s  .  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 2 s  .c  o  m
 * @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);
}

From source file:org.springframework.data.hadoop.admin.util.HadoopWorkflowUtils.java

/**
 * create and register Spring Batch job/*from  w w w . j a  v  a  2s.co  m*/
 * 
 * @param context root application context
 * @param artifacts workflow artifacts which include workflow descriptor and class loader information.
 * @throws Exception
 */
public static void createAndRegisterSpringBatchJob(ApplicationContext context,
        final WorkflowArtifacts artifacts) throws SpringHadoopAdminWorkflowException {
    if (context == null) {
        logger.warn("root applicaton context is null");
        throw new SpringHadoopAdminWorkflowException("root applicaton context is null");
    }
    String workflowDescriptor = getWorkflowDescriptor(artifacts);
    logger.info("create spring batch job:" + workflowDescriptor + ", classloader:"
            + artifacts.getWorkflowClassLoader());

    final FileSystemXmlApplicationContext ctx = new FileSystemXmlApplicationContext();
    ctx.setClassLoader(artifacts.getWorkflowClassLoader());
    ctx.setConfigLocation(HadoopWorkflowUtils.fileURLPrefix + workflowDescriptor);

    try {
        SimpleJob job = new SimpleJob(generateSpringBatchJobName(artifacts));
        TaskletStep step = new TaskletStep(springHadoopTaskName);
        SimpleSpringHadoopTasklet tasklet = new SimpleSpringHadoopTasklet();
        tasklet.setContext(ctx);
        step.setTasklet(tasklet);
        JobRepository jobRepository = context.getBean(JobRepository.class);
        DataSourceTransactionManager transactionManager = context.getBean("transactionManager",
                DataSourceTransactionManager.class);
        step.setTransactionManager(transactionManager);
        step.setJobRepository(jobRepository);
        step.afterPropertiesSet();
        job.addStep(step);
        JobRegistry jobRegistry = context.getBean("jobRegistry", JobRegistry.class);
        job.setJobRepository(jobRepository);
        job.afterPropertiesSet();
        JobFactory jobFactory = new ReferenceJobFactory(job);
        jobRegistry.register(jobFactory);
    } catch (Exception e) {
        logger.warn("create and register Spring Batch Job failed");
        throw new SpringHadoopAdminWorkflowException("create and register Spring Batch Job failed", e);
    }
}