在Spring Batch中如何向作业启动器提供执行上下文?

时间:2016-01-28 09:01:54

标签: spring spring-batch

我想在执行上下文中添加一些变量 - 我的一个步骤需要它 - 在启动整个Job以进行测试之前(对DAO,读者,编写者,很酷的东西进行模拟......)。

使用给定的执行步骤启动一个步骤很简单:

@Autowired
@Qualifier("myjob")
protected  Job myjob;

// Enrich execution context
long myVariable = 42;
ExecutionContext context = new ExecutionContext();
context.putLong("response", myVariable);


// Initiate Launcher
JobLauncherTestUtils  jobLauncherTestUtils = new JobLauncherTestUtils();
jobLauncherTestUtils.setJobLauncher(jobLauncher);
jobLauncherTestUtils.setJob(myjob);
jobLauncherTestUtils.setJobRepository(jobRepository);

// Launch Step (with context) : OK
JobExecution jobExecution = jobLauncherTestUtils.launchStep("oneStepBeyond",jobParameters,context);

但是,在工作中,如何处理这个? 任何伎俩?

// Launch entire job with context (1) ?
JobExecution jobExecution = jobLauncher.run(jobCachePlugged,   jobParameters /* no parameter for context */);

// Launch entire job with context (2) ?
JobExecution jobExecution = new JobExecution(new Long(1));
jobExecution.setExecutionContext(context);
// ... but no way to run/start directly a JobExecution... arfff...

任何想法

很多

度过美好的一天

1 个答案:

答案 0 :(得分:1)

您是否看过StepExecutionListenerSupport? 它使您可以访问StepExecution,您可以从中访问某些范围。

ExecutionContext stepContext = stepExecution.getExecutionContext();
ExecutionContext jobContext = stepExecution.getJobExecution().getExecutionContext();