1

我正在寻求帮助以找到在我的块侦听器中设置作业执行上下文的方法。

 @Bean
  public Step testStep(
      JdbcCursorItemReader<TestStep> testStageDataReader,
      TestStepProcessor testStepProcessor,
      CompositeItemWriter<Writer> testWriter,
      PlatformTransactionManager transactionManager,
      JobRepository jobRepository, TestChunkListener testChunkListener) {
    return stepBuilderFactory
        .get("TESTING")
        .<>chunk(100)
        .reader(testStageDataReader)
        .processor(testStepProcessor)
        .writer(testWriter)
        .faultTolerant()
        .skip(DataIntegrityViolationException.class)
        .skipLimit(4)
        .listener(testSkipListener)
        .transactionManager(transactionManager)
        .repository(jobRepository)
        .build();
  }

跳过监听器

@Component
@RequiredArgsConstructor
@Slf4j
@StepScope
public class TestSkipListener {

  private final TestRepository testRepository;
  @Value("#{jobExecutionContext['jobExecutionTestId']}")
  private Long jobExecutionTestId; //this is null

  @OnSkipInWrite
  public void onSkipInWrite(Writer writer,
                            Throwable t) {
    logException(t);

  }
}

我正在设置作业执行 ID

@Component
@Slf4j
@RequiredArgsConstructor
public class TestJobListener extends JobExecutionListenerSupport {

  @Override
  public void beforeJob(JobExecution jobExecution) {
    long jobExecutionId = jobExecution.getJobId();
    jobExecution.getExecutionContext()
                .put("jobExecutionTestId", jobExecutionId);
  }
}

这在我的处理器和编写器中工作正常,但在跳过侦听器时,它是空的

4

1 回答 1

2

在这段时间之后,你肯定找到了方法。但是我遇到了同样的情况,找到了你的帖子,最后自己想出了一个解决方案。让我们在这里为下一个卡住的开发人员分享它:

您可以使用加载执行上下文@Value。关键取决于Scope您在课堂上使用的内容。

@工作范围 :

@Component
@JobScope
public class MySkipListener implements SkipListener<T, T> {

    @Value("#{jobExecution.executionContext}")
    private ExecutionContext executionContext;

    @Override
    public void onSkipInRead(Throwable throwable) {
        String myVariableInContext = executionContext.get("myVariableKey");
        // do your stuff
    }

@StepScope:

@Component
@StepScope
public class MySkipListener implements SkipListener<T, T> {

    @Value("#{stepExecution.jobExecution.executionContext}")
    private ExecutionContext executionContext;

    @Override
    public void onSkipInRead(Throwable throwable) {
        String myVariableInContext = executionContext.get("myVariableKey");
        // do your stuff
    }
于 2021-01-21T18:32:22.690 回答