@Test
  public void test() throws Exception {
    Timing timing = new Timing();
    LeaderSelector leaderSelector = null;
    CuratorFramework client =
        CuratorFrameworkFactory.builder()
            .retryPolicy(new ExponentialBackoffRetry(100, 3))
            .connectString(server.getConnectString())
            .sessionTimeoutMs(timing.session())
            .connectionTimeoutMs(timing.connection())
            .build();
    try {
      client.start();

      MyLeaderSelectorListener listener = new MyLeaderSelectorListener();
      ExecutorService executorPool = Executors.newFixedThreadPool(20);
      leaderSelector = new LeaderSelector(client, "/test", threadFactory, executorPool, listener);

      leaderSelector.autoRequeue();
      leaderSelector.start();

      timing.sleepABit();

      Assert.assertEquals(listener.getLeaderCount(), 1);
    } finally {
      CloseableUtils.closeQuietly(leaderSelector);
      CloseableUtils.closeQuietly(client);
    }
  }
Esempio n. 2
0
 @Override
 public void close() throws IOException {
   CloseableUtils.closeQuietly(cores);
   if (local != null) {
     CloseableUtils.closeQuietly(local);
   }
 }
  public static void main(String[] args) {
    TestingServer server = null;
    CuratorFramework client = null;
    CuratorFramework optionsClient = null;

    try {
      server = new TestingServer();
      client = createSimple(server.getConnectString());
      client.start();
      client.create().creatingParentsIfNeeded().forPath(PATH, "test".getBytes());

      optionsClient =
          createWithOptions(
              server.getConnectString(), new ExponentialBackoffRetry(1000, 3), 1000, 1000);
      optionsClient.start();
      log.info("[{}]", new String(optionsClient.getData().forPath(PATH)));
    } catch (Exception e) {
      log.info("exception throws cause {}", Throwables.getStackTraceAsString(e));
    } finally {
      if (client != null) {
        CloseableUtils.closeQuietly(client);
      }
      if (optionsClient != null) {
        CloseableUtils.closeQuietly(optionsClient);
      }
    }
  }
  public static void main(String[] args) throws Exception {
    CuratorFramework client = null;
    DistributedDelayQueue<String> queue = null;

    try {
      client = CuratorClientFactory.newClient();
      client
          .getCuratorListenable()
          .addListener(
              new CuratorListener() {
                @Override
                public void eventReceived(CuratorFramework client, CuratorEvent event)
                    throws Exception {
                  System.out.println("CuratorEvent: " + event.getType().name());
                }
              });
      client.start();

      QueueConsumer<String> consumer = createQueueConsumer();
      QueueBuilder<String> builder =
          QueueBuilder.builder(client, consumer, createQueueSerializer(), PATH);
      queue = builder.buildDelayQueue();
      queue.start();

      for (int i = 0; i < 10; i++) {
        queue.put("test-" + i, System.currentTimeMillis() + 10000);
      }
      System.out.println(getCurrentTimeStr() + ": 所有的数据已经生产完毕");

      Thread.sleep(20000);
    } finally {
      CloseableUtils.closeQuietly(client);
      CloseableUtils.closeQuietly(queue);
    }
  }
  @Test
  public void testMultiClient() throws Exception {
    CuratorFramework client1 = null;
    CuratorFramework client2 = null;
    try {
      {
        CuratorFramework client =
            CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
        try {
          client.start();
          DistributedBarrier barrier = new DistributedBarrier(client, "/barrier");
          barrier.setBarrier();
        } finally {
          CloseableUtils.closeQuietly(client);
        }
      }

      client1 = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
      client2 = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));

      List<Future<Object>> futures = Lists.newArrayList();
      ExecutorService service = Executors.newCachedThreadPool();
      for (final CuratorFramework c : new CuratorFramework[] {client1, client2}) {
        Future<Object> future =
            service.submit(
                new Callable<Object>() {
                  @Override
                  public Object call() throws Exception {
                    c.start();
                    DistributedBarrier barrier = new DistributedBarrier(c, "/barrier");
                    barrier.waitOnBarrier(10, TimeUnit.MILLISECONDS);
                    return null;
                  }
                });
        futures.add(future);
      }

      Thread.sleep(1000);
      {
        CuratorFramework client =
            CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
        try {
          client.start();
          DistributedBarrier barrier = new DistributedBarrier(client, "/barrier");
          barrier.removeBarrier();
        } finally {
          CloseableUtils.closeQuietly(client);
        }
      }

      for (Future<Object> f : futures) {
        f.get();
      }
    } finally {
      CloseableUtils.closeQuietly(client1);
      CloseableUtils.closeQuietly(client2);
    }
  }
  public static void main(String[] args) throws Exception {
    TestingServer server = new TestingServer();
    CuratorFramework client = null;
    DistributedQueue<String> queue = null;
    try {
      client =
          CuratorFrameworkFactory.newClient(
              server.getConnectString(), new ExponentialBackoffRetry(1000, 3));
      client
          .getCuratorListenable()
          .addListener(
              new CuratorListener() {
                @Override
                public void eventReceived(CuratorFramework client, CuratorEvent event)
                    throws Exception {
                  System.out.println("CuratorEvent: " + event.getType().name());
                }
              });

      client.start();
      AkinDistributedBlockingQueue<String> consumerQueue =
          new AkinDistributedBlockingQueue<String>(
              null,
              new ConnectionStateListener() {

                @Override
                public void stateChanged(CuratorFramework client, ConnectionState newState) {}
              });
      QueueBuilder<String> builder =
          QueueBuilder.builder(client, consumerQueue, createQueueSerializer(), PATH);
      queue = builder.buildQueue();
      consumerQueue.setDistributedQueue(queue);
      queue.start();

      for (int i = 0; i < 10; i++) {
        queue.put(" test-" + i);
        Thread.sleep((long) (3 * Math.random()));
      }

      Thread.sleep(20000);

      for (Object object : consumerQueue) {
        System.out.println(consumerQueue.poll());
      }

    } catch (Exception ex) {

    } finally {
      CloseableUtils.closeQuietly(queue);
      CloseableUtils.closeQuietly(client);
      CloseableUtils.closeQuietly(server);
    }
  }
