【问题标题】:toCompletableFuture() stucks for asynchronous cacheCompletableFuture() 坚持异步缓存
【发布时间】:2018-07-19 14:10:13
【问题描述】:

您好,我正在尝试强制终止承诺以从中获取结果,但它只是卡在加载中。

public class CacheController extends Controller {
    private AsyncCacheApi cache;
    public Result cache()
    {
        String test = "nice";
        cache.set("item.key", test, 15);

        Customer user = new Customer("Ana", 12);
        CompletionStage<Done> result = cache.set(user.getName(), user);
        block(result);

        return ok("Cached");
    }
    public Result checkCache() throws Exception
    {
        Logger.info("start");
        //CompletionStage<String> news = cache.get("item.key");
        //news.thenRun(() -> System.out.println("works"));

        CompletionStage<Customer> result = cache.get("Ana");

        Logger.info("step 1");

        Logger.info(cache.get("Ana").toString());
        Logger.info("Step 2");

        Customer c = block(result);

        Logger.info("Step 3 " + c.getName());

        //result.thenRun(() -> setUser(result)).thenRun(() -> Logger.info(user.getName() + " " + user.getAge()));


        return ok("cancan");
    }

    private <T> T block(CompletionStage<T> stage) {
        try {
            return stage.toCompletableFuture().get();
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
    }
}

根据我的猜测,当尝试加载页面时,它在第 2 步之后卡在line 56: Customer c = block(result); 有什么解决办法吗?

【问题讨论】:

    标签: asynchronous caching playframework-2.6


    【解决方案1】:

    @Codrin

    我遇到了同样的问题。但是,请参阅https://www.playframework.com/documentation/2.6.x/JavaCache#Setting-the-execution-context

    默认情况下,所有 Ehcache 操作都是阻塞的,异步实现会阻塞默认执行上下文中的线程。

    也许 CompletableFuture.get() 卡住了,因为它与调用者在同一个线程中执行。

    参考链接页面,我在 application.conf 中添加了下面的 sn-p 并且它起作用了。

    play.cache.dispatcher = "contexts.blockingCacheDispatcher"
    
    contexts {
    blockingCacheDispatcher {
        fork-join-executor {
        parallelism-factor = 3.0
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-03-17
      • 1970-01-01
      • 2017-12-31
      • 2019-04-11
      • 2011-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-09
      相关资源
      最近更新 更多