简体   繁体   English

是否可以从Spring Batch的JobExecutionContext访问Tasklet

[英]Is it possible to access Tasklet from JobExecutionContext of spring batch

I am trying to cancel execution of a tasklet without cancelling to entire step/job. 我试图取消执行tasklet而不取消整个步骤/作业。 I have implemented the tasklet in such a way that it can be cancelled based on a flag. 我已经实现了tasklet,可以根据标志将其取消。 But JobExecutionContext does not provide any medium to access the tasklet? 但是JobExecutionContext不提供任何媒介来访问Tasklet吗? Is there any way to access the tasklet instance? 有什么方法可以访问Tasklet实例?

this is possible with 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("");
    }
}

for example you can check here - https://docs.spring.io/spring-batch/trunk/reference/html/configureStep.html 例如,您可以在此处查看-https: //docs.spring.io/spring-batch/trunk/reference/html/configureStep.html

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;
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM