Result and AsyncResult in Java

67 views
Skip to first unread message

ita...@code972.com

unread,
Oct 14, 2013, 12:02:25 PM10/14/13
to play-fr...@googlegroups.com
Hi,

I'm starting a PoC with Play 2.2.0 and having some pain points with the way sync/async Results are returned in Java.

Consider the following:

    public static F.Promise<Result> myAction(/* some input */) {
        // Request validation
        if (StringUtils.isEmpty(content.title)) {
            return badRequest("Title field cannot be empty");
        }
        if (StringUtils.isEmpty(content.content)) {
            return badRequest("Content field cannot be empty");
        }

        F.Promise<MyObject> rsp = F.Promise.promise(
                new F.Function0<MyObject>() {
                    public MyObject apply() {
                        return myMagicHappensHere(content, new MyStrategy());
                    }
                }
        );

        return rsp.map(
                new F.Function<MyObject,Result>() {
                    public Result apply(MyObject pr) {
                        return ok(index.render(pr));
                    }
                }
        );
    }

This won't compile, because badRequest() is a Result, and I need to return a Promise<Result>

Another issue is with the verbosity of the response building, just because I wanted to use the async capabilities. Since most controllers are going to be using this (or should), surely there's a better way to do that?

Is it possible perhaps to have the framework expect a return type which is an interface, having both F.Promise<Result> and Result implementing it, and then some helper functions to return every Result as a Promise?

Just throwing ideas in the air. Having looked at several other web-frameworks, I'll be happy to participate in any in-depth discussion on the matter.

Thanks

Itamar.

Johan Andren

unread,
Oct 14, 2013, 2:32:10 PM10/14/13
to play-fr...@googlegroups.com
That is how it has been up till 2.2, at least on the Scala side. So the change was very much intentional as I understand it, I think there is a bit of motivation in the 2.2 migration guide in the docs.

You can wrap the Result as a Promise<Result> by using Promise.pure(...yourresult...)

Reply all
Reply to author
Forward
0 new messages