在Play Framework中阻止IO调用的处理

问题描述 投票:2回答:1

请查看以下两个类,其中一个是建议的类,以及它们之间的不同..]]

// Approach 1
public class Test extends Controller {
    private final DatabaseExecutionContext executionContext;

    @Inject
    public Test(DatabaseExecutionContext executionContext) {
        this.executionContext = executionContext;
    }

    public CompletableFuture<Result> get() throws ExecutionException, InterruptedException {

        return io().thenApply(Results::ok);
    }

    public CompletableFuture<String> io() throws ExecutionException, InterruptedException {
        return CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "io";
        }, executionContext);

    }
}
//Approach 2
public class Test extends Controller {
    private final DatabaseExecutionContext executionContext;

    @Inject
    public Test(DatabaseExecutionContext executionContext) {
        this.executionContext = executionContext;
    }

    public Result get() throws ExecutionException, InterruptedException {

        return Results.ok(io());
    }

    public String io() throws ExecutionException, InterruptedException {
        return CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "io";
        }, executionContext).join();

    }
}

我想了解如何正确处理阻塞的IO调用。有人可以解释一下Play框架如何使用Completable Future,以我的观点,它仍然处于阻塞状态,没有任何意义,并使我的代码处理的CompletableFuture本身就很可怕。

请查看以下两个类,其中一个是建议的类,以及它们之间的区别..? //方法1公共类Test扩展了Controller {私有最终DatabaseExecutionContext ...

java playframework reactive-programming blocking
1个回答
1
投票

Play is new to me, I want to understand how to properly handle blocking IO calls.

© www.soinside.com 2019 - 2024. All rights reserved.