Package net.rubyeye.xmemcached

Examples of net.rubyeye.xmemcached.MemcachedClient


  public static void main(String[] args) {
    if (args.length < 1) {
      System.err.println("Useage:java SimpleExample [servers]");
      System.exit(1);
    }
    MemcachedClient memcachedClient = getMemcachedClient(args[0]);
    if (memcachedClient == null) {
      throw new NullPointerException(
          "Null MemcachedClient,please check memcached has been started");
    }
    try {
      // add a,b,c
      System.out.println("Add a,b,c");
      memcachedClient.set("a", 0, "Hello,xmemcached");
      memcachedClient.set("b", 0, "Hello,xmemcached");
      memcachedClient.set("c", 0, "Hello,xmemcached");
      // get a
      String value = memcachedClient.get("a");
      System.out.println("get a=" + value);
      System.out.println("delete a");
      // delete a
      memcachedClient.delete("a");
      // reget a
      value = memcachedClient.get("a");
      System.out.println("after delete,a=" + value);

      System.out.println("Iterate all keys...");
      // iterate all keys
      KeyIterator it = memcachedClient.getKeyIterator(AddrUtil
          .getOneAddress(args[0]));
      while (it.hasNext()) {
        System.out.println(it.next());
      }

    } catch (MemcachedException e) {
      System.err.println("MemcachedClient operation fail");
      e.printStackTrace();
    } catch (TimeoutException e) {
      System.err.println("MemcachedClient operation timeout");
      e.printStackTrace();
    } catch (InterruptedException e) {
      // ignore
    }
    try {
      Thread.sleep(Integer.MAX_VALUE);
      memcachedClient.shutdown();
    } catch (Exception e) {
      System.err.println("Shutdown MemcachedClient fail");
      e.printStackTrace();
    }
  }
View Full Code Here


    XMemcachedClientBuilder builder = new XMemcachedClientBuilder(AddrUtil
        .getAddressMap("localhost:4799,localhost:4798"));
    // It must be in failure mode
    builder.setFailureMode(true);
    MemcachedClient client = builder.build();

    client.setEnableHeartBeat(false);

    try {

      assertEquals("response from server1", client.get("a"));
      assertEquals("response from server1", client.get("a"));
      memServer1.stop();
      Thread.sleep(1000);
      assertEquals("response from server2", client.get("a"));
      // restart server1
      memServer1 = new TCPController();
      memServer1.setHandler(new MockHandler("response from server1"));
      memServer1.setCodecFactory(new TextLineCodecFactory());
      memServer1.bind(new InetSocketAddress(4799));
      Thread.sleep(5000);
      assertEquals("response from server1", client.get("a"));
    } finally {
      memServer1.stop();
      memServer2.stop();
      client.shutdown();
    }

  }
View Full Code Here

    XMemcachedClientBuilder builder = new XMemcachedClientBuilder(AddrUtil
        .getAddressMap("localhost:4799 localhost:4798"));
    // It must be in failure mode
    builder.setFailureMode(true);
    MemcachedClient client = builder.build();

    client.setEnableHeartBeat(false);
    TCPController memServer1 = null;
    try {

      assertEquals("response from server2", client.get("a"));
      try {
        assertEquals("response from server1", client.get("b"));
        fail();
      } catch (MemcachedException e) {
        assertEquals("Session(127.0.0.1:4799) has been closed", e
            .getMessage());
      }
      assertEquals(1, client.getConnector().getSessionByAddress(
          AddrUtil.getOneAddress("localhost:4799")).size());
      memServer1 = new TCPController();
      memServer1.setHandler(new MockHandler("response from server1"));
      memServer1.setCodecFactory(new TextLineCodecFactory());
      memServer1.bind(new InetSocketAddress(4799));
      Thread.sleep(5000);
      assertEquals(1, client.getConnector().getSessionByAddress(
          AddrUtil.getOneAddress("localhost:4799")).size());
      assertEquals("response from server2", client.get("a"));
      assertEquals("response from server1", client.get("b"));
    } finally {
      if (memServer1 != null)
        memServer1.stop();
      memServer2.stop();
      client.shutdown();
    }

  }
