79576810

Date: 2025-04-16 09:27:02
Score: 1
Natty:
Report link

I ran into the same exact issue when doing the following

    final Runnable callback = // Some method which provides a callback that sends a mail

    final CompletableFuture<Void> notifyUsersFuture = CompletableFuture.runAsync(callback).exceptionally(throwable -> {
        LOGGER.error(throwable.getMessage());
        return null;
    });

Issue occurs due to the way the thread is created.

The application context might not be loaded in the threads created by parallel stream.

In order for the application context to be loaded correctly, I used the spring @Async annotation instead to run the process asynchronously and the issue is resolved.

For your case you can simply do

reports.stream().forEach(it -> {
  creationProcess.startCreationProcess(it);
})

@Async
void startCreationProcess(final Report report) {
    // Your logic
}
Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • User mentioned (1): @Async
  • Low reputation (1):
Posted by: Mariol Ballaj