Scientist4J icon indicating copy to clipboard operation
Scientist4J copied to clipboard

Async support needs to support getting request context

Open ctoestreich opened this issue 8 years ago • 2 comments

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.

ctoestreich avatar May 24 '16 00:05 ctoestreich

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.

ctoestreich avatar May 24 '16 01:05 ctoestreich

Perhaps add a note around this in the README?

rawls238 avatar May 24 '16 07:05 rawls238