0
   @Bean
    @StepScope
    public MultiResourceItemReader<PosRow> multiResourceItemReader() {
        MultiResourceItemReader<PosRow> resourceItemReader = new MultiResourceItemReader<>();
        Resource[] resources = new Resource[0];
        String path = "file:" + filePath + File.separator + filePattern + "*";
        log.info("Looking for resource files matching {}", path);
        try {
            PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
            resources = resolver.getResources(path);
        } catch (IOException e) {
            log.error("Problem with getting resource files ", e);
        }
        resourceItemReader.setResources(resources);
        resourceItemReader.setDelegate(posRowReader());
        return resourceItemReader;
    }

I am unable to get resources even though there are files in the location. In previous steps, the files get copied over and then I try to look for the files using PathMatchingResourcePatternResolver. I get the following printed on my console:

 c.s.p.p.batch.config.BatchConfiguration  : Looking for resource files matching file:C:\Dev\workspace\batch\src\main\resources\localPath\PositionFile*
o.s.b.item.file.MultiResourceItemReader  : No resources to read. Set strict=true if this should be an error condition.

I can see the locationPattern is correctly constructed.

The filePath and filePattern look like this in application.properties file:

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

positionFile.patternName=PositionFile
M06H
  • 1,675
  • 3
  • 36
  • 76

2 Answers2

0

You are not setting ClassLoader in PathMatchingResourcePatternResolver

ClassLoader cl = this.getClass().getClassLoader();
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(cl);
Niraj Sonawane
  • 10,225
  • 10
  • 75
  • 104
  • As per Spring docs when using a default c'tor: ClassLoader access will happen via the thread context class loader – Simon Jan 09 '19 at 07:18
0

I encountered the same issues and after some digging it seems like PathMatchingResourcePatternResolver sometimes has issues resolving with back slashes, sometimes works fine

For more reliability, I solved the issue replacing all back slashes with forward slashes

try this:

PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
path = path.replace("\\", "/");
resources = resolver.getResources(path);

NB: Issue also occurs with File.separator on Windows platform

ACH
  • 185
  • 11