gpt4 book ai didi

java - Spring 批处理 : How to catch exception message with skip method in spring batch?

转载 作者:行者123 更新时间:2023-11-30 06:02:38 27 4
gpt4 key购买 nike

我是Spring Batch的新手。我的问题是如何使用 spring-batch 中的skip方法捕获异常?据我所知,当Spring Batch中发生一些异常时,我们可以使用skip方法来跳过它们。但是如何使用skip方法获取异常消息呢?有人建议我使用 SkipListener ,这个类有 onSkipInProcess() 等三个回调方法,但对我来说没有用。并且 ItemProcessListener 也不起作用。

代码如下:(我使用skip方法来忽略异常,并使用两个监听器来接收异常信息)

Step mainStep = stepBuilder.get("run")
.<ItemProcessing, ItemProcessing>chunk(5)
.faultTolerant()
.skip(IOException.class).skip(SocketTimeoutException.class)//skip IOException here
.skipLimit(2000)
.reader(reader)
.processor(processor)
.writer(writer)
.listener(stepExecListener)
.listener(new ItemProcessorListener()) //add process listener
.listener(new SkipExceptionListener()) //add skip exception listner
.build();

ItemProcessorListener 如下所示:

//(this class implements ItemProcessListener )
{
@Override
public void beforeProcess(Object item) {
// TODO Auto-generated method stub

}
@Override
public void afterProcess(Object item, Object result) {
logger.info("invoke remote finished, item={},result={}",item,result);

}
@Override
public void onProcessError(Object item, Exception e) {
logger.error("invoke remote error, item={},exception={},{}",item,e.getMessage(),e);
}
}

SkipExceptionListener 如下所示:

//(implements SkipListener<Object, Object>)
{

@Override
public void onSkipInRead(Throwable t) {
// TODO Auto-generated method stub
}

@Override
public void onSkipInWrite(Object item, Throwable t) {
// TODO Auto-generated method stub
}

@Override
public void onSkipInProcess(Object item, Throwable t) {
logger.info("invoke remote finished,item={},itemJsonStr={},errMsg={},e={}",
item,
JSONObject.toJSONString(item),
t.getMessage(),
t);
}
}

问题是所有记录器都不起作用。实际上skip方法确实工作得很好,我可以在表batch_step_execution中获取skip计数。我不确定这两个监听器是否被回调。谁能告诉我我该怎么办?或者还有其他什么吗?非常感谢。

最佳答案

How to catch exception message with skip method in spring batch?

您可以通过实现 SkipListener 接口(interface)或扩展 SkipListenerSupport 类来实现这一点。 SkipListener 接口(interface)中的所有方法都有一个 Throwable 参数,该参数是引发的异常并导致项目被跳过。您可以在此处获取异常消息。这是一个例子:

import java.util.Arrays;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.SkipListener;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.support.ListItemReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableBatchProcessing
public class MyJob {

@Autowired
private JobBuilderFactory jobs;

@Autowired
private StepBuilderFactory steps;

@Bean
public ItemReader<Integer> itemReader() {
return new ListItemReader<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
}

@Bean
public ItemWriter<Integer> itemWriter() {
return items -> {
for (Integer item : items) {
System.out.println("item = " + item);
}
};
}

@Bean
public ItemProcessor<Integer, Integer> itemProcessor() {
return item -> {
if (item.equals(7)) {
throw new IllegalArgumentException("Sevens are not accepted!!");
}
return item;
};
}

@Bean
public Step step() {
return steps.get("step")
.<Integer, Integer>chunk(5)
.reader(itemReader())
.processor(itemProcessor())
.writer(itemWriter())
.faultTolerant()
.skip(IllegalArgumentException.class)
.skipLimit(3)
.listener(new MySkipListener())
.build();
}

@Bean
public Job job() {
return jobs.get("job")
.start(step())
.build();
}

public static class MySkipListener implements SkipListener<Integer, Integer> {

@Override
public void onSkipInRead(Throwable t) {

}

@Override
public void onSkipInWrite(Integer item, Throwable t) {

}

@Override
public void onSkipInProcess(Integer item, Throwable t) {
System.out.println("Item " + item + " was skipped due to: " + t.getMessage());
}
}

public static void main(String[] args) throws Exception {
ApplicationContext context = new AnnotationConfigApplicationContext(MyJob.class);
JobLauncher jobLauncher = context.getBean(JobLauncher.class);
Job job = context.getBean(Job.class);
jobLauncher.run(job, new JobParameters());
}

}

在此示例中,MySkipListener 实现 SkipListener 并按照您尝试执行的操作从异常中获取消息。该示例读取从 1 到 10 的数字并跳过数字 7。您可以运行 main 方法并应看到以下输出:

item = 1
item = 2
item = 3
item = 4
item = 5
item = 6
item = 8
item = 9
item = 10
Item 7 was skipped due to: Sevens are not accepted!!

我希望这会有所帮助。

关于java - Spring 批处理 : How to catch exception message with skip method in spring batch?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51981640/

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