Search in sources :

Example 1 with MemcachedClient

use of net.spy.memcached.MemcachedClient 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 MemcachedClient

use of net.spy.memcached.MemcachedClient in project oxAuth by GluuFederation.

the class MemcacheManual method main.

public static void main(String[] args) throws IOException {
    final MemcachedClient client = createClients();
    final ExecutorService executorService = Executors.newFixedThreadPool(1000, daemonThreadFactory());
    int count = 10000;
    final long start = System.currentTimeMillis();
    for (int i = 0; i < count; i++) {
        final int key = i;
        executorService.execute(new Runnable() {

            @Override
            public void run() {
                // MemcachedClient client = clients.get(random);
                Object toPut = testGrant();
                // Object toPut = UUID.randomUUID().toString();
                OperationFuture<Boolean> set = null;
                for (int j = 0; j < 3; j++) {
                    set = client.set(Integer.toString(key), 60, toPut);
                }
                // block
                OperationStatus status = set.getStatus();
                System.out.println(" key: " + key + ", time: " + (new Date().getTime() - start) + "ms, set: " + status);
                executorService.execute(new Runnable() {

                    @Override
                    public void run() {
                        int random = random();
                        if (random % 3 == 0) {
                            try {
                                Thread.sleep(10000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        Object get = client.get(Integer.toString(key));
                        System.out.println("GET key: " + key + " result: " + get);
                    }
                });
            }
        });
    }
    sleep(40);
    // System.out.println(client.get("myKey"));
    //
    // client.set("myKey", 30, testState());
    //
    // sleep(12);
    // System.out.println(client.get("myKey"));
    //
    // sleep(12);
    // System.out.println(client.get("myKey"));
    client.shutdown();
}
Also used : MemcachedClient(net.spy.memcached.MemcachedClient) OperationStatus(net.spy.memcached.ops.OperationStatus) ExecutorService(java.util.concurrent.ExecutorService) OperationFuture(net.spy.memcached.internal.OperationFuture) Date(java.util.Date)

Example 3 with MemcachedClient

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

the class DomainObjectsAsValuesJUnitTest method testGetPutDomainObject.

@Test
public void testGetPutDomainObject() throws Exception {
    MemcachedClient client = new MemcachedClient(new InetSocketAddress(InetAddress.getLocalHost(), PORT));
    Customer c = new Customer("name0", "addr0");
    Customer c1 = new Customer("name1", "addr1");
    Future<Boolean> f = client.add("keyObj", 10, c);
    assertTrue(f.get());
    Future<Boolean> f1 = client.add("key1", 10, c1);
    assertTrue(f1.get());
    assertEquals(c, client.get("keyObj"));
    assertEquals(c1, client.get("key1"));
    assertNull(client.get("nonExistentkey"));
}
Also used : InetSocketAddress(java.net.InetSocketAddress) MemcachedClient(net.spy.memcached.MemcachedClient) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 4 with MemcachedClient

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

the class GemcachedBinaryClientJUnitTest method testCacheWriterException.

@SuppressWarnings("unchecked")
public void testCacheWriterException() throws Exception {
    MemcachedClient client = createMemcachedClient();
    assertTrue(client.set("key", 0, "value".getBytes()).get());
    client.set("exceptionkey", 0, "exceptionvalue").get();
    GemFireCacheImpl cache = GemFireCacheImpl.getInstance();
    Region region = cache.getRegion(GemFireMemcachedServer.REGION_NAME);
    region.getAttributesMutator().setCacheWriter(new CacheWriterAdapter() {

        @Override
        public void beforeCreate(EntryEvent event) throws CacheWriterException {
            if (event.getKey().equals(KeyWrapper.getWrappedKey("exceptionkey".getBytes()))) {
                throw new RuntimeException("ExpectedStrings: Cache writer exception");
            }
        }

        @Override
        public void beforeUpdate(EntryEvent event) throws CacheWriterException {
            if (event.getKey().equals(KeyWrapper.getWrappedKey("exceptionkey".getBytes()))) {
                throw new RuntimeException("ExpectedStrings: Cache writer exception");
            }
        }
    });
    long start = System.nanoTime();
    try {
        client.set("exceptionkey", 0, "exceptionvalue").get();
        throw new RuntimeException("expected exception not thrown");
    } catch (ExecutionException e) {
    // expected
    }
    assertTrue(client.set("key2", 0, "value2".getBytes()).get());
}
Also used : CacheWriterAdapter(org.apache.geode.cache.util.CacheWriterAdapter) MemcachedClient(net.spy.memcached.MemcachedClient) EntryEvent(org.apache.geode.cache.EntryEvent) GemFireCacheImpl(org.apache.geode.internal.cache.GemFireCacheImpl) Region(org.apache.geode.cache.Region) ExecutionException(java.util.concurrent.ExecutionException) CacheWriterException(org.apache.geode.cache.CacheWriterException)

Example 5 with MemcachedClient

use of net.spy.memcached.MemcachedClient 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)

Aggregations

MemcachedClient (net.spy.memcached.MemcachedClient)52 Test (org.junit.Test)22 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)20 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)17 InetSocketAddress (java.net.InetSocketAddress)11 ExecutionException (java.util.concurrent.ExecutionException)9 ConnectionFactoryBuilder (net.spy.memcached.ConnectionFactoryBuilder)8 ConnectionFactory (net.spy.memcached.ConnectionFactory)7 IOException (java.io.IOException)6 TimeoutException (java.util.concurrent.TimeoutException)6 Map (java.util.Map)5 HashMap (java.util.HashMap)4 ArrayList (java.util.ArrayList)3 Properties (java.util.Properties)3 BinaryConnectionFactory (net.spy.memcached.BinaryConnectionFactory)3 Predicate (com.google.common.base.Predicate)2 Supplier (com.google.common.base.Supplier)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 Config (com.hazelcast.config.Config)2