Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async support needs to support getting request context #12

Open
ctoestreich opened this issue May 24, 2016 · 2 comments
Open

Async support needs to support getting request context #12

ctoestreich opened this issue May 24, 2016 · 2 comments

Comments

@ctoestreich
Copy link
Contributor

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
Copy link
Contributor Author

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.

@rawls238
Copy link
Owner

Perhaps add a note around this in the README?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants