foomango / play-java-rest-api-example

REST API using Play in Java

Home Page:https://playframework.com/download#examples

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

play-java-rest-api-example

A REST API showing Play with a JPA backend. For the Scala version, please see https://github.com/playframework/play-scala-rest-api-example.

Best Practices for Blocking API

If you look at the controller: https://github.com/playframework/play-java-rest-api-example/blob/2.6.x/app/v1/post/PostController.java then you can see that when calling out to a blocking API like JDBC, you should put it behind an asynchronous boundary -- in practice, this means using the CompletionStage API to make sure that you're not blocking the rendering thread while the database call is going on in the background.

public CompletionStage<Result> list() {
    return handler.find().thenApplyAsync(posts -> {
        final List<PostResource> postList = posts.collect(Collectors.toList());
        return ok(Json.toJson(postList));
    }, ec.current());
}

There is more detail in https://www.playframework.com/documentation/latest/ThreadPools -- notably, you can always bump up the number of threads in the rendering thread pool rather than do this -- but it gives you an idea of best practices.

About

REST API using Play in Java

https://playframework.com/download#examples

License:Other


Languages

Language:Java 76.7%Language:Scala 14.1%Language:Shell 5.3%Language:HTML 4.0%