Implement Kafka Producer & Consumer
Overview
Follow the steps detailed below to implement Kafka Producer & Consumer.
Producer
Producer classes help in pushing data from the application to Kafka topics. DIGIT has a custom implementation of KafkaTemplate class in the tracer library called CustomKafkaTemplate. This implementation of the Producer class does not change across services of DIGIT.
Steps
Access the producer implementation details here - Producer Implementation.
The Codegen jar already has created a Producer class. We will continue using it.
Make sure the
tracerdependency version in thepom.xmlis 2.0.0-SNAPSHOT.
Consumer
For our guide, we will be implementing a notification consumer in the following section.
Once an application is created/requested or progresses further in the workflow, notifications can be triggered as each of these events is pushed onto Kafka topics which can be listened to and an sms/email/in-app notification can be sent to the concerned user(s).
For our guide, we will be implementing a notification consumer which will listen to the topic on which birth registration applications are created. Create a customised message and send it to the notification service (sms/email) to trigger notifications to the concerned users.
Steps
Open
Kafka/NotificationConsumer.javaand paste the following code:
Create a POJO by the name of SMSRequest in the
web.modelspackage and add the following content to it:
Create a class by the name of
NotificationServiceunderservicefolder to handle preparation of customised messages and pushing the notifications.Add the following content to it -
Last updated
Was this helpful?