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 SkippableExceptionDuringWriteSample {
039
040        private final JobBuilderFactory jobBuilderFactory;
041
042        private final StepBuilderFactory stepBuilderFactory;
043
044        public SkippableExceptionDuringWriteSample(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                                return item;
058                        }
059                };
060        }
061
062        @Bean
063        public ItemProcessor<Integer, Integer> itemProcessor() {
064                return item -> {
065                        System.out.println("processing item = " + item);
066                        return item;
067                };
068        }
069
070        @Bean
071        public ItemWriter<Integer> itemWriter() {
072                return items -> {
073                        System.out.println("About to write chunk: " + items);
074                        for (Integer item : items) {
075                                if (item.equals(5)) {
076                                        System.out.println("Throwing exception on item " + item);
077                                        throw new IllegalArgumentException("Sorry, no 5 here!");
078                                }
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}