Java Code Examples for net.spy.memcached.ConnectionFactoryBuilder#build()

The following examples show how to use net.spy.memcached.ConnectionFactoryBuilder#build() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MemcacheClientFactoryImpl.java    From simple-spring-memcached with MIT License 6 votes vote down vote up
@Override
public CacheClient create(final List<InetSocketAddress> addrs, final CacheConfiguration conf) throws IOException {
    // currently its works because this factory creates clients with the same connection settings, only memcached
    // addresses can be changed
    if (connectionFactory == null) {
        ConnectionFactoryBuilder builder = new ConnectionFactoryBuilder();

        if (conf.isConsistentHashing()) {
            builder.setHashAlg(DefaultHashAlgorithm.KETAMA_HASH);
            builder.setLocatorType(Locator.CONSISTENT);
        }

        builder.setProtocol(conf.isUseBinaryProtocol() ? Protocol.BINARY : Protocol.TEXT);
        if (conf.getOperationTimeout() != null) {
            builder.setOpTimeout(conf.getOperationTimeout());
        }

        if (conf instanceof SpymemcachedConfiguration) {
            setProviderSpecificSettings(builder, (SpymemcachedConfiguration) conf);
        }

        connectionFactory = builder.build();
    }

    return new MemcacheClientWrapper(new MemcachedClient(connectionFactory, addrs));
}
 
Example 2
Source File: MemcachePoolMixin.java    From attic-polygene-java with Apache License 2.0 5 votes vote down vote up
@Override
public void activateService()
    throws Exception
{
    MemcacheConfiguration config = configuration.get();
    expiration = ( config.expiration().get() == null )
                 ? 3600
                 : config.expiration().get();
    String addresses = ( config.addresses().get() == null )
                       ? "localhost:11211"
                       : config.addresses().get();
    Protocol protocol = ( config.protocol().get() == null )
                        ? Protocol.TEXT
                        : Protocol.valueOf( config.protocol().get().toUpperCase() );
    String username = config.username().get();
    String password = config.password().get();
    String authMech = config.authMechanism().get() == null
                      ? "PLAIN"
                      : config.authMechanism().get();

    ConnectionFactoryBuilder builder = new ConnectionFactoryBuilder();
    builder.setProtocol( protocol );
    if( username != null && !username.isEmpty() )
    {
        String[] authType = { authMech };
        AuthDescriptor to = new AuthDescriptor( authType, new PlainCallbackHandler( username, password ) );
        builder.setAuthDescriptor( to );
    }

    client = new MemcachedClient( builder.build(), AddrUtil.getAddresses( addresses ) );
}
 
Example 3
Source File: MemcachedBlockCache.java    From hbase with Apache License 2.0 5 votes vote down vote up
public MemcachedBlockCache(Configuration c) throws IOException {
  LOG.info("Creating MemcachedBlockCache");

  long opTimeout = c.getLong(MEMCACHED_OPTIMEOUT_KEY, MEMCACHED_DEFAULT_TIMEOUT);
  long queueTimeout = c.getLong(MEMCACHED_TIMEOUT_KEY, opTimeout + MEMCACHED_DEFAULT_TIMEOUT);
  boolean optimize = c.getBoolean(MEMCACHED_OPTIMIZE_KEY, MEMCACHED_OPTIMIZE_DEFAULT);

  ConnectionFactoryBuilder builder = new ConnectionFactoryBuilder()
      .setOpTimeout(opTimeout)
      .setOpQueueMaxBlockTime(queueTimeout) // Cap the max time before anything times out
      .setFailureMode(FailureMode.Redistribute)
      .setShouldOptimize(optimize)
      .setDaemon(true)                      // Don't keep threads around past the end of days.
      .setUseNagleAlgorithm(false)          // Ain't nobody got time for that
      .setReadBufferSize(HConstants.DEFAULT_BLOCKSIZE * 4 * 1024); // Much larger just in case

  // Assume only the localhost is serving memecached.
  // A la mcrouter or co-locating memcached with split regionservers.
  //
  // If this config is a pool of memecached servers they will all be used according to the
  // default hashing scheme defined by the memcache client. Spy Memecache client in this
  // case.
  String serverListString = c.get(MEMCACHED_CONFIG_KEY,"localhost:11211");
  String[] servers = serverListString.split(",");
  List<InetSocketAddress> serverAddresses = new ArrayList<>(servers.length);
  for (String s:servers) {
    serverAddresses.add(Addressing.createInetSocketAddressFromHostAndPortStr(s));
  }

  client = new MemcachedClient(builder.build(), serverAddresses);
}
 
