@Test public void testEurekaClientUnregister() { ThreadPoolExecutor executorMock = EasyMock.createMock(ThreadPoolExecutor.class); EasyMock.expect(executorMock.isShutdown()).andReturn(Boolean.TRUE); EurekaNotificationServerListUpdater serverListUpdater = new EurekaNotificationServerListUpdater( new Provider<EurekaClient>() { @Override public EurekaClient get() { return eurekaClientMock; } }, executorMock ); try { Capture<EurekaEventListener> registeredListener = new Capture<EurekaEventListener>(); eurekaClientMock.registerEventListener(EasyMock.capture(registeredListener)); EasyMock.replay(eurekaClientMock); EasyMock.replay(executorMock); serverListUpdater.start(new ServerListUpdater.UpdateAction() { @Override public void doUpdate() { Assert.fail("should not reach here"); } }); registeredListener.getValue().onEvent(new CacheRefreshedEvent()); } finally { EasyMock.verify(executorMock); EasyMock.verify(eurekaClientMock); } }
@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)) { 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); } } }); } catch (Exception e) { logger.warn("Error submitting update task to executor, skipping one round of updates", e); 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"); } }
EurekaNotificationServerListUpdater implements ServerListUpdater { @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)) { 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); } } }); } catch (Exception e) { logger.warn("Error submitting update task to executor, skipping one round of updates", e); 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"); } } }
EurekaNotificationServerListUpdater implements ServerListUpdater { @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)) { 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); } } }); } catch (Exception e) { logger.warn("Error submitting update task to executor, skipping one round of updates", e); 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"); } } EurekaNotificationServerListUpdater();  EurekaNotificationServerListUpdater(final Provider<EurekaClient> eurekaClientProvider);  EurekaNotificationServerListUpdater(final Provider<EurekaClient> eurekaClientProvider, ExecutorService refreshExecutor); }
EurekaNotificationServerListUpdater implements ServerListUpdater { @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)) { 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); } } }); } catch (Exception e) { logger.warn("Error submitting update task to executor, skipping one round of updates", e); 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"); } } EurekaNotificationServerListUpdater();  EurekaNotificationServerListUpdater(final Provider<EurekaClient> eurekaClientProvider);  EurekaNotificationServerListUpdater(final Provider<EurekaClient> eurekaClientProvider, ExecutorService refreshExecutor); static ExecutorService getDefaultRefreshExecutor(); @Override synchronized void start(final UpdateAction updateAction); @Override synchronized void stop(); @Override String getLastUpdate(); @Override long getDurationSinceLastUpdateMs(); @Override int getNumberMissedCycles(); @Override int getCoreThreads(); }
EurekaNotificationServerListUpdater implements ServerListUpdater { @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)) { 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); } } }); } catch (Exception e) { logger.warn("Error submitting update task to executor, skipping one round of updates", e); 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"); } } EurekaNotificationServerListUpdater();  EurekaNotificationServerListUpdater(final Provider<EurekaClient> eurekaClientProvider);  EurekaNotificationServerListUpdater(final Provider<EurekaClient> eurekaClientProvider, ExecutorService refreshExecutor); static ExecutorService getDefaultRefreshExecutor(); @Override synchronized void start(final UpdateAction updateAction); @Override synchronized void stop(); @Override String getLastUpdate(); @Override long getDurationSinceLastUpdateMs(); @Override int getNumberMissedCycles(); @Override int getCoreThreads();  }