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);
    }
  }
示例#2
0
  public static void main(String[] args) throws Exception {
    CuratorFramework client = null;
    DistributedQueue<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.buildQueue();
      queue.start();

      for (int i = 0; i < 10; i++) {
        queue.put("test-" + i);
        Thread.sleep((long) (3 * Math.random()));
      }
      System.out.println(getCurrentTimeStr() + ": 所有的数据已经生产完毕");

      Thread.sleep(20000);
    } finally {
      CloseableUtils.closeQuietly(client);
      CloseableUtils.closeQuietly(queue);
    }
  }