InterruptedException Issue on Sonar while getting data from CompletableFuture

327 views Asked by At

Here inside the for loop I'm trying to get the value from stringCompletableFutureEntry which throws the InterruptedException and with sonar enabled on this, I am supposed to interrupt the thread when an exception occurs but according to my expectation I should get data for all futures if it's not there than I can use Optional.empty() or skip this iteration and move further. How should I get rid of this sonar issue?

private void mapFutures(
      Map<Obj1, CompletableFuture<DummyContainer>> futures, List<DummyStatus> dummyStatusList) {
    for (Map.Entry<Obj1, CompletableFuture<DummyContainer>> stringCompletableFutureEntry :
        futures.entrySet()) {
      try {
        Optional<DummyContainer> dummyContainer =
            Optional.ofNullable(**stringCompletableFutureEntry.getValue().get()**);
        List<Dummy> dummy = null;
        dummy = dummyContainer.map(DummyContainer::getDummyData).orElseGet(() -> null);
        
        if (!CollectionUtils.isEmpty(dummy)) {
          dummyStatusList.add(new DummyStatus(stringCompletableFutureEntry.getKey(), dummy.get(0)));
        } else {
          dummyStatusList.add(new DummyStatus(stringCompletableFutureEntry.getKey()));
        }
      } catch (InterruptedException | ExecutionException e) {
        Thread.currentThread().interrupt(); // sonar expectation
      }
    }
  }

I tried using timeout while getting data e.g.

stringCompletableFutureEntry.getValue().get(Long.parseLong("3000"), TimeUnit.MILLISECONDS))

It'll throw a TimeoutException once the time threshold is reached.

I'm looking for other Solutions to get rid of InterruptedException inside the loop.

0

There are 0 answers