尽管阻塞,CompletableFuture仍未解决

时间:2018-05-26 15:31:21

标签: java java-8 future executorservice completable-future

我有一个用例,其中,我需要根据从休息服务轮询状态返回可完成的未来: 我拥有的是:

Class Dummy implements Function<DummyObject, CompletionStage<MyMainStatus>> {
    private static final int POLL_INTERVAL = 15;
    private final ScheduledExecutorService service;
    private final StateFullComponent component;

    public Dummy(ScheduledExecutorService service, StateFullComponent component){
       this.service = requireNonNull(service);
       this.component = requireNonNull(component);
    }

    @Override
    public CompletionStage<MyMainStatus> apply(DummyObject dummy) {
        CompletableFuture<MyMainStatus> cf1 = new CompletableFuture<>();
        CompletableFuture<MyIntermediateStatus> cf2 = new CompletableFuture<>();

        final ScheduledFuture<?> checkFuture = service.scheduleAtFixedRate(() -> {
           StatusObject so = someRestServiceCall(dummy);
           if(so.status.ALL_OK){
              cf2.complete(MyIntermediateStatus.OK);
           } else if(so.status.SOME_FAIL){
              Set<RetryJobs> jobs = findRetryJobs(dummy);
              component.add(jobs);
              if(so.status.SOME_FAIL_SOME_PASS) {
             //this block basically monitors the retry jobs
              CompletionStage<MyIntermediateStage> cf3 = 
                       someMethodWhichReturnsStage(component.get(jobs)); 
              cf3.thenCompose(interStatus -> {
                 if(interStatus.equals(MyIntermediateStatus.OK))
                     cf2.complete(MyIntermediateStatus.OK);
                 else
                     cf2.complete(MyIntermediateStatus.FAIL);
                  return cf2;
                }
              }
              , 0, POLL_INTERVAL, TimeUnit.Seconds);

        return cf2.thenCompose((result) -> {
             checkFuture.cancel(true);
             if(result.equals(MyIntermediateStatus.OK))
                  cf1.complete(MyMainStatus.OK);
             else
                  cf1.complete(MyMainStatus.NOT_OK);
             return cf1;
   }          

好的,现在遇到这个问题,最初的民意调查发生的一切都很好,而且我也可以安排重试。当SOME_PASS_SOME_FAIL块发挥作用时出现问题,即使重试批次尚未完成,似乎未来CF2已完成,因为轮询停止并且基本上没有发生任何事情。 我意识到我没有对cf3做任何事情,这可能是原因吗? 有什么建议可以改善上述情况? 我正在做一个关于未来的.get()我从上面的类返回

0 个答案:

没有答案