[play-framework][2.0][Java] Http context in Async Result

963 views
Skip to first unread message

Stikkos

unread,
May 15, 2012, 6:14:16 AM5/15/12
to play-fr...@googlegroups.com
Hi,

I am using AsyncResults in my controllers as defined http://www.playframework.org/documentation/2.0.1/JavaAsync

public static Result index() {
  Promise<Integer> promiseOfInt = Akka.future(
    new Callable<Integer>() {
      public Integer call() {
        return intensiveComputation();
      }
    }
  );
  return async(
    promiseOfInt.map(
      new Function<Integer,Result>() {
        public Result apply(Integer i) {
	  flash().put("success", "The operation was successful");     // problem line
          return ok("Got result: " + i);
        } 
      }
    )
  );
}
The problem is that there is not HTTP context in the async result, which causes the following error

java.lang.RuntimeException: There is no HTTP Context available from here.
at play.mvc.Http$Context.current(Http.java:27) ~[play_2.9.1.jar:2.0.1]
at play.mvc.Controller.flash(Controller.java:70) ~[play_2.9.1.jar:2.0.1]

Is there any way to modify the HTTP context in an async result? Or what is the recommended approach to handle stuff like this?

Thanks,
Stik

Julien Richard-Foy

unread,
May 15, 2012, 6:46:54 AM5/15/12
to play-fr...@googlegroups.com
Are you using master?

Stikkos

unread,
May 15, 2012, 6:48:30 AM5/15/12
to play-fr...@googlegroups.com
Euhm, i guess…. I'm using play-2.0.1


On 15 May 2012, at 11:46, Julien Richard-Foy wrote:

> Are you using master?
>
> --
> You received this message because you are subscribed to the Google Groups "play-framework" group.
> To post to this group, send email to play-fr...@googlegroups.com.
> To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.
>

Julien Richard-Foy

unread,
May 15, 2012, 6:50:56 AM5/15/12
to play-fr...@googlegroups.com
This should has been fixed in master. Can you try with it?

Kevin Bosman

unread,
May 15, 2012, 7:23:31 AM5/15/12
to play-fr...@googlegroups.com

> Euhm, i guess…. I'm using play-2.0.1

https://github.com/playframework/Play20/wiki/BuildingFromSource

Stikkos

unread,
May 15, 2012, 7:52:55 AM5/15/12
to play-fr...@googlegroups.com
Cool, i just tested it in master and indeed it has been fixed!

Love it ;-)



On 15 May 2012, at 11:50, Julien Richard-Foy wrote:

> This should has been fixed in master. Can you try with it?
>

Stijn Janssens

unread,
Jul 3, 2012, 7:36:32 AM7/3/12
to play-fr...@googlegroups.com
Hi I just upgraded to play 2.0.2 and the problem re-appeared

Caused by: java.lang.RuntimeException: There is no HTTP Context available from here.
at play.mvc.Http$Context.current(Http.java:27) ~[play_2.9.1.jar:2.0.2]
at play.mvc.Controller.flash(Controller.java:70) ~[play_2.9.1.jar:2.0.2]

Any ideas?

Thanks!

Stikkos
15 May
to play-framework

Guillaume Bort

unread,
Jul 3, 2012, 7:58:01 AM7/3/12
to play-fr...@googlegroups.com
The 2.0.x branch doesn't contain this fix, because it is backward incompatible.
--
Guillaume Bort, http://guillaume.bort.fr

Ondřej Kroupa

unread,
Oct 31, 2012, 5:40:02 PM10/31/12
to play-fr...@googlegroups.com
Can you publish any temporary solution for this isssue in 2.0.x projects? We are implementing REST and this feature is absolutely needed. Now we are running on 2.1, but we would like to come back to stable release to be able to use async call in requests modify response headers.

Dne úterý, 3. července 2012 13:58:01 UTC+2 Guillaume Bort napsal(a):

Nilanjan

unread,
Nov 1, 2012, 12:41:37 AM11/1/12
to play-fr...@googlegroups.com
I guess the alternative would be to capture the contents of the Context.current.get and set it for the thread executing the async block.

HTH

Nilanjan

Ondřej Kroupa

unread,
Nov 4, 2012, 5:43:02 AM11/4/12
to play-fr...@googlegroups.com
I have tried it right now. Reading request headers seems working fine after re-setting Http.Context.current. But when I set http header to response, it is not returned in fact.

S pozdravem
Ondřej Kroupa


To view this discussion on the web visit https://groups.google.com/d/msg/play-framework/-/7r5JGfzrmOsJ.

Aurélien

unread,
Nov 10, 2012, 1:05:18 PM11/10/12
to play-fr...@googlegroups.com, ma...@seal.io
Hello,

I am having the same problem in play java 2.0.4.
Will it solve the problem if I use the 2.1 dev version of play ?
If so, what kind of regression/change of behavior may I experienced ?

Thank you for your response,

Aurélien

On Friday, November 9, 2012 7:27:45 PM UTC+1, Jirka Příhoda wrote:
Currently trying to figure out same problem. Anyone there with a solution?

Dne neděle, 4. listopadu 2012 11:43:36 UTC+1 Ondřej Kroupa napsal(a):

James Roper

unread,
Nov 12, 2012, 1:06:32 AM11/12/12
to play-fr...@googlegroups.com, ma...@seal.io
2.1 definitely does fix this issue.  We are currently preparing the release notes, which will include an upgrade guide.

Ondřej Kroupa

unread,
Nov 12, 2012, 11:10:07 AM11/12/12
to play-fr...@googlegroups.com, ma...@seal.io
Yes, it will solve it, but most of Play 2 modules like play-authenticate are not compatible with current 2.1 branch, so consider it well.

Dne pondělí, 12. listopadu 2012 7:06:32 UTC+1 James Roper napsal(a):
Reply all
Reply to author
Forward
0 new messages