public TelemetryServiceRegistryImpl(Supplier<ServiceLoader<F>> staticServiceSupplier, long gracePeriodMs, long waitPeriodMs, long lookupDelayMs) { this.delegate = new ServiceLookupBuilder(new ServiceLookup<String, Void>() { @Override public <T> T lookup(String criteria, Void filter) { return (T) m_serviceFactoryByClassName.get(criteria); } }).blocking(gracePeriodMs, lookupDelayMs, waitPeriodMs).build(); if (staticServiceSupplier != null) { // Register all of the factories exposed via the service loader for (F serviceFactory : staticServiceSupplier.get()) { final String className = serviceFactory.getBeanClass().getCanonicalName(); m_serviceFactoryByClassName.put(className, new ServiceFactoryRegistration(serviceFactory, true)); } } }
public OsgiPersisterFactory(boolean blocking) { if (blocking) { serviceLookup = new ServiceLookupBuilder(new ServiceRegistryLookup(DefaultServiceRegistry.INSTANCE)) .blocking() .build(); } else { serviceLookup = new ServiceLookupBuilder(new ServiceRegistryLookup(DefaultServiceRegistry.INSTANCE)) .build(); } }
/** * @param gracePeriodInMs a grace period of time to allow the implementation to show up on initial startup * @param sleepTimeInMs how long to sleep in between attempts to find an implementation of the service * @param waitTimeMs how long to block waiting for an implementation of the service */ @SuppressWarnings("unchecked") public AlarmFeedbackDaoImpl(long gracePeriodInMs, long sleepTimeInMs, long waitTimeMs) { SERVICE_LOOKUP = new ServiceLookupBuilder(new ServiceRegistryLookup(DefaultServiceRegistry.INSTANCE)) .blocking(gracePeriodInMs, sleepTimeInMs, waitTimeMs) .build(); }
@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); }
private static void verifyConsiderPeriods(long lookupDelay, long waitTime, long gracePeriod, long initialDelay) throws InterruptedException, ExecutionException, TimeoutException { final ServiceRegistry serviceRegistry = new DefaultServiceRegistry(); final ServiceLookup<Class<?>, String> serviceLookup = new ServiceLookupBuilder(new ServiceRegistryLookup(serviceRegistry)) .blocking(gracePeriod, lookupDelay, waitTime) .build(); verifyConsiderPeriods(serviceLookup, () -> Date.class, () -> serviceRegistry.register(new Date(), Date.class), initialDelay, lookupDelay); }