Source:
public static void deleteTwins() throws IOException, InterruptedException { System.out.println("DELETE DIGITAL TWINS"); Map<String, String> twins = FileHelper.loadAllFilesInPath(TwinsPath); for (Map.Entry<String, String> twin : twins.entrySet()) { String twinId = twin.getKey(); List<BasicRelationship> relationshipList = new ArrayList<>(); Semaphore listRelationshipSemaphore = new Semaphore(0); Semaphore deleteRelationshipsSemaphore = new Semaphore(0); CountDownLatch deleteTwinsLatch = new CountDownLatch(1); client.listRelationships(twinId, BasicRelationship.class) .doOnNext(relationshipList::add) .doOnError(IgnoreNotFoundError) .doOnTerminate(listRelationshipSemaphore::release) .subscribe(); client.listIncomingRelationships(twinId) .doOnNext(e -> relationshipList.add(mapper.convertValue(e, BasicRelationship.class))) .doOnError(IgnoreNotFoundError) .doOnTerminate(listRelationshipSemaphore::release) .subscribe(); if (listRelationshipSemaphore.tryAcquire(2, MaxWaitTimeAsyncOperationsInSeconds, TimeUnit.SECONDS)) { relationshipList .forEach(relationship -> client.deleteRelationship(relationship.getSourceId(), relationship.getId()) .doOnSuccess(aVoid -> { if (twinId.equals(relationship.getSourceId())) { System.out.println("Found and deleted relationship: " + relationship.getId()); } else { System.out.println("Found and deleted incoming relationship: " + relationship.getId()); } }) .doOnError(IgnoreNotFoundError) .doOnTerminate(deleteRelationshipsSemaphore::release) .subscribe()); } if (deleteRelationshipsSemaphore.tryAcquire(relationshipList.size(), MaxWaitTimeAsyncOperationsInSeconds, TimeUnit.SECONDS)) { client.deleteDigitalTwin(twinId) .doOnSuccess(aVoid -> System.out.println("Deleted digital twin: " + twinId)) .doOnError(IgnoreNotFoundError) .doOnTerminate(deleteTwinsLatch::countDown) .subscribe(); deleteTwinsLatch.await(MaxWaitTimeAsyncOperationsInSeconds, TimeUnit.SECONDS); } } }

Target:
public static void deleteTwins() throws IOException, InterruptedException { System.out.println("DELETE DIGITAL TWINS"); Map<String, String> twins = FileHelper.loadAllFilesInPath(TwinsPath); for (Map.Entry<String, String> twin : twins.entrySet()) { String twinId = twin.getKey(); List<BasicRelationship> relationshipList = Collections.synchronizedList(new ArrayList<>()); Semaphore listRelationshipSemaphore = new Semaphore(0); Semaphore deleteRelationshipsSemaphore = new Semaphore(0); CountDownLatch deleteTwinsLatch = new CountDownLatch(1); client.listRelationships(twinId, BasicRelationship.class) .doOnNext(relationshipList::add) .doOnError(IgnoreNotFoundError) .doOnTerminate(listRelationshipSemaphore::release) .subscribe(); client.listIncomingRelationships(twinId) .doOnNext(e -> relationshipList.add(mapper.convertValue(e, BasicRelationship.class))) .doOnError(IgnoreNotFoundError) .doOnTerminate(listRelationshipSemaphore::release) .subscribe(); if (listRelationshipSemaphore.tryAcquire(2, MaxWaitTimeAsyncOperationsInSeconds, TimeUnit.SECONDS)) { relationshipList .forEach(relationship -> client.deleteRelationship(relationship.getSourceId(), relationship.getId()) .doOnSuccess(aVoid -> { if (twinId.equals(relationship.getSourceId())) { System.out.println("Found and deleted relationship: " + relationship.getId()); } else { System.out.println("Found and deleted incoming relationship: " + relationship.getId()); } }) .doOnError(IgnoreNotFoundError) .doOnTerminate(deleteRelationshipsSemaphore::release) .subscribe()); } if (deleteRelationshipsSemaphore.tryAcquire(relationshipList.size(), MaxWaitTimeAsyncOperationsInSeconds, TimeUnit.SECONDS)) { client.deleteDigitalTwin(twinId) .doOnSuccess(aVoid -> System.out.println("Deleted digital twin: " + twinId)) .doOnError(IgnoreNotFoundError) .doOnTerminate(deleteTwinsLatch::countDown) .subscribe(); deleteTwinsLatch.await(MaxWaitTimeAsyncOperationsInSeconds, TimeUnit.SECONDS); } } }