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);
    }
  }
  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);
    }
  }
Exemplo n.º 3
0
 public ZooKeeperQueueConsumer(
     WorkflowManagerImpl workflowManager, TaskRunner taskRunner, TaskType taskType) {
   this.taskRunner = Preconditions.checkNotNull(taskRunner, "taskRunner cannot be null");
   workflowManager = Preconditions.checkNotNull(workflowManager, "workflowManager cannot be null");
   String path = ZooKeeperConstants.getQueuePath(taskType);
   QueueBuilder<ExecutableTask> builder =
       QueueBuilder.builder(
           workflowManager.getCurator(),
           this,
           new TaskQueueSerializer(workflowManager.getSerializer()),
           path);
   if (taskType.isIdempotent()) {
     builder = builder.lockPath(ZooKeeperConstants.getQueueLockPath(taskType));
   }
   queue = ZooKeeperQueue.makeQueue(builder, taskType);
 }
  public DistributedDelayQueue<String> distributedDelayQueue() throws KafkaException {
    org.apache.curator.framework.recipes.queue.QueueConsumer<String> consumer =
        new org.apache.curator.framework.recipes.queue.QueueConsumer<String>() {
          @Override
          public void consumeMessage(String message) {
            logger.debug("message received from delayQ: {}", message);
            JsonObject jsonObject = Json.createReader(new StringReader(message)).readObject();
            String queue = jsonObject.getJsonObject(PAYLOAD).getString(QUEUE);
            String queueMessage =
                jsonObject.getJsonObject(PAYLOAD).getJsonObject(PAYLOAD).toString();
            kafkaClient.sendMessage(new KafkaMessage(queue, queueMessage));
          }

          @Override
          public void stateChanged(CuratorFramework client, ConnectionState newState) {
            if (newState.equals(ConnectionState.SUSPENDED)) {
              logger.error("screwed");
            }
          }
        };

    QueueSerializer<String> serializer =
        new QueueSerializer<String>() {
          @Override
          public byte[] serialize(String item) {
            return item.getBytes(Charset.defaultCharset());
          }

          @Override
          public String deserialize(byte[] bytes) {
            return new String(bytes, Charset.defaultCharset());
          }
        };

    QueueBuilder<String> builder =
        QueueBuilder.builder(curatorFramework, consumer, serializer, DELAY_Q_PATH);

    delayQ = builder.buildDelayQueue();
    try {
      delayQ.start();
    } catch (Exception e) {
      logger.error("unable to start delayQ", e);
      throw new KafkaException("unable to start delayQ", e);
    }

    return delayQ;
  }