diff --git a/src/main/java/net/rubyeye/xmemcached/XMemcachedClient.java b/src/main/java/net/rubyeye/xmemcached/XMemcachedClient.java index d24d19fc3..946828509 100644 --- a/src/main/java/net/rubyeye/xmemcached/XMemcachedClient.java +++ b/src/main/java/net/rubyeye/xmemcached/XMemcachedClient.java @@ -808,6 +808,9 @@ public XMemcachedClient(final InetSocketAddress inetSocketAddress, throw new IllegalArgumentException("Null InetSocketAddress"); } + if (cmdFactory == null) { + throw new IllegalArgumentException("Null command factory."); + } if (weight <= 0) { throw new IllegalArgumentException("weight<=0"); } @@ -1002,7 +1005,24 @@ private final boolean isWindowsPlatform() { */ public XMemcachedClient(List addressList) throws IOException { + this(addressList, new TextCommandFactory()); + } + + /** + * XMemcached Constructor.Every server's weight is one by default. + * + * @param cmdFactory + * command factory + * @param addressList + * memcached server socket address list. + * @throws IOException + */ + public XMemcachedClient(List addressList, + CommandFactory cmdFactory) throws IOException { super(); + if (cmdFactory == null) { + throw new IllegalArgumentException("Null command factory."); + } if (addressList == null || addressList.isEmpty()) { throw new IllegalArgumentException("Empty address list"); } @@ -1010,8 +1030,8 @@ public XMemcachedClient(List addressList) this.buildConnector(new ArrayMemcachedSessionLocator(), simpleBufferAllocator, XMemcachedClientBuilder.getDefaultConfiguration(), - XMemcachedClientBuilder.getDefaultSocketOptions(), - new TextCommandFactory(), new SerializingTranscoder()); + XMemcachedClientBuilder.getDefaultSocketOptions(), cmdFactory, + new SerializingTranscoder()); this.start0(); for (InetSocketAddress inetSocketAddress : addressList) { this.connect(new InetSocketAddressWrapper(inetSocketAddress, diff --git a/src/main/java/net/rubyeye/xmemcached/aws/AWSElasticCacheClient.java b/src/main/java/net/rubyeye/xmemcached/aws/AWSElasticCacheClient.java index d73d0ba3f..2c7203253 100644 --- a/src/main/java/net/rubyeye/xmemcached/aws/AWSElasticCacheClient.java +++ b/src/main/java/net/rubyeye/xmemcached/aws/AWSElasticCacheClient.java @@ -33,13 +33,13 @@ public class AWSElasticCacheClient extends XMemcachedClient implements private boolean firstTimeUpdate = true; - private InetSocketAddress configAddr; + private List configAddrs = new ArrayList(); public synchronized void onUpdate(ClusterConfigration config) { if (firstTimeUpdate) { firstTimeUpdate = false; - removeConfigAddr(); + removeConfigAddrs(); } List oldList = this.currentClusterConfiguration != null ? this.currentClusterConfiguration @@ -79,14 +79,17 @@ public synchronized void onUpdate(ClusterConfigration config) { this.currentClusterConfiguration = config; } - private void removeConfigAddr() { - this.removeAddr(configAddr); - while (this.getConnector().getSessionByAddress(configAddr) != null - && this.getConnector().getSessionByAddress(configAddr).size() > 0) { - try { - Thread.sleep(50); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); + private void removeConfigAddrs() { + for (InetSocketAddress configAddr : this.configAddrs) { + this.removeAddr(configAddr); + while (this.getConnector().getSessionByAddress(configAddr) != null + && this.getConnector().getSessionByAddress(configAddr) + .size() > 0) { + try { + Thread.sleep(50); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } } } } @@ -105,13 +108,39 @@ public AWSElasticCacheClient(InetSocketAddress addr, } public AWSElasticCacheClient(InetSocketAddress addr, + long pollConfigIntervalMills, CommandFactory cmdFactory) + throws IOException { + this(asList(addr), pollConfigIntervalMills, cmdFactory); + } + + private static List asList(InetSocketAddress addr) { + List addrs = new ArrayList(); + addrs.add(addr); + return addrs; + } + + public AWSElasticCacheClient(List addrs) + throws IOException { + this(addrs, DEFAULT_POLL_CONFIG_INTERVAL_MS); + } + + public AWSElasticCacheClient(List addrs, + long pollConfigIntervalMills) throws IOException { + this(addrs, pollConfigIntervalMills, new TextCommandFactory()); + } + + public AWSElasticCacheClient(List addrs, long pollConfigIntervalMills, CommandFactory commandFactory) throws IOException { - super(addr, 1, commandFactory); + super(addrs, commandFactory); + if (pollConfigIntervalMills <= 0) { + throw new IllegalArgumentException( + "Invalid pollConfigIntervalMills value."); + } // Use failure mode by default. this.commandFactory = commandFactory; this.setFailureMode(true); - this.configAddr = addr; + this.configAddrs = addrs; this.configPoller = new ConfigurationPoller(this, pollConfigIntervalMills); // Run at once to get config at startup. @@ -119,7 +148,7 @@ public AWSElasticCacheClient(InetSocketAddress addr, this.configPoller.run(); if (this.currentClusterConfiguration == null) { throw new IllegalStateException( - "Retrieve ElasticCache config from `" + addr.toString() + "Retrieve ElasticCache config from `" + addrs.toString() + "` failed."); } this.configPoller.start();