use of net.spy.memcached.DefaultConnectionFactory in project zm-mailbox by Zimbra.
the class ZimbraMemcachedClient method connect.
/**
* Connects/reconnects the memcached client with server list, protocol and hashing algorithm.
* @param servers memcached server list
* @param useBinaryProtocol if true, use the binary protocol; if false, use the ascii protocol
* @param hashAlgorithm net.spy.memcached.HashAlgorithm enum
* @param defaultExpiry in seconds
* @param defaultTimeout in milliseconds
* @throws ServiceException
*/
public void connect(String[] servers, boolean useBinaryProtocol, String hashAlgorithm, int defaultExpiry, long defaultTimeout) throws ServiceException {
// Force spymemcached to use log4j rather than raw stdout/stderr.
Properties props = System.getProperties();
props.put("net.spy.log.LoggerImpl", "net.spy.memcached.compat.log.Log4JLogger");
HashAlgorithm hashAlgo = DefaultHashAlgorithm.KETAMA_HASH;
if (hashAlgorithm != null && hashAlgorithm.length() > 0) {
HashAlgorithm ha = DefaultHashAlgorithm.valueOf(hashAlgorithm);
if (ha != null)
hashAlgo = ha;
}
int qLen = DefaultConnectionFactory.DEFAULT_OP_QUEUE_LEN;
int bufSize = DefaultConnectionFactory.DEFAULT_READ_BUFFER_SIZE;
MemcachedClient client = null;
StringBuilder serverList = new StringBuilder();
if (servers != null && servers.length > 0) {
// Eliminate duplicates and sort case-insensitively. This negates operator error
// configuring server list with inconsistent order on different memcached clients.
// TreeSet provides deduping and sorting.
TreeSet<String> tset = new TreeSet<String>();
for (int i = 0; i < servers.length; ++i) {
tset.add(servers[i].toLowerCase());
}
for (String s : tset) {
if (serverList.length() > 0)
serverList.append(", ");
serverList.append(s);
}
List<InetSocketAddress> serverAddrs = parseServerList(tset.toArray(new String[0]));
ConnectionFactory cf;
if (useBinaryProtocol)
cf = new BinaryConnectionFactory(qLen, bufSize, hashAlgo);
else
cf = new DefaultConnectionFactory(qLen, bufSize, hashAlgo);
try {
client = new MemcachedClient(cf, serverAddrs);
boolean added = client.addObserver(new ConnObserver());
if (!added)
ZimbraLog.misc.error("Unable to add connection observer to memcached client");
} catch (IOException e) {
throw ServiceException.FAILURE("Unable to initialize memcached client", e);
}
}
MemcachedClient oldClient = null;
synchronized (this) {
oldClient = mMCDClient;
mMCDClient = client;
mDefaultExpiry = defaultExpiry;
mDefaultTimeout = defaultTimeout;
mServerList = serverList.length() > 0 ? serverList.toString() : null;
mHashAlgorithm = hashAlgo.toString();
mBinaryProtocolEnabled = useBinaryProtocol;
}
// New client is ready for use by other threads at this point.
if (oldClient != null)
disconnect(oldClient, 30000);
}
use of net.spy.memcached.DefaultConnectionFactory in project oxCore by GluuFederation.
the class MemcachedProvider method create.
public void create() {
log.debug("Starting MemcachedProvider ...");
try {
final ConnectionFactory connectionFactory;
if (memcachedConfiguration.getConnectionFactoryType() == MemcachedConnectionFactoryType.BINARY) {
connectionFactory = new BinaryConnectionFactory(memcachedConfiguration.getMaxOperationQueueLength(), memcachedConfiguration.getBufferSize());
} else {
connectionFactory = new DefaultConnectionFactory(memcachedConfiguration.getMaxOperationQueueLength(), memcachedConfiguration.getBufferSize());
}
client = new MemcachedClient(connectionFactory, AddrUtil.getAddresses(memcachedConfiguration.getServers()));
testConnection();
log.debug("MemcachedProvider started.");
} catch (Exception e) {
throw new IllegalStateException("Error starting MemcachedProvider", e);
}
}
Aggregations