|
| 1 | +package com.github.fhuss.kafka.spring; |
| 2 | + |
| 3 | +import org.apache.kafka.clients.consumer.ConsumerRebalanceListener; |
| 4 | +import org.apache.kafka.common.TopicPartition; |
| 5 | +import org.springframework.beans.factory.annotation.Autowired; |
| 6 | +import org.springframework.boot.ApplicationArguments; |
| 7 | +import org.springframework.boot.ApplicationRunner; |
| 8 | +import org.springframework.boot.SpringApplication; |
| 9 | +import org.springframework.boot.autoconfigure.SpringBootApplication; |
| 10 | +import org.springframework.kafka.core.ConsumerFactory; |
| 11 | +import org.springframework.kafka.listener.AbstractMessageListenerContainer; |
| 12 | +import org.springframework.kafka.listener.KafkaMessageListenerContainer; |
| 13 | +import org.springframework.kafka.listener.MessageListener; |
| 14 | +import org.springframework.kafka.listener.config.ContainerProperties; |
| 15 | + |
| 16 | +import java.util.Collection; |
| 17 | +import java.util.concurrent.CountDownLatch; |
| 18 | +import java.util.concurrent.TimeUnit; |
| 19 | + |
| 20 | +@SpringBootApplication |
| 21 | +public class ConsumerApplicationRunner implements ApplicationRunner { |
| 22 | + |
| 23 | + private final CountDownLatch COUNT_DOWN_LATCH = new CountDownLatch(4); |
| 24 | + |
| 25 | + @Autowired |
| 26 | + private ConsumerFactory<String, String> cf; |
| 27 | + |
| 28 | + public static void main(String[] args) { |
| 29 | + SpringApplication.run(ConsumerApplicationRunner.class, args).close(); |
| 30 | + } |
| 31 | + |
| 32 | + @Override |
| 33 | + public void run(ApplicationArguments args) throws Exception { |
| 34 | + |
| 35 | + ContainerProperties containerProperties = new ContainerProperties("testing"); |
| 36 | + containerProperties.setConsumerRebalanceListener(consumerRebalanceListener()); |
| 37 | + containerProperties.setAckMode(AbstractMessageListenerContainer.AckMode.RECORD); |
| 38 | + containerProperties.setPollTimeout(Integer.MAX_VALUE); |
| 39 | + |
| 40 | + containerProperties.setMessageListener((MessageListener<String, String>) record -> { |
| 41 | + System.out.printf("[%s] Consumed new message : key=%s, value=%s, partition=%s, offset=%s\n", Thread.currentThread().getName(), |
| 42 | + record.key(), |
| 43 | + record.value(), |
| 44 | + record.partition(), |
| 45 | + record.offset()); |
| 46 | + COUNT_DOWN_LATCH.countDown(); |
| 47 | + }); |
| 48 | + |
| 49 | + KafkaMessageListenerContainer<String, String> container = |
| 50 | + new KafkaMessageListenerContainer<>(cf, containerProperties); |
| 51 | + |
| 52 | + container.setClientIdSuffix("spring-consumer-container"); |
| 53 | + container.start(); |
| 54 | + |
| 55 | + if (COUNT_DOWN_LATCH.await(1, TimeUnit.MINUTES)) { |
| 56 | + System.out.println("Messages received"); |
| 57 | + } else { |
| 58 | + System.out.println("Timeout before receiving all messages"); |
| 59 | + } |
| 60 | + container.stop(new Runnable() { |
| 61 | + @Override |
| 62 | + public void run() { |
| 63 | + |
| 64 | + } |
| 65 | + }); |
| 66 | + } |
| 67 | + |
| 68 | + private ConsumerRebalanceListener consumerRebalanceListener() { |
| 69 | + return new ConsumerRebalanceListener() { |
| 70 | + @Override |
| 71 | + public void onPartitionsRevoked(Collection<TopicPartition> partitions) { |
| 72 | + System.out.println("Revoked : " + partitions); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public void onPartitionsAssigned(Collection<TopicPartition> partitions) { |
| 77 | + System.out.println("Assigned : " + partitions); |
| 78 | + } |
| 79 | + }; |
| 80 | + } |
| 81 | +} |
0 commit comments