spring-boot - SkipListener 中未调用 @OnSkipInWrite

标签 spring-boot spring-data-jpa spring-batch

我正在读取csv文件并使用spring批处理将数据插入数据库(读取、处理和写入)。我在itemWriter类中使用“jpaRepository.save”将数据保存到数据库中。我试图在 @OnSkipInWrite 方法中捕获跳过的项目和跳过的消息,但即使跳过数据,也不会调用此方法。在batch_step_execution表中: read_count = 18,write_count = 10,write_skip_count = 0,roll_back_count = 8。 为什么 write_skip_count 为 0?我只想知道跳过了哪一项以及异常消息是什么。我的步骤:

@Bean
public Step step() throws IOException {
    return stepBuilderFactory.get("step").<Entity, Entity>chunk(1).reader(multiResourceItemReader())
            .processor(processor()).writer(writer()).faultTolerant().skip(Exception.class).skipLimit(100)
            .listener(new stepExecutionListener()).build();
}

这是我的监听器类。

     public class StepExecutionListener{

private static final Logger LOG = Logger.getLogger(StepExecutionListener.class);

@OnSkipInRead
public void onSkipInRead(Throwable t) {
    LOG.error("On Skip in Read Error : " + t.getMessage());
}

@OnSkipInWrite
public void onSkipInWrite(Entity item, Throwable t) {
    LOG.error("Skipped in write due to : " + t.getMessage());
}

@OnSkipInProcess
public void onSkipInProcess(Entity item, Throwable t) {
    LOG.error("Skipped in process due to: " + t.getMessage());
}

@OnWriteError
public void onWriteError(Exception exception, List<? extends Entity> items) {
    LOG.error("Error on write on " + items + " : " + exception.getMessage());
}}

为什么@OnSkipInWrite和@OnWriteError没有被调用?任何帮助将非常感激。提前致谢。

最佳答案

从您分享的内容中我看不出为什么不调用跳过监听器,但这里是一个使用您的监听器的独立示例:

import java.util.Arrays;
import java.util.List;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.annotation.OnSkipInProcess;
import org.springframework.batch.core.annotation.OnSkipInRead;
import org.springframework.batch.core.annotation.OnSkipInWrite;
import org.springframework.batch.core.annotation.OnWriteError;
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.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) {
                if (item.equals(3)) {
                    throw new Exception("No 3 here!");
                }
                System.out.println("item = " + item);
            }
        };
    }

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

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

    public class StepExecutionListener {

        @OnSkipInRead
        public void onSkipInRead(Throwable t) {
            System.err.println("On Skip in Read Error : " + t.getMessage());
        }

        @OnSkipInWrite
        public void onSkipInWrite(Integer item, Throwable t) {
            System.err.println("Skipped in write due to : " + t.getMessage());
        }

        @OnSkipInProcess
        public void onSkipInProcess(Integer item, Throwable t) {
            System.err.println("Skipped in process due to: " + t.getMessage());
        }

        @OnWriteError
        public void onWriteError(Exception exception, List<? extends Integer> items) {
            System.err.println("Error on write on " + items + " : " + exception.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);
        JobExecution jobExecution = jobLauncher.run(job, new JobParameters());
        StepExecution stepExecution = jobExecution.getStepExecutions().iterator().next();
        System.out.println("WriteSkipCount = " + stepExecution.getWriteSkipCount());
    }

}

此示例打印:

item = 1
item = 2
Error on write on [1, 2, 3, 4, 5] : No 3 here!
item = 1
item = 2
Error on write on [3] : No 3 here!
item = 4
Skipped in write due to : No 3 here!
item = 5
item = 6
item = 7
item = 8
item = 9
item = 10
WriteSkipCount = 1

这意味着当写入时跳过某个项目并且 writeSkipCount 正确时,将调用跳过监听器。

希望这有帮助。

关于spring-boot - SkipListener 中未调用 @OnSkipInWrite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54644625/

相关文章:

java - BeanCreationException,Spring 启动

java - 如何通过JPA获取保存对象的id?

java - Hibernate 和 ZonedDateTime

Spring批处理还是Spring boot异步方法执行?

java - @RestControllerAdvice 在 Spring Boot 响应式 Java 应用程序中不起作用

spring-boot - Spring Boot 3升级后找不到changelog目录

java - 在 spring boot 中通过动态键读取属性

java - 使用 JPA Criteria API 返回 Collection 属性包含特定值的所有对象

spring-batch - Spring Batch 应用程序与 Spring Batch Admin 集成

java - spring batch 异常无法构造 java.util.Map$Entry