View Full Code Here

    XMemcachedClientBuilder builder = new XMemcachedClientBuilder(AddrUtil
        .getAddressMap("localhost:4799,localhost:4798"));
    // It must be in failure mode
    builder.setFailureMode(true);
    MemcachedClient client = builder.build();

    client.setEnableHeartBeat(false);

    try {
      assertEquals("response from server1", client.get("a"));
      assertEquals("response from server1", client.get("a"));
      memServer1.stop();
      Thread.sleep(1000);
      assertEquals("response from server2", client.get("a"));
      memServer2.stop();
      Thread.sleep(1000);
      try {
        client.get("a");
        fail();
      } catch (MemcachedException e) {
        assertEquals("Session(127.0.0.1:4799) has been closed", e
            .getMessage());
        // e.printStackTrace();
      }
      // restart server2
      memServer2 = new TCPController();
      memServer2.setHandler(new MockHandler("response from server2"));
      memServer2.setCodecFactory(new TextLineCodecFactory());
      memServer2.bind(new InetSocketAddress(4798));
      Thread.sleep(5000);
      assertEquals("response from server2", client.get("a"));

      // restart server1
      memServer1 = new TCPController();
      memServer1.setHandler(new MockHandler("response from server1"));
      memServer1.setCodecFactory(new TextLineCodecFactory());
      memServer1.bind(new InetSocketAddress(4799));
      Thread.sleep(10000);
      assertEquals("response from server1", client.get("a"));

    } finally {
      memServer1.stop();
      memServer2.stop();
      client.shutdown();
    }

  }
View Full Code Here

    memServer1.bind(new InetSocketAddress(4799));

    XMemcachedClientBuilder builder = new XMemcachedClientBuilder(AddrUtil
        .getAddressMap("localhost:4799"));
    builder.setFailureMode(true);
    MemcachedClient client = builder.build();

    client.setEnableHeartBeat(false);

    try {

      assertEquals("response from server1", client.get("a"));
      assertEquals("response from server1", client.get("a"));
      memServer1.stop();
      Thread.sleep(1000);
      try {
        client.get("a");
        fail();
      } catch (MemcachedException e) {
        assertEquals("Session(127.0.0.1:4799) has been closed", e
            .getMessage());
        assertTrue(true);
      }
    } finally {
      memServer1.stop();
      client.shutdown();
    }

  }
View Full Code Here

    memServer2.setCodecFactory(new TextLineCodecFactory());
    memServer2.bind(new InetSocketAddress(4798));

    XMemcachedClientBuilder builder = new XMemcachedClientBuilder(AddrUtil
        .getAddressMap("localhost:4799,localhost:4798"));
    MemcachedClient client = builder.build();

    client.setEnableHeartBeat(false);
    try {

      assertEquals("response from server1", client.get("a"));
      assertEquals("response from server1", client.get("a"));
      memServer1.stop();
      Thread.sleep(1000);
      try {
        client.get("a");
        fail();
      } catch (MemcachedException e) {
        assertEquals("There is no available connection at this moment",
            e.getMessage());
        assertTrue(true);
      }
    } finally {
      memServer1.stop();
      memServer2.stop();
      client.shutdown();
    }

  }
View Full Code Here

    memServer1.setCodecFactory(new TextLineCodecFactory());
    memServer1.bind(new InetSocketAddress(4799));

    XMemcachedClientBuilder builder = new XMemcachedClientBuilder(AddrUtil
        .getAddressMap("localhost:4799"));
    MemcachedClient client = builder.build();

    client.setEnableHeartBeat(false);
    try {

      assertEquals("response from server1", client.get("a"));
      assertEquals("response from server1", client.get("a"));
      memServer1.stop();
      Thread.sleep(1000);
      try {
        client.get("a");
        fail();
      } catch (MemcachedException e) {
        assertEquals("There is no available connection at this moment",
            e.getMessage());
        assertTrue(true);
      }
    } finally {
      memServer1.stop();
      client.shutdown();
    }

  }
View Full Code Here

    memServer2.setCodecFactory(new TextLineCodecFactory());
    memServer2.bind(new InetSocketAddress(4798));

    XMemcachedClientBuilder builder = new XMemcachedClientBuilder(AddrUtil
        .getAddressMap("localhost:4799 localhost:4798"));
    MemcachedClient client = builder.build();

    client.setEnableHeartBeat(false);

    try {

      assertEquals("response from server2", client.get("a"));
      assertEquals("response from server2", client.get("a"));
      memServer2.stop();
      Thread.sleep(1000);
      assertEquals("response from server1", client.get("a"));
    } finally {
      memServer1.stop();
      memServer2.stop();
      client.shutdown();
    }

  }
View Full Code Here

        .getAddresses(getServerList()));
    builder.setCommandFactory(getCommandFactory());
    builder.setSessionLocator(getSessionLocator());
    builder.getConfiguration()
        .setSessionReadBufferSize(getReadBufferSize());
    MemcachedClient client = builder.build();
    client.setOpTimeout(getOperationTimeoutMillis());
    return new Xmemcache(client);
  }
View Full Code Here

  public void setUp() throws Exception {
    this.ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
  }

  public void testSimpleConfig() throws Exception {
    MemcachedClient memcachedClient = (MemcachedClient) this.ctx
        .getBean("memcachedClient1");

    validateClient(memcachedClient);
  }
View Full Code Here

TOP

Related Classes of net.rubyeye.xmemcached.MemcachedClient

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.