private <T extends Source> T waitForSource(String id, Class<T> type) throws InterruptedException, InvalidSyntaxException { T source = null; long timeoutLimit = System.currentTimeMillis() + TimeUnit.MINUTES.toMillis(5); boolean available = false; while (!available) { if (source == null) { source = serviceManager .getServiceReferences(type, null) .stream() .map(serviceManager::getService) .filter(src -> id.equals(src.getId())) .findFirst() .orElse(null); } if (source != null) { available = source.isAvailable(); } if (!available) { if (System.currentTimeMillis() > timeoutLimit) { fail("Source (" + id + ") was not created in a timely manner."); } Thread.sleep(100); } } LOGGER.info("Source {} is available.", id); return source; }
/** * This method is necessary for cases where you have nested ResourceLists and it is necessary for * each resource in the upper-level list to re-ask its top-most child if it is available. This is * a must as the resources of the bottom-most resource may change availability while the top-most * will not reflect this change (as it may be second in the queue and therefore never polled for * available resources, and then subsequently the children are never re-polled for availability). */ public void checkAllInternalResources() { for (T t : resources) t.isAvailable(); }