I am trying to use parallel streams in my Spring application but am getting a "No thread-bound request found" exception.
My code looks similar to the following:
@Controller
@RequiredArgsConstructor(onConstructor = @__(@Inject)) // yes, I'm using lombok
public class controllerClass {
private final someOtherComponent;
@RequestMapping(value = "/test", method = RequestMethod.Get)
public Map<String, String> doParallelStream() {
List<String> testStrings = Arrays.asList("one", "two", "three");
return testStrings.parallelStream()
.map(testStrings -> someOtherComponent.someCall(testStrings))
.collect(Collectors.toConcurrentMap(
returnedString, returnedString, (p1, p2) -> p1
));
}
}
My guess is that because I'm using someOtherComponent within the map inside the parallelstream that the threads spun up no longer have the context to access it.
The full error I'm getting is:
Error executing a controller { java.lang.IllegalStateException: java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
Any suggestions on how I can get around this?
someOtherComponent.someCallmight use request attribute auto wiring - you'll need to wire these manually. - Boris the Spider@Injector@Autowiredhas been required since Spring 4.1 or 4.2 which could clean up your lombok usage. Your assumption is correct due to the usage of parallelStreams. - Darren Forsythe