Subscriber only service means an application which only consumes, does not produce.
We have generally seen the applications which both produces and consumes data from a Kafka topic but sometimes we need to write an application which only consumes data i.e. consumes data from a 3rd party service. So in this blog I am going to explain how to write a Lagom service which only consumes data (does not produce any data).
Add below dependency to your impl pom.xml:
We need to create a separate module kafka-connect in Lagom project. That module will contain a Service Descriptor declaring the topic service will consume from.
Then, add kafka-connect dependency in impl module:
Then, to consume data implement consumer as below:
Finally, you need to bind KafkaConsumer as Eager singleton and KafkaService in HelloModule:
To connect with external kafka, made below changes in root pom:
and, add below configuration in application.conf file:
That’s it. Now you need to start zookeper and Kafka at your local machine, create topic greetings and then produce any message in Kafka. As a message will be produced in Kafka, consumer will consume data and will print the message.
I hope you enjoyed the blog.
You can get full code here.