Here is the bean
@Service
@RequiredArgsConstructor
public class BeanWithResourceLoader{
private final ResourceLoader resourceLoader;
@SneakyThrows
private String readResource() {
return IOUtils.toString(
resourceLoader.getResource(
"classpath:/some_resource.json").getInputStream(),
StandardCharsets.UTF_8.name());
}
}
Another bean calls it:
var result =
customThreadPool.submit(
() -> listOfThings.parallelStream().map(aThing ->beanWithResourceLoader.readResource())
.collect(Collectors.toList()))
.get();
customThreadPool.submit
or just listOfThings.parallelStream()
causes java.io.FileNotFoundException at readResource
It can't find file!
Caused by: java.io.FileNotFoundException: class path resource [some_resource.json] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:180)
but stream
(no concurrency)
listOfThings.stream().map(aThing ->beanWithResourceLoader.readResource())
.collect(Collectors.toList())
works...
Un-reproducible in unit-tests, reproducible when application is started as jar.
How can I overcome it?