CacheFactory implements AddressChangeListener, FactoryBean<Cache>, InitializingBean, DisposableBean { @Override public void changeAddresses(final List<InetSocketAddress> addresses) { if (isCacheDisabled()) { LOGGER.warn("Cache {} is disabled", cacheName); return; } if (!(cache instanceof CacheImpl)) { LOGGER.warn("This client doesn't support changing memcached addresses on the fly"); return; } try { LOGGER.info("Creating new memcached client for cache {} with new addresses: {}", cacheName, addresses); CacheClient memcacheClient = createClient(addresses); LOGGER.info("New memcached client for cache {} was created with addresses: {}", cacheName, addresses); ((CacheImpl)cache).changeCacheClient(memcacheClient); } catch (IOException e) { if (LOGGER.isErrorEnabled()) { LOGGER.error(String.format("Cannot change memcached client to new one with addresses %s", addresses), e); } } }  @Override void afterPropertiesSet(); @Override Cache getObject(); @Override Class<?> getObjectType(); @Override boolean isSingleton(); @Override void destroy(); @Override void changeAddresses(final List<InetSocketAddress> addresses);  }
@Test public void changeAddresses() throws IOException, NamingException { final CacheConfiguration conf = new CacheConfiguration(); conf.setConsistentHashing(false); AddressProvider addrsProvider = new DefaultAddressProvider("127.0.0.1:11211"); factory.setConfiguration(conf); factory.setAddressProvider(addrsProvider); Cache cache = factory.createCache(); List<InetSocketAddress> newAddrs = Arrays.asList(new InetSocketAddress("127.0.0.2", 11221)); factory.changeAddresses(newAddrs); Collection<SocketAddress> c = cache.getAvailableServers(); assertEquals(1, c.size()); assertEquals(newAddrs, c); verify(cacheClientFactory).create(addrsProvider.getAddresses(), conf); }