gpt4 book ai didi

java - Spring批量默认重试策略

转载 作者:太空宇宙 更新时间:2023-11-04 14:06:56 25 4
gpt4 key购买 nike

我有以下批量 Spring 配置。当特定用例引发不可跳过的异常时,我遇到了生产批处理的问题。当我在集成测试中重现用例时,批处理行为仍然很奇怪。 当我的batch.commit-interval属性大于1时,会触发重试机制,回滚未提交的 block 并重新处理之前的所有记录。当batch-commit-interval设置为1时,一旦抛出不可跳过的异常,批处理就会失败

我尝试了批量提交间隔和重试限制的各种值。导致该问题的特定异常是 NullPointerException。 我的目标是最好使用 NeverRetryPolicy 处理该异常,如果没有,则使用经典的异常处理。我不希望重试失败的项目处理,因此我什至使用了 NeverRetryPolicy。但似乎整个事情最终是由批量提交间隔驱动的(使用重试限制、可重试异常类和 NeverRetryPolicy,似乎没有效果)

我已经从 StackOverflow 阅读了其他类似的问题。感谢您的帮助。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:batch="http://www.springframework.org/schema/batch"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=" http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/batch
http://www.springframework.org/schema/batch/spring-batch.xsd">


<!--================ JOB & STEPS ================ -->
<batch:job id="BEL-batch-Retry-Tests">

<batch:step id="mainRetryTests" >
<batch:tasklet>
<batch:chunk reader="jpaReaderRetryTests" processor="bdToFlatFileProcessorRetryTests" writer="flatWriterRetryTests" commit-interval="${batch.commit-interval}" skip-limit="${batch.skip-limit}">

<batch:streams>
<batch:stream ref="flatWriterRetryTests" />
</batch:streams>
<batch:skippable-exception-classes>
<batch:include
class="org.springframework.batch.item.file.FlatFileParseException" />
<batch:include class="ma.awb.ebk.bel.batch.EntiteBatchInvalideException" />
</batch:skippable-exception-classes>
</batch:chunk>
<batch:listeners>

<batch:listener ref="stepListener"/>
<batch:listener ref="skipListener"/>
</batch:listeners>
</batch:tasklet>

<batch:listeners>
<batch:listener ref="stepListener"/>
<batch:listener ref="skipListener"/>
</batch:listeners>
</batch:step>

<batch:listeners>
<batch:listener ref="jobListener"/>
</batch:listeners>
</batch:job>
<!--================ READERS & WRITERS ================-->

<bean id="jpaReaderRetryTests" class="org.springframework.batch.item.database.JpaPagingItemReader">
<property name="entityManagerFactory" ref="entityManagerFactoryBel"/>
<property name="queryString" value="select c from CaracteristiquesCarte c"/>
<property name="pageSize" value="100"/>
</bean>

<bean id="flatWriterRetryTests" class="org.springframework.batch.item.file.FlatFileItemWriter" scope="step">
<property name="resource" value="file:${retry-tests.outputfile.basename}-#{jobParameters['runDate']}${retry-tests.outputfile.ext}" />
<property name="lineAggregator">
<bean
class="org.springframework.batch.item.file.transform.DelimitedLineAggregator">
<property name="delimiter" value="${batch.file.writer.delimiter}" />
<property name="fieldExtractor">
<bean
class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
<property name="names" value="codeProduit,nomCarte,codeAAfficher,rechargeable" />
</bean>
</property>
</bean>
</property>

</bean>



<bean id="bdToFlatFileProcessorRetryTests" class="ma.awb.ebk.bel.batch.retry.BDToFlatFileRetryProcessor" />

读者和作者都很经典。这是处理器的代码

package ma.awb.ebk.bel.batch.retry;

import ma.awb.ebk.bel.domain.CaracteristiquesCarte;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.item.ItemProcessor;



public class BDToFlatFileRetryProcessor implements ItemProcessor<CaracteristiquesCarte, CaracteristiquesCarte> {

private static int counter = 0;
private static final Logger LOGGER = LoggerFactory.getLogger (BDToFlatFileRetryProcessor.class);


@Override
public CaracteristiquesCarte process(CaracteristiquesCarte caracteristiques)
throws Exception {

String witness = null;
counter++;

System.out.println("DEBUGGING iteration N° " + counter+" "+caracteristiques.getCodeProduit());

// Tester le cas d'un renvoie de null
if (counter == 4) {
System.out.println("PETTAGE " + counter);
String test = witness.substring(2);

}

return caracteristiques;
}

}

最佳答案

我看到的问题之一是你得到了空指针,这永远不应该发生。如果修复它会发生什么?休息看起来像是批处理所需的行为。

关于java - Spring批量默认重试策略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28771962/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com