私はPlayフレームワークWebアプリケーションを作成しましたが、次の問題があります。私はデータベースにアクセスするいくつかのDAOを持っており、データベースへの1つの要求は、別の要求からの情報に依存していることがあります。他のタスクに依存するCompletableFutureを実行
これは問題の1つです。私はgetLicenseByKey
メソッドを非同期で実行し、その結果を取得します。今度はversion_dao.getVersionUntilX()
をlicense_dao
リクエストの結果で実行できます。ここで問題となるのは、という機能がHttpExecutionContext
(Playフレームワーク)のHTTPスレッドの1つをブロックして実行され、このデータベース要求に時間がかかるとスレッドがブロックされるという問題です。
したがって、license_dao.getLicenseByKey()
を非同期で実行し、この方法でversion_dao.getVersionUntilX()
メソッドを非同期に実行するとどうなりますか?両方が終了した場合は、PlayのHttpExecutionContext
からHTTPの結果を返したいとします。
public CompletionStage<Result> showDownloadScreen(String license_key) {
return license_dao.getLicenseByKey(license_key).thenApplyAsync(license -> {
try {
if(license!=null) {
if(license.isRegistered()) {
return ok(views.html.download.render(license,
version_dao.getVersionUntilX(license.getUpdates_until())
.toCompletableFuture().get()));
}else {
return redirect(routes.LicenseActivationController.showActivationScreen(license_key));
}
}else {
return redirect(routes.IndexController.index("Insert Key can not be found!"));
}
} catch (InterruptedException | ExecutionException e) {
return redirect(routes.IndexController.index("Error while checking the Verison"));
}
}, httpExecutionContext.current());
}