File logDir = TMP_FOLDER.newFolder();
EmbeddedKafkaServer server = new EmbeddedKafkaServer(generateKafkaConfig(connectionStr, logDir));
server.startAndWait();
// Start a Kafka client
KafkaClientService kafkaClient = new ZKKafkaClientService(zkClient);
kafkaClient.startAndWait();
// Attach a consumer
final BlockingQueue<String> consumedMessages = Queues.newLinkedBlockingQueue();
Cancellable cancelConsumer = kafkaClient.getConsumer()
.prepare().addFromBeginning("test", 0).consume(new KafkaConsumer.MessageCallback() {
@Override
public void onReceived(Iterator<FetchedMessage> messages) {
while (messages.hasNext()) {
consumedMessages.add(Charsets.UTF_8.decode(messages.next().getPayload()).toString());
}
}
@Override
public void finished() {
// No-op
}
});
// Get a publisher and publish a message
KafkaPublisher publisher = kafkaClient.getPublisher(KafkaPublisher.Ack.FIRE_AND_FORGET, Compression.NONE);
publisher.prepare("test").add(Charsets.UTF_8.encode("Message 0"), 0).send().get();
// Should receive one message
Assert.assertEquals("Message 0", consumedMessages.poll(5, TimeUnit.SECONDS));
// Now shutdown and restart the server on different port
server.stopAndWait();
server = new EmbeddedKafkaServer(generateKafkaConfig(connectionStr, logDir));
server.startAndWait();
// Wait a little while to make sure changes is reflected in broker service
TimeUnit.SECONDS.sleep(3);
// Now publish again with the same publisher. It should succeed and the consumer should receive the message.
publisher.prepare("test").add(Charsets.UTF_8.encode("Message 1"), 0).send().get();
Assert.assertEquals("Message 1", consumedMessages.poll(5, TimeUnit.SECONDS));
kafkaClient.stopAndWait();
zkClient.stopAndWait();
server.stopAndWait();
}