简体   繁体   English

Spring Batch:在容错步骤中调整事务属性

[英]Spring Batch: adjusting transaction properties in a fault tolerant step

I have a quite basic step in a Spring Batch job (using Java config): 我在Spring Batch作业中有一个非常基本的步骤(使用Java配置):

@Bean
public Step step1() {
    return stepBuilders.get("stepName")
        .<Object1, Void>chunk(50)
        .reader(reader(inputResource(null))
        .processor(processor())
        .listener(stepLogger())
        .transactionAttribute(transactionTimeoutAttribute(null))
        .build();
}

   .......

@Bean
@StepScope
public StepExecutionListener stepLogger() {
    return new StepLogger();
}

@Bean
@StepScope
public TransactionAttribute transactionTimeoutAttribute(
        @Value("#{jobParameters[transactionTimeout]}") Integer timeout) {
    timeout = timeout != null ? timeout : DEFAULT_TRANSACTION_TIMEOUT;
    RuleBasedTransactionAttribute transactionAttribute = new RuleBasedTransactionAttribute();
    transactionAttribute.setTimeout(timeout);
    return transactionTimeout;
}

As you can see, it's a requirement that the transaction timeout can be given as a job parameter. 如您所见,要求事务超时可以作为作业参数给出。 This works flawlessly, and if I set the transactionTimeout job parameter too low, the job execution will fail as transactions time out before the chunk completes. 这完美无缺,如果我将transactionTimeout作业参数设置得太低,则作业执行将失败,因为事务在块完成之前超时。

However, if I try to tack on fault tolerance (to be able to skip a certain amount of failed elements), everything falls apart. 但是,如果我试图提高容错能力(能够跳过一定数量的失败元素),一切都会崩溃。 When I add faultTolerant() to the step config to be able to specify the skip policy etc, like this: 当我将faultTolerant()添加到步骤配置中以便能够指定跳过策略等时,如下所示:

@Bean
public Step step1() {
    return stepBuilders.get("stepName")
        .<Object1, Void>chunk(50)
        .reader(reader(inputResource(null))
        .processor(processor())
        .faultTolerant()
        .listener(stepLogger())
        .transactionAttribute(transactionTimeoutAttribute(null))
        .build();
}

Spring can no longer start the context (on Jetty for now), and just throws the following exception on startup: Spring不能再启动上下文(现在在Jetty上),只是在启动时抛出以下异常:

BeanCreationException: Error creating bean with name 'scopedTarget.transactionTimeoutAttribute': Scope 'step' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No context holder available for step scope

What is the correct way of specifying both a transaction attribute and skip policy of a step in Spring Batch using Java Config? 使用Java Config在Spring Batch中指定事务属性和跳过策略的正确方法是什么?

EDIT: Just to make the question a bit more understandable, my requirement is to make a fault tolerant step where the transaction timeout is configurable as a job parameter. 编辑:只是为了使问题更容易理解,我的要求是做一个容错步骤,其中事务超时可配置为作业参数。 For non-fault tolerant step, that's not a problem, just make a step scoped TransactionAttribute bean with the job parameters wired in. But the FaultTolerantStepBuilder handles the transaction attribute differently (it basically merges the given transaction attribute with its internal one), so the step scope is not available. 对于非容错步骤,这不是问题,只需使用连接的作业参数创建一个步长范围的TransactionAttribute bean。但是FaultTolerantStepBuilder以不同方式处理事务属性(它基本上将给定的事务属性与其内部属性合并),因此步骤范围不可用。 How can I use the job parameters to configure the transaction attribute of a fault tolerant step (Java config)? 如何使用作业参数配置容错步骤(Java config)的事务属性?

If you are using Spring Batch 3 or newer you can mark the transaction attribute and step as @JobScope. 如果您使用的是Spring Batch 3或更新版本,则可以将事务属性标记为@JobScope。 This will prevent the fault tolerant step to access the transaction attribute too early. 这将阻止容错步骤过早访问事务属性。

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

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