001/*
002 * Copyright 2019 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *       http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.batch.sample.skip;
018
019import java.util.Arrays;
020
021import org.springframework.batch.core.Job;
022import org.springframework.batch.core.Step;
023import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
024import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
025import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
026import org.springframework.batch.item.ItemProcessor;
027import org.springframework.batch.item.ItemReader;
028import org.springframework.batch.item.ItemWriter;
029import org.springframework.batch.item.support.ListItemReader;
030import org.springframework.context.annotation.Bean;
031import org.springframework.context.annotation.Configuration;
032
033/**
034 * @author Mahmoud Ben Hassine
035 */
036@Configuration
037@EnableBatchProcessing
038public class SkippableExceptionDuringReadSample {
039
040        private final JobBuilderFactory jobBuilderFactory;
041
042        private final StepBuilderFactory stepBuilderFactory;
043
044        public SkippableExceptionDuringReadSample(JobBuilderFactory jobBuilderFactory,
045                                                                                          StepBuilderFactory stepBuilderFactory) {
046                this.jobBuilderFactory = jobBuilderFactory;
047                this.stepBuilderFactory = stepBuilderFactory;
048        }
049
050        @Bean
051        public ItemReader<Integer> itemReader() {
052                return new ListItemReader<Integer>(Arrays.asList(1, 2, 3, 4, 5, 6)) {
053                        @Override
054                        public Integer read() {
055                                Integer item = super.read();
056                                System.out.println("reading item = " + item);
057                                if (item != null && item.equals(5)) {
058                                        System.out.println("Throwing exception on item " + item);
059                                        throw new IllegalArgumentException("Sorry, no 5 here!");
060                                }
061                                return item;
062                        }
063                };
064        }
065
066        @Bean
067        public ItemProcessor<Integer, Integer> itemProcessor() {
068                return item -> {
069                        System.out.println("processing item = " + item);
070                        return item;
071                };
072        }
073
074        @Bean
075        public ItemWriter<Integer> itemWriter() {
076                return items -> {
077                        System.out.println("About to write chunk: " + items);
078                        for (Integer item : items) {
079                                System.out.println("writing item = " + item);
080                        }
081                };
082        }
083
084        @Bean
085        public Step step() {
086                return this.stepBuilderFactory.get("step")
087                                .<Integer, Integer>chunk(3)
088                                .reader(itemReader())
089                                .processor(itemProcessor())
090                                .writer(itemWriter())
091                                .faultTolerant()
092                                .skip(IllegalArgumentException.class)
093                                .skipLimit(3)
094                                .build();
095        }
096
097        @Bean
098        public Job job() {
099                return this.jobBuilderFactory.get("job")
100                                .start(step())
101                                .build();
102        }
103
104}