Search in sources :

Example 1 with EurekaEvent

use of com.netflix.discovery.EurekaEvent in project ribbon by Netflix.

the class EurekaNotificationServerListUpdater method start.

@Override
public synchronized void start(final UpdateAction updateAction) {
    if (isActive.compareAndSet(false, true)) {
        this.updateListener = new EurekaEventListener() {

            @Override
            public void onEvent(EurekaEvent event) {
                if (event instanceof CacheRefreshedEvent) {
                    if (!updateQueued.compareAndSet(false, true)) {
                        // if an update is already queued
                        logger.info("an update action is already queued, returning as no-op");
                        return;
                    }
                    if (!refreshExecutor.isShutdown()) {
                        try {
                            refreshExecutor.submit(new Runnable() {

                                @Override
                                public void run() {
                                    try {
                                        updateAction.doUpdate();
                                        lastUpdated.set(System.currentTimeMillis());
                                    } catch (Exception e) {
                                        logger.warn("Failed to update serverList", e);
                                    } finally {
                                        updateQueued.set(false);
                                    }
                                }
                            });
                        // fire and forget
                        } catch (Exception e) {
                            logger.warn("Error submitting update task to executor, skipping one round of updates", e);
                            // if submit fails, need to reset updateQueued to false
                            updateQueued.set(false);
                        }
                    } else {
                        logger.debug("stopping EurekaNotificationServerListUpdater, as refreshExecutor has been shut down");
                        stop();
                    }
                }
            }
        };
        if (eurekaClient == null) {
            eurekaClient = eurekaClientProvider.get();
        }
        if (eurekaClient != null) {
            eurekaClient.registerEventListener(updateListener);
        } else {
            logger.error("Failed to register an updateListener to eureka client, eureka client is null");
            throw new IllegalStateException("Failed to start the updater, unable to register the update listener due to eureka client being null.");
        }
    } else {
        logger.info("Update listener already registered, no-op");
    }
}
Also used : EurekaEvent(com.netflix.discovery.EurekaEvent) EurekaEventListener(com.netflix.discovery.EurekaEventListener) CacheRefreshedEvent(com.netflix.discovery.CacheRefreshedEvent)

Aggregations

CacheRefreshedEvent (com.netflix.discovery.CacheRefreshedEvent)1 EurekaEvent (com.netflix.discovery.EurekaEvent)1 EurekaEventListener (com.netflix.discovery.EurekaEventListener)1