Пример #1
0
 public void send(SimplifiedLog message, String topicName) {
   if (producer == null) {
     producer =
         KafkaUtils.createProducer("localhost:" + kafkaPort, KafkaProducerType.ASYNC, false);
   }
   producer.send(new KeyedMessage<>(topicName, message.getHostName(), message));
   LOGGER.debug("Sent message: {}", message);
 }
Пример #2
0
  public Callable<Boolean> messagesArrived(final Collection<SimplifiedLog> expected) {
    final ConsumerConnector consumer =
        KafkaUtils.createConsumer(zkServer.getConnectString(), "test_group", "1");
    final ConsumerIterator<String, SimplifiedLog> consumerIterator =
        KafkaUtils.getConsumerIterator(consumer, topicName);
    final List<SimplifiedLog> received = new ArrayList<>();

    return new Callable<Boolean>() {
      @Override
      public Boolean call() throws Exception {
        if (consumerIterator.hasNext()) {
          MessageAndMetadata data = consumerIterator.next();
          received.add((SimplifiedLog) data.message());
          LOGGER.debug(
              "Received message: {} | From partition: {}", data.message(), data.partition());
        }
        consumer.shutdown();
        return received.containsAll(expected);
      }
    };
  }