Example usage for org.springframework.beans.factory.support DefaultListableBeanFactory autowireBean

List of usage examples for org.springframework.beans.factory.support DefaultListableBeanFactory autowireBean

Introduction

In this page you can find the example usage for org.springframework.beans.factory.support DefaultListableBeanFactory autowireBean.

Prototype

@Override
    public void autowireBean(Object existingBean) 

Source Link

Usage

From source file:io.gravitee.gateway.service.ratelimit.AsyncRateLimitService.java

@Override
protected void doStart() throws Exception {
    super.doStart();

    DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) ((ConfigurableApplicationContext) applicationContext)
            .getBeanFactory();/*from www .ja  va2 s  . co  m*/
    DefaultListableBeanFactory parentBeanFactory = (DefaultListableBeanFactory) ((ConfigurableApplicationContext) applicationContext
            .getParent()).getBeanFactory();

    // Retrieve the current rate-limit repository implementation
    RateLimitRepository rateLimitRepository = parentBeanFactory.getBean(RateLimitRepository.class);
    LOGGER.debug("Rate-limit repository implementation is {}", rateLimitRepository.getClass().getName());

    if (enabled) {
        // Prepare caches
        RateLimitRepository aggregateCacheRateLimitRepository = new CachedRateLimitRepository(aggregateCache);
        RateLimitRepository localCacheRateLimitRepository = new CachedRateLimitRepository(localCache);

        // Prepare queue to flush data into the final repository implementation
        BlockingQueue<RateLimit> rateLimitsQueue = new BlockingArrayQueue<>(queueCapacity);

        LOGGER.debug("Register rate-limit repository asynchronous implementation {}",
                AsyncRateLimitRepository.class.getName());
        AsyncRateLimitRepository asyncRateLimitRepository = new AsyncRateLimitRepository();
        beanFactory.autowireBean(asyncRateLimitRepository);
        asyncRateLimitRepository.setLocalCacheRateLimitRepository(localCacheRateLimitRepository);
        asyncRateLimitRepository.setAggregateCacheRateLimitRepository(aggregateCacheRateLimitRepository);
        asyncRateLimitRepository.setRateLimitsQueue(rateLimitsQueue);

        LOGGER.info("Register the rate-limit service bridge for synchronous and asynchronous mode");
        DefaultRateLimitService rateLimitService = new DefaultRateLimitService();
        rateLimitService.setRateLimitRepository(rateLimitRepository);
        rateLimitService.setAsyncRateLimitRepository(asyncRateLimitRepository);
        parentBeanFactory.registerSingleton(RateLimitService.class.getName(), rateLimitService);

        // Prepare and start rate-limit poller
        rateLimitPollerExecutor = Executors
                .newSingleThreadScheduledExecutor(r -> new Thread(r, "rate-limit-poller"));
        RateLimitPoller rateLimitPoller = new RateLimitPoller();
        beanFactory.autowireBean(rateLimitPoller);
        rateLimitPoller.setRateLimitRepository(rateLimitRepository);
        rateLimitPoller.setAggregateCacheRateLimitRepository(aggregateCacheRateLimitRepository);

        LOGGER.info("Schedule rate-limit poller at fixed rate: {} {}", polling, TimeUnit.MILLISECONDS);
        rateLimitPollerExecutor.scheduleAtFixedRate(rateLimitPoller, 0L, polling, TimeUnit.MILLISECONDS);

        // Prepare and start rate-limit updater
        rateLimitUpdaterExecutor = Executors.newSingleThreadExecutor(r -> new Thread(r, "rate-limit-updater"));
        RateLimitUpdater rateLimitUpdater = new RateLimitUpdater(rateLimitsQueue);
        beanFactory.autowireBean(rateLimitUpdater);
        rateLimitUpdater.setRateLimitRepository(rateLimitRepository);

        LOGGER.info("Start rate-limit updater");
        rateLimitUpdaterExecutor.submit(rateLimitUpdater);
    } else {
        // By disabling async and cached rate limiting, only the strict mode is allowed
        LOGGER.info("Register the rate-limit service bridge for strict mode only");
        DefaultRateLimitService rateLimitService = new DefaultRateLimitService();
        rateLimitService.setRateLimitRepository(rateLimitRepository);
        rateLimitService.setAsyncRateLimitRepository(rateLimitRepository);
        parentBeanFactory.registerSingleton(RateLimitService.class.getName(), rateLimitService);
    }
}