ReactiveEndToEndLatency(Map<String, Object> consumerPropsOverride, Map<String, Object> producerPropsOverride, String bootstrapServers, String topic) { super(consumerPropsOverride, producerPropsOverride, bootstrapServers, topic); sender = KafkaSender.create(SenderOptions.create(producerProps)); ReceiverOptions<byte[], byte[]> receiverOptions = ReceiverOptions.<byte[], byte[]>create(consumerProps) .addAssignListener(partitions -> { if (assignSemaphore.availablePermits() == 0) { partitions.forEach(p -> p.seekToEnd()); assignSemaphore.release(); } }) .subscription(Collections.singleton(topic)); flux = KafkaReceiver.create(receiverOptions) .receive(); receiveQueue = new LinkedBlockingQueue<>(); System.out.println("Running latency test using Reactive API, class=" + this.getClass().getName()); } public void initialize() {
/** * Consume from latest offsets of partitions by seeking to end of all partitions in the assign listener. */ @Test public void seekToEnd() throws Exception { sendMessages(topic, 0, 10); Semaphore assignSemaphore = new Semaphore(0); receiverOptions = receiverOptions .addAssignListener(partitions -> { for (ReceiverPartition p : partitions) p.seekToEnd(); assignSemaphore.release(); }) .subscription(Collections.singleton(topic)); for (TopicPartition partition : cluster.partitions(topic)) receiveStartOffsets.put(partition, (long) cluster.log(partition).size()); CountDownLatch latch = new CountDownLatch(10); Disposable disposable = asyncReceive(latch); assertTrue("Assign callback not invoked", assignSemaphore.tryAcquire(1, TimeUnit.SECONDS)); sendMessages(topic, 10, 20); assertTrue("Messages not received", latch.await(1, TimeUnit.SECONDS)); verifyMessages(10); disposable.dispose(); }
@Test public void seekToEnd() throws Exception { int count = 10; sendMessages(0, count); receiverOptions = receiverOptions .addAssignListener(partitions -> { for (ReceiverPartition partition : partitions) partition.seekToEnd(); onPartitionsAssigned(partitions); }) .subscription(Collections.singletonList(topic)); Flux<? extends ConsumerRecord<Integer, String>> kafkaFlux = KafkaReceiver.create(receiverOptions) .receive(); sendReceiveWithSendDelay(kafkaFlux, Duration.ofMillis(100), count, count); }