2

我正在使用 multiResourceItemReader 读取 csv 文件,并且我将跳过限制保持为 10。当超过限制时,我想捕获SkipLimitExceedException并抛出我自己的自定义异常,并显示类似“Invalid csv”的消息,在哪里或如何我抓到了吗?

try {
      log.info("Running job to insert batch fcm: {} into database.", id);
            jobLauncher
                    .run(importJob, new JobParametersBuilder()
                    .addString("fullPathFileName", TMP_DIR)
                    .addString("batch_fcm_id", String.valueOf(id))
                    .addLong("time",System.currentTimeMillis())
                    .toJobParameters());
        }
catch(...){...}

我在这里抓不到它,是不是因为我正在使用MultiResourceItemReader并且异步过程不允许我在这里抓到它?

我的工作如下

@Bean(name = "fcmJob")
    Job importJob(@Qualifier(MR_ITEM_READER) Reader reader,
                  @Qualifier(JDBC_WRITER) JdbcBatchItemWriter jdbcBatchItemWriter,
                  @Qualifier("fcmTaskExecutor") TaskExecutor taskExecutor) {
        Step writeToDatabase = stepBuilderFactory.get("file-database")//name of step
                .<FcmIdResource, FcmIdResource>chunk(csvChunkSize) // <input as, output as>
                .reader(reader)
                .faultTolerant()
                .skipLimit(10)
                .skip(UncategorizedSQLException.class)
                .noSkip(FileNotFoundException.class)
                .writer(jdbcBatchItemWriter)
                .taskExecutor(taskExecutor)
                .throttleLimit(20)
                .build();

        return jobBuilderFactory.get("jobBuilderFactory") //Name of job builder factory
                .incrementer(new RunIdIncrementer())
                .start(writeToDatabase)
                .on("*")
                .to(deleteTemporaryFiles())
                .end()
                .build();
    }

我尝试过使用 ItemReaderListener、SkipPolicy、SkipListener,但它们不能抛出异常,还有其他方法吗?

4

1 回答 1

2

您正在寻找的异常不是由作业引发的,您可以使用JobExecution#getAllFailureExceptions.

所以在你的例子中,而不是这样做:

try {
    jobLauncher.run(job, new JobParameters());
} catch (Exception e) {
   //...
}

你应该做:

JobExecution jobExecution = jobLauncher.run(job, new JobParameters());
List<Throwable> allFailureExceptions = jobExecution.getFailureExceptions();

在您的情况下,SkipLimitExceedException将是其中之一allFailureExceptions

编辑:添加一个示例,显示它SkipLimitExceedException是以下内容的一部分allFailureExceptions

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.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 ItemProcessor<Integer, Integer> itemProcessor() {
        return item -> {
            if (item % 3 == 0) {
                throw new IllegalArgumentException("no multiples of three here! " + item);
            }
            return item;
        };
    }

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

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

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

    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());
        List<Throwable> allFailureExceptions = jobExecution.getAllFailureExceptions();
        for (Throwable failureException : allFailureExceptions) {
            System.out.println("failureException = " + failureException);
        }
    }

}

此示例打印:

item = 1
item = 2
item = 4
item = 5
item = 7
item = 8
failureException = org.springframework.batch.core.step.skip.SkipLimitExceededException: Skip limit of '2' exceeded
于 2019-04-04T07:32:23.060 回答