How to use Http.Context.current() in a Promise in Play?

前端 未结 1 1883
耶瑟儿~
耶瑟儿~ 2020-12-19 14:17

In Play Framework 2.2.2, I\'d like to return a Promise. However I\'m calling a function which needs access to the variables stored in Http.Context.current() (th

相关标签:
1条回答
  • 2020-12-19 14:35

    Yes, HttpExecutionContext is what you need.

    When an HttpExecutionContext is created it gets the current thread's Http.Context and stores it. Then, when the HttpExecutionContext is later used to execute code it restores the Http.Context.

    All Promise methods use an HttpExecutionContext wrapped around the default ExecutionContext so they should propagate the Http.Context correctly across threads.

    Your example code above should work fine, for example. However you do need to make sure that when you call getAvailableServices, that the Http.Context is available in the thread you're calling from. If the Http.Context isn't available when you call the method, then the HttpExecutionContext will be unable to capture the Http.Context from that thread and propagate it when the promise's Function0 is applied.

    0 讨论(0)
提交回复
热议问题