Search in sources :

Example 1 with ConnectionFactoryBuilder

use of net.spy.memcached.ConnectionFactoryBuilder in project qi4j-sdk by Qi4j.

the class MemcachePoolMixin method activateService.

@Override
public void activateService() throws Exception {
    if (configuration != null) {
        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()) {
            builder.setAuthDescriptor(new AuthDescriptor(new String[] { authMech }, new PlainCallbackHandler(username, password)));
        }
        client = new MemcachedClient(builder.build(), AddrUtil.getAddresses(addresses));
    }
}
Also used : ConnectionFactoryBuilder(net.spy.memcached.ConnectionFactoryBuilder) PlainCallbackHandler(net.spy.memcached.auth.PlainCallbackHandler) AuthDescriptor(net.spy.memcached.auth.AuthDescriptor) MemcachedClient(net.spy.memcached.MemcachedClient) Protocol(net.spy.memcached.ConnectionFactoryBuilder.Protocol)

Example 2 with ConnectionFactoryBuilder

use of net.spy.memcached.ConnectionFactoryBuilder in project DataX by alibaba.

the class ConfigurationChecker method hostReachableCheck.

/**
 * 检查ocs服务器网络是否可达
 */
private static void hostReachableCheck(Configuration config) {
    String proxy = config.getString(Key.PROXY);
    String port = config.getString(Key.PORT);
    String username = config.getString(Key.USER);
    String password = config.getString(Key.PASSWORD);
    AuthDescriptor ad = new AuthDescriptor(new String[] { "PLAIN" }, new PlainCallbackHandler(username, password));
    try {
        MemcachedClient client = new MemcachedClient(new ConnectionFactoryBuilder().setProtocol(ConnectionFactoryBuilder.Protocol.BINARY).setAuthDescriptor(ad).build(), AddrUtil.getAddresses(proxy + ":" + port));
        client.get("for_check_connectivity");
        client.getVersions();
        if (client.getAvailableServers().isEmpty()) {
            throw new RuntimeException("没有可用的Servers: getAvailableServers() -> is empty");
        }
        client.shutdown();
    } catch (Exception e) {
        throw DataXException.asDataXException(OcsWriterErrorCode.HOST_UNREACHABLE, String.format("OCS[%s]服务不可用", proxy), e);
    }
}
Also used : PlainCallbackHandler(net.spy.memcached.auth.PlainCallbackHandler) ConnectionFactoryBuilder(net.spy.memcached.ConnectionFactoryBuilder) AuthDescriptor(net.spy.memcached.auth.AuthDescriptor) MemcachedClient(net.spy.memcached.MemcachedClient) DataXException(com.alibaba.datax.common.exception.DataXException)

Example 3 with ConnectionFactoryBuilder

use of net.spy.memcached.ConnectionFactoryBuilder in project hazelcast by hazelcast.

the class InvalidEndpointTest method attemptMemcacheOnHttpEndpoint.

@Test
public void attemptMemcacheOnHttpEndpoint() throws IOException, InterruptedException {
    Config config = createRestEndpointConfig();
    HazelcastInstance instance = factory.newHazelcastInstance(config);
    // Invalid endpoint - points to REST
    InetSocketAddress address = instance.getCluster().getLocalMember().getSocketAddress(REST);
    ConnectionFactory factory = new ConnectionFactoryBuilder().setOpTimeout(60 * 60 * 60).setDaemon(true).setFailureMode(FailureMode.Retry).build();
    MemcachedClient client = new MemcachedClient(factory, Collections.singletonList(address));
    try {
        client.set("one", 0, "two").get();
        fail("Should not be able to connect");
    } catch (InterruptedException e) {
        ignore(e);
    } catch (ExecutionException e) {
        ignore(e);
    }
    shutdownQuietly(client);
}
Also used : ConnectionFactoryBuilder(net.spy.memcached.ConnectionFactoryBuilder) ConnectionFactory(net.spy.memcached.ConnectionFactory) HazelcastInstance(com.hazelcast.core.HazelcastInstance) Config(com.hazelcast.config.Config) ServerSocketEndpointConfig(com.hazelcast.config.ServerSocketEndpointConfig) RestServerEndpointConfig(com.hazelcast.config.RestServerEndpointConfig) InetSocketAddress(java.net.InetSocketAddress) MemcachedClient(net.spy.memcached.MemcachedClient) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test) SlowTest(com.hazelcast.test.annotation.SlowTest)

Example 4 with ConnectionFactoryBuilder

use of net.spy.memcached.ConnectionFactoryBuilder in project hazelcast by hazelcast.

the class AdvancedNetworkingCommunicationIntegrationTest method getMemcachedClient.

private MemcachedClient getMemcachedClient(HazelcastInstance instance, int port) throws Exception {
    String hostName = instance.getCluster().getLocalMember().getSocketAddress().getHostName();
    InetSocketAddress address = new InetSocketAddress(hostName, port);
    ConnectionFactory factory = new ConnectionFactoryBuilder().setOpTimeout(3000).setDaemon(true).setFailureMode(FailureMode.Retry).build();
    return new MemcachedClient(factory, Collections.singletonList(address));
}
Also used : ConnectionFactoryBuilder(net.spy.memcached.ConnectionFactoryBuilder) ConnectionFactory(net.spy.memcached.ConnectionFactory) InetSocketAddress(java.net.InetSocketAddress) MemcachedClient(net.spy.memcached.MemcachedClient)

Example 5 with ConnectionFactoryBuilder

use of net.spy.memcached.ConnectionFactoryBuilder in project druid by druid-io.

the class MemcachedCacheBenchmark method setUp.

@Override
protected void setUp() throws Exception {
    SerializingTranscoder transcoder = new SerializingTranscoder(// 50 MB
    50 * 1024 * 1024);
    // disable compression
    transcoder.setCompressionThreshold(Integer.MAX_VALUE);
    client = new MemcachedClient(new ConnectionFactoryBuilder().setProtocol(ConnectionFactoryBuilder.Protocol.BINARY).setHashAlg(DefaultHashAlgorithm.FNV1A_64_HASH).setLocatorType(ConnectionFactoryBuilder.Locator.CONSISTENT).setDaemon(true).setFailureMode(FailureMode.Retry).setTranscoder(transcoder).setShouldOptimize(true).build(), AddrUtil.getAddresses(hosts));
    cache = new MemcachedCache(Suppliers.<ResourceHolder<MemcachedClientIF>>ofInstance(StupidResourceHolder.create(client)), new MemcachedCacheConfig() {

        @Override
        public String getMemcachedPrefix() {
            return "druid-memcached-benchmark";
        }

        @Override
        public int getTimeout() {
            return 30000;
        }

        @Override
        public int getExpiration() {
            return 3600;
        }
    }, MemcachedCacheTest.NOOP_MONITOR);
    randBytes = new byte[objectSize * 1024];
    new Random(0).nextBytes(randBytes);
}
Also used : SerializingTranscoder(net.spy.memcached.transcoders.SerializingTranscoder) ConnectionFactoryBuilder(net.spy.memcached.ConnectionFactoryBuilder) Random(java.util.Random) MemcachedClient(net.spy.memcached.MemcachedClient) ResourceHolder(io.druid.collections.ResourceHolder) StupidResourceHolder(io.druid.collections.StupidResourceHolder)

Aggregations

ConnectionFactoryBuilder (net.spy.memcached.ConnectionFactoryBuilder)11 MemcachedClient (net.spy.memcached.MemcachedClient)8 InetSocketAddress (java.net.InetSocketAddress)5 ConnectionFactory (net.spy.memcached.ConnectionFactory)3 AuthDescriptor (net.spy.memcached.auth.AuthDescriptor)3 PlainCallbackHandler (net.spy.memcached.auth.PlainCallbackHandler)3 ArrayList (java.util.ArrayList)2 Random (java.util.Random)2 SerializingTranscoder (net.spy.memcached.transcoders.SerializingTranscoder)2 DataXException (com.alibaba.datax.common.exception.DataXException)1 JsonObject (com.bluenimble.platform.json.JsonObject)1 PluginRegistryException (com.bluenimble.platform.plugins.PluginRegistryException)1 Config (com.hazelcast.config.Config)1 RestServerEndpointConfig (com.hazelcast.config.RestServerEndpointConfig)1 ServerSocketEndpointConfig (com.hazelcast.config.ServerSocketEndpointConfig)1 HazelcastInstance (com.hazelcast.core.HazelcastInstance)1 SlowTest (com.hazelcast.test.annotation.SlowTest)1 ResourceHolder (io.druid.collections.ResourceHolder)1 StupidResourceHolder (io.druid.collections.StupidResourceHolder)1 IOException (java.io.IOException)1