Post pic - get it? Because he is Franz Kafka!
Lame jokes aside, we’ve already talked about Kafka testing and why I don’t like annotations. So let’s combine those two things and talk about how to make “Spring for Apache Kafka” work but without annotations?
First of all, why don’t I use just plain Kafka library for Java? Because it’s a bit too “raw”. In samples for consuming messages infinite while(true)
loop is used, which seems a bit weird (or maybe I haven’t seen enough production code that do that?).
Spring-kafka, as most Spring-related libraries, likes annotations. Fortunately, docs include both approaches - plain Java code and annotations, so it’s not that bad.
Also, I went for “Spring for Apache Kafka” in hope of easier configuration. And while I do complain about EmbeddedKafka, setting up consumer and producer was fairly painless process. Docs are pretty good. Maybe they could be structured differently (sections order is a bit weird), but that’s just my personal opinion.
Let’s get to code. How do I consume message from Kafka?
Code is based on spring-kafka version 2.1.0.RELEASE
Not that bad, right?
You create consumer configuration and KafkaConsumerFactory
where you configure everything: addresses, parsing etc. Then you create ContainerProperties
which specifies Kafka topic and what to do with message.
In short: KafkaConsumerFactory
says HOW and ContainerProperties
says WHAT.
Consuming done, how to produce messages? It’s even easier.
There is really not much to add. Code is pretty simple and we didn’t use any annotation. Great success!
You can see sample code on my Github: https://github.com/douevencode/spring-kafka-sample
(Code might be structured differently because this way is more readable for blog post, but for reading normal code it might be not the best choice.)