简体   繁体   中英

accessing resources in next steps spring batch

Hello I have the follow Tasklet in my spring batch application:

public class FileMovingTasklet implements Tasklet, InitializingBean {


    @Value("${positionFile.source-path}")
    private String sourcePath;

    @Value("${positionFile.local-path}")
    private String localDirectory;

    @Value("${positionFile.patternName}")
    private String fileNamePattern;

    @Value("${positionFile.suffix}")
    private String suffix;

    @Override
    public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {

        //***code***

            List<PathResource> resources = FileManager.getInputFileList(sourcePath, fileNamePattern, suffix, fileDate);

            if (!CollectionUtils.isEmpty(resources)) {
                copyFiles(resources, localDirectory);
                log.info("Copied files to local directory...");

            }

        } catch (IOException e) {
            log.error("Position File not found with sourcePatch={}, fileName={}, suffix={}, filedate={}", sourcePath, fileNamePattern, suffix, fileDate);
            throw new TaskletException("Could not move file from source to local " + e);
        }

        return RepeatStatus.FINISHED;
    }

    private void copyFiles(List<PathResource> resources, String localDirectory) {

        for (Resource resource : resources) {

            File source;
            File target;

            try {
                source = resource.getFile();
                target = new File(localDirectory + File.separator + source.getName());

                try {
                    FileCopyUtils.copy(source, target);
                } catch (IOException e) {

                }
            } catch (IOException e) {
            }
        }

    }

}

I am moving some files from source to my local destination. The local destination is where I want to to read in files and process and write in my subsequent steps.

I have configured my Job as follows:

@Configuration
public class BatchConfiguration {

    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Autowired
    private FileMovingTasklet fileMovingTasklet;

    @Value("${positionFile.local-path}")
    private Resource[] resources;

    @Autowired
    private PriceDao scpDao;

    @Autowired
    public PositionRowReader positionRowReader;


    @Bean
    public MultiResourceItemReader<Pos> multiResourceItemReader() {
        MultiResourceItemReader<Pos> resourceItemReader = new MultiResourceItemReader<>();
        resourceItemReader.setResources(resources);
        resourceItemReader.setDelegate(posRowReader());
        return resourceItemReader;
    }

    @Bean
    public FlatFileItemReader<Pos> posRowReader() {
        return positionRowReader.getReader();
    }


    @Bean
    public ItemProcessor<Pos, Price> posRowProcessor() {
        return new PosRowItemProcessor();
    }


    @Bean
    public JobExecutionListener listener() {
        return new JobCompletionNotificationListener(scpDao);
    }


    @Bean
    public Job import() {
        return jobBuilderFactory.get("import")
                .incrementer(new RunIdIncrementer())
                .listener(listener())
                .start(getPositionFileStep())
                .next(step1())
                .build();
    }


    @Bean
    public Step getPositionFileStep() {
        return stepBuilderFactory.get("getPositionFileStep")
                .tasklet(fileMovingTasklet)
                .build();
    }

    @Bean
    public Step step1() {
        return stepBuilderFactory.get("step1")
                .<Pos, Price>chunk(50)
                .reader(multiResourceItemReader())
                .processor(posRowProcessor())
                .writer(new PriceWriter(scpDao))
                .build();
    }



}

I get failed to initialize the reader when executing step1 :

org.springframework.batch.item.ItemStreamException: Failed to initialize the reader

my local-path property is as follows:

positionFile.local-path=C:\\Dev\\workspace\\batch\\src\\main\\resources\\localPath

positionFile.patterName=PositionFile*

my question is how can I access the resources (files) which can be multiple files in step1 after files have been copied over from source folder.

My resources size is 0 even though files are there:

   @Value("${positionFile.local-path}")
    private String filePath;

    @Value("${positionFile.patternName}")
    private String filePattern;


    @Bean
    @StepScope
    public MultiResourceItemReader<PosRow> multiResourceItemReader() {
        MultiResourceItemReader<PosRow> resourceItemReader = new MultiResourceItemReader<>();
        Resource[] resources = new Resource[0];
        try {
            ClassLoader cl = this.getClass().getClassLoader();
            ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(cl);
            resources = resolver.getResources("file:" + filePath + File.separator + filePattern + "*");
        } catch (IOException e) {
            log.error("Problem with getting resource files");
        }
        resourceItemReader.setResources(resources);
        resourceItemReader.setDelegate(posRowReader());
        return resourceItemReader;
    }

You need to make your MultiResourceItemReader and FlatFileItemReader lazy by using @StepScope

    @Bean
    @StepScope
    public MultiResourceItemReader<Pos> multiResourceItemReader() {
            MultiResourceItemReader<Pos> resourceItemReader = new MultiResourceItemReader<>();
            resourceItemReader.setResources(resources);
            resourceItemReader.setDelegate(posRowReader());
            return resourceItemReader;
        }

        @Bean
        @StepScope
        public FlatFileItemReader<Pos> posRowReader() {
            return positionRowReader.getReader();
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM