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; }