首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >可以从spring batch的JobExecutionContext访问微线程吗

可以从spring batch的JobExecutionContext访问微线程吗
EN

Stack Overflow用户
提问于 2019-03-11 18:51:58
回答 2查看 331关注 0票数 1

我试图在不取消整个步骤/作业的情况下取消执行微线程。我已经实现了这个微线程,它可以根据一个标志被取消。但是JobExecutionContext没有提供任何访问微线程的媒介?有什么方法可以访问微线程实例吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-03-25 16:07:11

代码语言:javascript
复制
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;
        }
票数 0
EN

Stack Overflow用户

发布于 2019-03-11 21:08:43

使用decider可以做到这一点

代码语言:javascript
复制
    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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55100169

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档