private PersisterFactory getDelegate() { // Find the first concrete persister factory implementation // Exclude the delegate strategy to avoid circular calls since it may also be exposed in the service registry return serviceLookup.lookup(PersisterFactory.class, "(!(strategy=delegate))"); }
private NotificationStrategy getNotificationStrategy(String filter) { return SERVICE_LOOKUP.lookup(NotificationStrategy.class, filter); } }
@Override public <T> T lookup(String criteria, Void filter) { return delegate.lookup(criteria, filter); }
/** * Gets a reference to the {@link FeedbackRepository feedback repository} if it can be found. * * @return the feedback repository or null if none found */ private FeedbackRepository getRepository() { return SERVICE_LOOKUP.lookup(FeedbackRepository.class, null); }
@Override public <T> T lookup(C criteria, F filter) { Objects.requireNonNull(criteria); // Lookup T service = delegate.lookup(criteria, filter); if (service != null) { return service; } // A service matching the filter is not currently available. // Wait until the system has finished starting up (uptime >= grace period) // while ensuring we've waited for at least WAIT_PERIOD_MS before aborting the search. final long waitUntil = System.currentTimeMillis() + this.waitTimeMs; while (uptimeSupplier.get() < this.gracePeriodInMs || System.currentTimeMillis() < waitUntil) { try { Thread.sleep(this.lookupDelayMs); } catch (InterruptedException e) { LOG.error("Interrupted while waiting for service with search criteria " + criteria + " to become available in the service registry. Aborting."); return null; } service = delegate.lookup(criteria, filter); if (service != null) { return service; } } // Couldn't find a service within the defined time return null; }
@Override public <T> T lookup(C criteria, F filter) { Objects.requireNonNull(criteria); // Lookup T service = delegate.lookup(criteria, filter); if (service != null) { return service; } // A service matching the filter is not currently available. // Wait until the system has finished starting up (uptime >= grace period) // while ensuring we've waited for at least WAIT_PERIOD_MS before aborting the search. final long waitUntil = System.currentTimeMillis() + this.waitTimeMs; while (uptimeSupplier.get() < this.gracePeriodInMs || System.currentTimeMillis() < waitUntil) { try { Thread.sleep(this.lookupDelayMs); } catch (InterruptedException e) { LOG.error("Interrupted while waiting for service with search criteria " + criteria + " to become available in the service registry. Aborting."); return null; } service = delegate.lookup(criteria, filter); if (service != null) { return service; } } // Couldn't find a service within the defined time return null; }
@Override public T getService(BD beanDefinition) { final ServiceFactoryRegistration<F> registration = delegate.lookup(beanDefinition.getClassName(), null); if (registration != null) { final T service = (T) registration.getServiceFactory().createBean(beanDefinition); if (registration.shouldAutowire()) { // Autowire! final AutowireCapableBeanFactory beanFactory = applicationContext.getAutowireCapableBeanFactory(); beanFactory.autowireBean(service); beanFactory.initializeBean(service, "service"); } return service; } return null; }
public static <T, C, F> void verifyConsiderPeriods(ServiceLookup<C, F> serviceLookup, Supplier<C> searchCriteriaSupplier, Runnable registerCallback, long initialDelay, long lookupDelay) throws InterruptedException, ExecutionException, TimeoutException { final C lookupCriteria = searchCriteriaSupplier.get(); final CompletableFuture<Long> future = new CompletableFuture(); CompletableFuture.runAsync(() -> { final long start = System.currentTimeMillis(); final T object = serviceLookup.lookup(lookupCriteria, null); assertNotNull(object); final long took = System.currentTimeMillis() - start; future.complete(took); }); // Wait before making it available new Thread(() -> { try { Thread.sleep(initialDelay); registerCallback.run(); } catch (InterruptedException e) { throw new RuntimeException(e); } }).start(); // Wait for the future to complete long took = future.get((initialDelay + lookupDelay) * 2, TimeUnit.MILLISECONDS); assertThat(took, allOf(greaterThanOrEqualTo(initialDelay), lessThanOrEqualTo(initialDelay + lookupDelay * 2))); } }
@Test public void verifyTimeout() throws InterruptedException, ExecutionException, TimeoutException { final long lookupDelay = 250; final long gracePeriod = 2000; final long waitPeriod = 0; final ServiceRegistry serviceRegistry = new DefaultServiceRegistry(); final ServiceLookup<Class<?>, String> serviceLookup = new ServiceLookupBuilder(new ServiceRegistryLookup(serviceRegistry)) .blocking(gracePeriod, lookupDelay, waitPeriod) .build(); final CompletableFuture<Date> future = new CompletableFuture(); CompletableFuture.runAsync(() -> { final Date date = serviceLookup.lookup(Date.class, null); future.complete(date); }); // Wait for the future to complete Date date = future.get(gracePeriod * 2, TimeUnit.MILLISECONDS); assertNull(date); }