コード例 #1
0
  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);
    }
  }
コード例 #2
0
  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;
  }