public OsgiPersisterFactory(boolean blocking) { if (blocking) { serviceLookup = new ServiceLookupBuilder(new ServiceRegistryLookup(DefaultServiceRegistry.INSTANCE)) .blocking() .build(); } else { serviceLookup = new ServiceLookupBuilder(new ServiceRegistryLookup(DefaultServiceRegistry.INSTANCE)) .build(); } }
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))"); }
public ServiceLookup<C, F> build() { if (this.upTimeSupplier != null) { BlockingServiceLookup lookup = new BlockingServiceLookup(serviceProvider); lookup.setGracePeriodInMs(gracePeriodInMs); lookup.setLookupDelayMs(sleepTimeInMs); lookup.setWaitTimeMs(waitTimeMs); lookup.setUptimeSupplier(upTimeSupplier); return lookup; } return serviceProvider; }
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 ServiceLookupBuilder blocking() { return blocking(GRACE_PERIOD_MS, LOOKUP_DELAY_MS, GRACE_PERIOD_MS); }
@Test public void verifyAlwaysWaits() throws InterruptedException, ExecutionException, TimeoutException { final long lookupDelay = 250; final long gracePeriod = 0; final long initialDelay = 1000; // Delay before service is made available final long waitPeriod = 5000; verifyConsiderPeriods(lookupDelay, waitPeriod, gracePeriod, initialDelay); }
/** * @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(); }
/** * 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); }
public ServiceLookupBuilder blocking() { return blocking(GRACE_PERIOD_MS, LOOKUP_DELAY_MS, GRACE_PERIOD_MS); }
public ServiceLookup<C, F> build() { if (this.upTimeSupplier != null) { BlockingServiceLookup lookup = new BlockingServiceLookup(serviceProvider); lookup.setGracePeriodInMs(gracePeriodInMs); lookup.setLookupDelayMs(sleepTimeInMs); lookup.setWaitTimeMs(waitTimeMs); lookup.setUptimeSupplier(upTimeSupplier); return lookup; } return serviceProvider; }
@Test public void verifyGracePeriod() throws InterruptedException, ExecutionException, TimeoutException { final long lookupDelay = 250; final long waitTime = 500; final long gracePeriod = 5000; // How long do we try? final long initialDelay = 1000; // Delay before service is made available verifyConsiderPeriods(lookupDelay, waitTime, gracePeriod, initialDelay); }
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); }
@Override public <T> T lookup(String criteria, Void filter) { return delegate.lookup(criteria, filter); }
public ServiceLookupBuilder blocking(long gracePeriodInMs, long sleepTimeInMs, long waitTimeMs) { return blocking(gracePeriodInMs, sleepTimeInMs, waitTimeMs, () -> ManagementFactory.getRuntimeMXBean().getUptime()); }
@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 NotificationStrategy getNotificationStrategy(String filter) { return SERVICE_LOOKUP.lookup(NotificationStrategy.class, filter); } }
public ServiceLookupBuilder blocking(long gracePeriodInMs, long sleepTimeInMs, long waitTimeMs) { return blocking(gracePeriodInMs, sleepTimeInMs, waitTimeMs, () -> ManagementFactory.getRuntimeMXBean().getUptime()); }
@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; }