簡體   English   中英

是否可以從Spring Batch的JobExecutionContext訪問Tasklet

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

我試圖取消執行tasklet而不取消整個步驟/作業。 我已經實現了tasklet,可以根據標志將其取消。 但是JobExecutionContext不提供任何媒介來訪問Tasklet嗎? 有什么方法可以訪問Tasklet實例?

決定者可以做到

    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

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