Scientist4J
Scientist4J copied to clipboard
Async support needs to support getting request context
Something I bumped into with the runAsync is when wrapping code that looks at the request for params, cookies, etc Spring barfs out a message for "No thread-bound request found"
This is the error here in spring.
This is the workaround using grails. Should be similar in Spring Boot app.
class ManagedContentService implements WebAttributes {
//more code here
protected Map<String, String> runManagedContentExperiment(boolean forceCache, String sessionId, List<String> contentNameList, Map<String, String> mcTokens, ZonedDateTime date) {
Experiment<Map<String, String>> managedContentExperiment = new Experiment("managedContent");
RequestAttributes requestAttributes = getWebRequest() as RequestAttributes
Supplier<Map<String, String>> oldMC = new Supplier<Map<String, String>>() {
@Override
Map get() {
RequestContextHolder.setRequestAttributes(requestAttributes)
return getManagedContentRemote(forceCache, sessionId, contentNameList, mcTokens, date)
}
}
Supplier<Map<String, String>> newMC = new Supplier<Map<String, String>>() {
@Override
Map get() {
RequestContextHolder.setRequestAttributes(requestAttributes)
return getManagedContentRemoteRedux(sessionId, contentNameList, mcTokens, date)
}
}
return managedContentExperiment.runAsync(oldMC, newMC)
}
}
The line binding the request to the holder RequestContextHolder.setRequestAttributes(requestAttributes)
is the secret sauce here.
Perhaps add a note around this in the README?