Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/184.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 如何在RetryContext中获取JobId?_Spring_Spring Batch_Spring Retry - Fatal编程技术网

Spring 如何在RetryContext中获取JobId?

Spring 如何在RetryContext中获取JobId?,spring,spring-batch,spring-retry,Spring,Spring Batch,Spring Retry,我只是把我的问题扩展到这里- 如何在RetryContext中获取JobId 我通过链接:,但仍然不知道 @Component @Slf4j public class RecoveryCallback implements RecoveryCallback<String>{ @Autowired private NamedParameterJdbcTemplate namedJdbcTemplate; @Autowired private A

我只是把我的问题扩展到这里-

如何在
RetryContext
中获取
JobId

我通过链接:,但仍然不知道

@Component
@Slf4j
public class RecoveryCallback implements RecoveryCallback<String>{
    @Autowired
    private NamedParameterJdbcTemplate namedJdbcTemplate;
    
    @Autowired
    private AbcService abcService;
    
    @Value("#{stepExecution.jobExecution.jobId}")
    private Long jobId;
        
    @Override
    public String recover(RetryContext context) throws Exception {
        log.warn("RecoveryCallback | recover is executed ...");
        
        ErrorLog errorLog = ErrorLog.builder()
                .jobName("ABC")
                .stepName("RETRY_STEP")
                .stepType("RETRY")
                ....
                ....
                ....
                .jobId(jobId)
                .build();
        abcService.updateErrLog(errorLog);
        
        return "Batch Job Retried and exausted with all attemps";
    }
}
@组件
@Slf4j
公共类RecoveryCallback实现RecoveryCallback{
@自动连线
私有名称参数jdbc模板名称djdbctemplate;
@自动连线
私人abc服务abc服务;
@值(“#{stepExecution.jobExecution.jobId}”)
私人长期工单;
@凌驾
公共字符串恢复(RetryContext上下文)引发异常{
log.warn(“RecoveryCallback |已执行恢复…”);
ErrorLog ErrorLog=ErrorLog.builder()
.工作名称(“ABC”)
.stepName(“重试步骤”)
.stepType(“重试”)
....
....
....
.jobId(jobId)
.build();
abcService.updateErrLog(errorLog);
返回“重试批处理作业并检查所有调度”;
}
}

因为您正在Springbean的一个字段中注入
stepExecution.jobExecution.jobId
,所以需要将这个bean
Step
范围化。在这种方法中,不使用RetryContext

如果要使用retry上下文,则需要首先将jobId放在retryable方法的上下文中。从您的链接问题:

retryTemplate.execute(retryContext  -> {
        JobExecution jobExecution = jobLauncher.run(sampleAcctJob, pdfParams);
        if(!jobExecution.getAllFailureExceptions().isEmpty()) {
            log.error("============== sampleAcctJob Job failed, retrying.... ================");
            throw jobExecution.getAllFailureExceptions().iterator().next();
        }
        logDetails(jobExecution);
        // PUT JOB ID in retryContext
        retryContext.setAttribute("jobId", jobExecution.getExecutionId());
        return jobExecution;
    });
这样,您就可以通过
recover
方法从上下文中获取作业ID:

@Override
public String recover(RetryContext context) throws Exception {
    log.warn("RecoveryCallback | recover is executed ...");
    
    ErrorLog errorLog = ErrorLog.builder()
            .jobName("ABC")
            .stepName("RETRY_STEP")
            .stepType("RETRY")
            ....
            ....
            .jobId(context.getAttribute("jobId"))
            .build();
    abcService.updateErrLog(errorLog);
    
    return "Batch Job Retried and exausted with all attemps";
}

由于您正在Springbean的一个字段中注入
stepExecution.jobExecution.jobId
,因此需要对这个bean
Step
进行范围限定。在这种方法中,不使用RetryContext

如果要使用retry上下文,则需要首先将jobId放在retryable方法的上下文中。从您的链接问题:

retryTemplate.execute(retryContext  -> {
        JobExecution jobExecution = jobLauncher.run(sampleAcctJob, pdfParams);
        if(!jobExecution.getAllFailureExceptions().isEmpty()) {
            log.error("============== sampleAcctJob Job failed, retrying.... ================");
            throw jobExecution.getAllFailureExceptions().iterator().next();
        }
        logDetails(jobExecution);
        // PUT JOB ID in retryContext
        retryContext.setAttribute("jobId", jobExecution.getExecutionId());
        return jobExecution;
    });
这样,您就可以通过
recover
方法从上下文中获取作业ID:

@Override
public String recover(RetryContext context) throws Exception {
    log.warn("RecoveryCallback | recover is executed ...");
    
    ErrorLog errorLog = ErrorLog.builder()
            .jobName("ABC")
            .stepName("RETRY_STEP")
            .stepType("RETRY")
            ....
            ....
            .jobId(context.getAttribute("jobId"))
            .build();
    abcService.updateErrLog(errorLog);
    
    return "Batch Job Retried and exausted with all attemps";
}

谢谢,我尝试添加StepScope等,但我得到了错误proxyScope等。。看来我没办法在这里找到工作。您可以进一步建议吗?您可以使用第二种方法,将jobId放入可重试方法(
context.setAttribute(“jobId”,jobExecution.getExecutionId();
)的上下文中,然后在恢复方法(
.jobId(context.getAttribute(“jobId”))
)中访问它。谢谢检查,
jobExecution.getJobId()
获取JobId的正确方法是什么?
JobExecution.getJobId()
返回作业实例id。
我在回答中使用的JobExecution.getExecutionId()
返回作业执行id。根据您的要求使用您需要的id。谢谢,我尝试添加StepScope等,但得到了错误proxyScope等。。看来我没办法在这里找到工作。您可以进一步建议吗?您可以使用第二种方法,将jobId放入可重试方法(
context.setAttribute(“jobId”,jobExecution.getExecutionId();
)的上下文中,然后在恢复方法(
.jobId(context.getAttribute(“jobId”))
)中访问它。谢谢检查,
jobExecution.getJobId()
获取作业id的正确方法是什么?
JobExecution.getJobId()
返回作业实例id。
JobExecution.getExecutionId()
我在回答中使用的返回作业执行id。根据需要使用一个。