[play 2.2.1 - Java] Method returning Promise<Result> can not be used as a handler

1,150 views
Skip to first unread message

Dima Gutzeit

unread,
Mar 15, 2014, 12:33:12 AM3/15/14
to play-fr...@googlegroups.com
Dear members,

We implement a web application using Play framework and we would like to use async processing. We are using Play 2.2.1, programming using Java.

Documentation located here: http://www.playframework.com/documentation/2.2.x/JavaAsync clearly states that to enable async processing we should use a handler such as:

public static Promise<Result> index() {
  Promise<Integer> promiseOfInt = Promise.promise(
    new Function0<Integer>() {
      public Integer apply() {
        return intensiveComputation();
      }
    }
  );
  return promiseOfInt.map(
      new Function<Integer, Result>() {
        public Result apply(Integer i) {
          return ok("Got result: " + i);
        } 
      }
    );
}
instead of DEPRECATED async(Promise) method.

The problem is that while doing that, the application fails to compile claiming:

[error] web/conf/routes:7: Cannot use a method returning play.libs.F.Promise[_$1] as an Handler

[error] GET        /pay                     controllers.Application.verifyCheckoutRequest()

[error] web/conf/routes:7: not enough arguments for constructor HandlerRef: (implicit handlerInvoker: play.core.Router.HandlerInvoker[play.libs.F.Promise[_$1]])play.api.mvc.HandlerRef[play.libs.F.Promise[_$1]].

[error] Unspecified value parameter handlerInvoker.

[error] GET        /pay                     controllers.Application.verifyCheckoutRequest()

[error] web/conf/routes:7: Cannot use a method returning play.libs.F.Promise[_$1] as an Handler

[error] GET        /pay                     controllers.Application.verifyCheckoutRequest()

[error] three errors found

[error] (compile:compile) Compilation failed

[error] application - 


We are clearly missing something basic here.

Can anyone help to shed some light into that mystery ?

Thanks in advance for your help.

Rich Dougherty

unread,
Mar 15, 2014, 1:23:49 AM3/15/14
to play-fr...@googlegroups.com
Are you getting a compile error using that example code or is your code different?


--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framewor...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Rich Dougherty - @richdougherty
Typesafe - Reactive apps on the JVM

Dima Gutzeit

unread,
Mar 15, 2014, 3:07:59 AM3/15/14
to play-fr...@googlegroups.com
My code is different but the idea is the same, handler method returns Promise<Result>, instead of 'classic' Result
I think the compilation error is very clear in this aspect although I find it contradicting the example in documentation and deprecation of aync(Promise) method.

ssch...@jibe.com

unread,
Mar 15, 2014, 1:31:37 PM3/15/14
to play-fr...@googlegroups.com
That compilation error indicates that your handler method is declared to return something like Promise<? extends Result> rather than Promise<Result>..
The route compiler currently insists on only Promise<Result>.

Dima Gutzeit

unread,
Mar 15, 2014, 7:01:03 PM3/15/14
to play-fr...@googlegroups.com
True, but how else should I handle a method that can return both Result and Status such as badRequest using Promise.pure(badRequest()) ?

Regards,
Dima Gutzeit.

Sent from a mobile device.
You received this message because you are subscribed to a topic in the Google Groups "play-framework" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/play-framework/U1Xnaj6-hCQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to play-framewor...@googlegroups.com.

ssch...@jibe.com

unread,
Mar 15, 2014, 7:26:24 PM3/15/14
to play-fr...@googlegroups.com
Good question.
See https://github.com/playframework/playframework/issues/2493

The only (clugy) workaround I can think of is to map the Promise<Status> to a Promise<Result> with an explicit cast.
Reply all
Reply to author
Forward
0 new messages