Esempio n. 7
0
  public static void main(String[] args) throws Exception {
    List<LeaderLatch> leaders = new ArrayList<LeaderLatch>();
    List<CuratorFramework> clients = new ArrayList<CuratorFramework>();

    TestingServer server = new TestingServer();

    try {
      for (int i = 0; i < 10; i++) {
        CuratorFramework client =
            CuratorFrameworkFactory.newClient(
                "192.168.50.202:2181,192.168.50.203:2181,192.168.50.204:2181",
                new ExponentialBackoffRetry(20000, 3));
        clients.add(client);

        LeaderLatch leader = new LeaderLatch(client, "/francis/leader");
        leader.addListener(
            new LeaderLatchListener() {

              @Override
              public void isLeader() {
                // TODO Auto-generated method stub
                System.out.println("I am Leader");
              }

              @Override
              public void notLeader() {
                // TODO Auto-generated method stub
                System.out.println("I am not Leader");
              }
            });

        leaders.add(leader);

        client.start();
        leader.start();
      }

      Thread.sleep(Integer.MAX_VALUE);
    } finally {

      for (CuratorFramework client : clients) {
        CloseableUtils.closeQuietly(client);
      }

      for (LeaderLatch leader : leaders) {
        CloseableUtils.closeQuietly(leader);
      }

      //            CloseableUtils.closeQuietly(server);
    }

    Thread.sleep(Integer.MAX_VALUE);
  }
  @Override
  public void contextDestroyed(ServletContextEvent event) {
    if (exhibitor != null) {
      CloseableUtils.closeQuietly(exhibitor);
      exhibitor = null;
    }

    if (exhibitorCreator != null) {
      for (Closeable closeable : exhibitorCreator.getCloseables()) {
        CloseableUtils.closeQuietly(closeable);
      }
    }
  }
Esempio n. 9
0
  @AfterMethod
  public void closeStuff() throws Exception {

    CloseableUtils.closeQuietly(zkClient);
    LOG.info("ZK Client state {}", zkClient.getState());
    zkClient = null;

    CloseableUtils.closeQuietly(storageInternalZKClient);
    LOG.info("ZK Internal Client state {}", storageInternalZKClient.getState());
    storageInternalZKClient = null;

    CloseableUtils.closeQuietly(zkServer);
    LOG.info("ZK Server Stopped");
    zkServer = null;
  }
 @Override
 public void beforeStop() {
   if (node != null) {
     CloseableUtils.closeQuietly(node);
   }
   requests.stream().filter(requests::remove).forEach(UpdateRequest::shutdown);
 }
Esempio n. 11
0
 @Override
 public void cleanUp() {
   CloseableUtils.closeQuietly(coreDomains);
   if (legacy != null) {
     legacy.close();
   }
   if (client != null) {
     client.close();
   }
 }
  @Override
  public void close() throws IOException {
    for (ServiceCache<T> cache : Lists.newArrayList(caches)) {
      CloseableUtils.closeQuietly(cache);
    }
    for (ServiceProvider<T> provider : Lists.newArrayList(providers)) {
      CloseableUtils.closeQuietly(provider);
    }

    for (Entry<T> entry : services.values()) {
      try {
        internalUnregisterService(entry);
      } catch (KeeperException.NoNodeException ignore) {
        // ignore
      } catch (Exception e) {
        log.error("Could not unregister instance: " + entry.service.getName(), e);
      }
    }

    client.getConnectionStateListenable().removeListener(connectionStateListener);
  }
  private void internalUnregisterService(final Entry<T> entry) throws Exception {
    if (entry != null) {
      synchronized (entry) {
        if (entry.cache != null) {
          CloseableUtils.closeQuietly(entry.cache);
          entry.cache = null;
        }

        String path = pathForInstance(entry.service.getName(), entry.service.getId());
        try {
          client.delete().guaranteed().forPath(path);
        } catch (KeeperException.NoNodeException ignore) {
          // ignore
        }
      }
    }
  }
  private Map<String, String> makeArgsBuilder() {
    Map<String, String> argsBuilder = Maps.newHashMap();

    try {
      URL resource = Resources.getResource(EXHIBITOR_PROPERTIES);
      InputStream stream = resource.openStream();
      try {
        Properties properties = new Properties(System.getProperties());
        properties.load(stream);
        applyProperties(argsBuilder, properties);
      } finally {
        CloseableUtils.closeQuietly(stream);
      }
    } catch (IllegalArgumentException e) {
      log.warn("Could not find " + EXHIBITOR_PROPERTIES);
    } catch (IOException e) {
      log.error("Could not load " + EXHIBITOR_PROPERTIES, e);
    }

    applyProperties(argsBuilder, System.getProperties());

    return argsBuilder;
  }
Esempio n. 15
0
 @Override
 public void close() {
   isOpen.set(false);
   CloseableUtils.closeQuietly(queue);
 }
Esempio n. 16
0
 @Override
 public void close() {
   CloseableUtils.closeQuietly(searcher);
   CloseableUtils.closeQuietly(reader);
   CloseableUtils.closeQuietly(directory);
 }
Esempio n. 17
0
 public void close() throws IOException {
   CloseableUtils.closeQuietly(serviceDiscovery);
 }