Example 4
Source File: MemcacheClientFactoryImpl.java    From simple-spring-memcached with MIT License 5 votes vote down vote up
@Override
public CacheClient create(final List<InetSocketAddress> addrs, final CacheConfiguration conf) throws IOException {
    // currently its works because this factory creates clients with the same connection settings, only memcached
    // addresses can be changed
    if (connectionFactory == null) {
        ElastiCacheConfiguration elasticacheConf = null;
        if (conf instanceof ElastiCacheConfiguration) {
            elasticacheConf = (ElastiCacheConfiguration) conf;
        }
        
        if (elasticacheConf != null && Boolean.TRUE.equals(elasticacheConf.getUseAutoDiscovery())) {
            // there is no way to use custom client settings and auto discovery together
            LOGGER.info("All cache settings will be ignored because useAutoDiscovery is true");
            return new MemcacheClientWrapper(new MemcachedClient(addrs));
        }
        
        
        ConnectionFactoryBuilder builder = new ConnectionFactoryBuilder();

        if (conf.isConsistentHashing()) {
            builder.setHashAlg(DefaultHashAlgorithm.KETAMA_HASH);
            builder.setLocatorType(Locator.CONSISTENT);
        }

        builder.setProtocol(conf.isUseBinaryProtocol() ? Protocol.BINARY : Protocol.TEXT);
        if (conf.getOperationTimeout() != null) {
            builder.setOpTimeout(conf.getOperationTimeout());
        }

        if (elasticacheConf != null) {
            setProviderSpecificSettings(builder, elasticacheConf);
        }

        connectionFactory = builder.build();
    }

    return new MemcacheClientWrapper(new MemcachedClient(connectionFactory, addrs));
}
 
Example 5
Source File: ApiMemcached.java    From iaf with Apache License 2.0 5 votes vote down vote up
public ApiMemcached() {
	AppConstants ac = AppConstants.getInstance();
	String address = ac.getProperty("etag.cache.server", "localhost:11211");
	String username = ac.getProperty("etag.cache.username", "");
	String password = ac.getProperty("etag.cache.password", "");
	int timeout = ac.getInt("etag.cache.timeout", 10);

	List<InetSocketAddress> addresses = AddrUtil.getAddresses(address);

	ConnectionFactoryBuilder connectionFactoryBuilder = new ConnectionFactoryBuilder()
		.setProtocol(Protocol.BINARY)
		.setOpTimeout(timeout)
		.setInitialObservers(Collections.singleton(obs));

	if(addresses.size()  > 1)
		connectionFactoryBuilder.setFailureMode(FailureMode.Redistribute);
	else
		connectionFactoryBuilder.setFailureMode(FailureMode.Retry);

	if(!username.isEmpty())
		connectionFactoryBuilder.setAuthDescriptor(AuthDescriptor.typical(username, password));

	ConnectionFactory cf = connectionFactoryBuilder.build();

	try {
		client = new MemcachedClient(cf, addresses);
		//Fetching a none-existing key to test the connection
		Future<Object> future = client.asyncGet("test-connection");
		future.get(timeout, TimeUnit.MILLISECONDS);
	} catch (Exception e) {
		ConfigurationWarnings.add(log, "Unable to connect to one or more memcached servers.");
	}
}