我试图在不取消整个步骤/作业的情况下取消执行微线程。我已经实现了这个微线程,它可以根据一个标志被取消。但是JobExecutionContext没有提供任何访问微线程的媒介?有什么方法可以访问微线程实例吗?
发布于 2019-03-25 16:07:11
We can access an executing tasklet from jobRegistry
JobExecution jobExecution = findExecutionById(executionId);
try {
Job job = jobRegistry.getJob(jobExecution.getJobInstance().getJobName());
if (job instanceof StepLocator) {
// can only process as StepLocator is the only way to get the step object
// get the current stepExecution
for (StepExecution stepExecution : jobExecution.getStepExecutions()) {
if (stepExecution.getStatus().isRunning()) {
try {
// have the step execution that's running -> need to 'stop' it
Step step = ((StepLocator) job).getStep(stepExecution.getStepName());
if (step instanceof TaskletStep) {
//Implement your logic here }
}
} catch (NoSuchStepException e) {
logger.warn("Step not found", e);
throw new WorkflowServiceException("Step not found", e);
}
}
}
}
} catch (NoSuchJobException e) {
logger.warn("Cannot find Job object in the job registry. StoppableTasklet#stop() will not be called", e);
throw new WorkflowServiceException(
"Cannot find Job object in the job registry. StoppableTasklet#stop() will not be called", e);
}
return true;
}发布于 2019-03-11 21:08:43
使用decider可以做到这一点
public class MyDecider implements JobExecutionDecider {
@Override
public FlowExecutionStatus decide(JobExecution jobExecution, StepExecution stepExecution) {
if (condition) {
// choose what ever you want to do
return new FlowExecutionStatus("");
}
// choose what ever you want to do
return new FlowExecutionStatus("");
}
}例如,您可以在此处查看- https://docs.spring.io/spring-batch/trunk/reference/html/configureStep.html
https://stackoverflow.com/questions/55100169
复制相似问题