Search in sources :

Example 1 with BinaryConnectionFactory

use of net.spy.memcached.BinaryConnectionFactory 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);
}
Also used : DefaultConnectionFactory(net.spy.memcached.DefaultConnectionFactory) InetSocketAddress(java.net.InetSocketAddress) BinaryConnectionFactory(net.spy.memcached.BinaryConnectionFactory) IOException(java.io.IOException) Properties(java.util.Properties) ConnectionFactory(net.spy.memcached.ConnectionFactory) DefaultConnectionFactory(net.spy.memcached.DefaultConnectionFactory) BinaryConnectionFactory(net.spy.memcached.BinaryConnectionFactory) TreeSet(java.util.TreeSet) MemcachedClient(net.spy.memcached.MemcachedClient) DefaultHashAlgorithm(net.spy.memcached.DefaultHashAlgorithm) HashAlgorithm(net.spy.memcached.HashAlgorithm)

Example 2 with BinaryConnectionFactory

use of net.spy.memcached.BinaryConnectionFactory in project geode by apache.

the class GemcachedBinaryClientJUnitTest method createMemcachedClient.

@Override
protected MemcachedClient createMemcachedClient() throws IOException, UnknownHostException {
    List<InetSocketAddress> addrs = new ArrayList<InetSocketAddress>();
    addrs.add(new InetSocketAddress(InetAddress.getLocalHost(), PORT));
    MemcachedClient client = new MemcachedClient(new BinaryConnectionFactory(), addrs);
    return client;
}
Also used : InetSocketAddress(java.net.InetSocketAddress) MemcachedClient(net.spy.memcached.MemcachedClient) ArrayList(java.util.ArrayList) BinaryConnectionFactory(net.spy.memcached.BinaryConnectionFactory)

Example 3 with BinaryConnectionFactory

use of net.spy.memcached.BinaryConnectionFactory 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);
    }
}
Also used : DefaultConnectionFactory(net.spy.memcached.DefaultConnectionFactory) BinaryConnectionFactory(net.spy.memcached.BinaryConnectionFactory) ConnectionFactory(net.spy.memcached.ConnectionFactory) DefaultConnectionFactory(net.spy.memcached.DefaultConnectionFactory) MemcachedClient(net.spy.memcached.MemcachedClient) BinaryConnectionFactory(net.spy.memcached.BinaryConnectionFactory)

Aggregations

BinaryConnectionFactory (net.spy.memcached.BinaryConnectionFactory)3 MemcachedClient (net.spy.memcached.MemcachedClient)3 InetSocketAddress (java.net.InetSocketAddress)2 ConnectionFactory (net.spy.memcached.ConnectionFactory)2 DefaultConnectionFactory (net.spy.memcached.DefaultConnectionFactory)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Properties (java.util.Properties)1 TreeSet (java.util.TreeSet)1 DefaultHashAlgorithm (net.spy.memcached.DefaultHashAlgorithm)1 HashAlgorithm (net.spy.memcached.HashAlgorithm)1