Links

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 to implement Kafka Producer

  1. 1.
    Access the producer implementation details here - Producer Implementation.
    The Codegen jar already has created a Producer class. We will continue using it.
  2. 2.
    Make sure the tracer dependency version in the pom.xml is 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.
Sending SMS notifications to the customer:
Once an application is created/updated the data is pushed on Kafka topic. We trigger notifications by consuming data from this topic. Whenever any message is consumed the service will call the localisation service to fetch the SMS template. It will then replace the placeholders in the SMS template with the values in the message it consumed.
(For example, It will replace the {NAME} placeholder with the owner name from the data consumed). Once the SMS text is ready, the service pushes this data on the notification topic. SMS service consumes data from notification topic and triggers SMS.

Steps to implement Kafka Consumer

  1. 1.
    Open Kafka/NotificationConsumer.java and paste the following code:
1
package digit.kafka;
2
3
import com.fasterxml.jackson.databind.ObjectMapper;
4
import digit.service.NotificationService;
5
import digit.web.models.BirthRegistrationRequest;
6
import lombok.extern.slf4j.Slf4j;
7
import org.springframework.beans.factory.annotation.Autowired;
8
import org.springframework.kafka.annotation.KafkaListener;
9
import org.springframework.kafka.support.KafkaHeaders;
10
import org.springframework.messaging.handler.annotation.Header;
11
import org.springframework.stereotype.Component;
12
13
import java.util.HashMap;
14
15
@Component
16
@Slf4j
17
public class NotificationConsumer {
18
19
@Autowired
20
private ObjectMapper mapper;
21
22
@Autowired
23
private NotificationService notificationService;
24
25
@KafkaListener(topics = {"${egov.bt.registration.create.topic}"})
26
public void listen(final HashMap<String, Object> record, @Header(KafkaHeaders.RECEIVED_TOPIC) String topic) {
27
28
try {
29
30
BirthRegistrationRequest request = mapper.convertValue(record, BirthRegistrationRequest.class);
31
//log.info(request.toString());
32
notificationService.prepareEventAndSend(request);
33
34
} catch (final Exception e) {
35
36
log.error("Error while listening to value: " + record + " on topic: " + topic + ": ", e);
37
}
38
}
39
40
}
41
  1. 2.
    Create a POJO by the name of SMSRequest in the web.models package and add the following content to it:
1
package digit.web.models;
2
3
import lombok.*;
4
5
@Getter
6
@AllArgsConstructor
7
@NoArgsConstructor
8
@Builder
9
@ToString
10
public class SMSRequest {
11
private String mobileNumber;
12
private String message;
13
}
14
  1. 3.
    Create a class by the name of NotificationService under service folder to handle preparation of customised messages and pushing the notifications.
    Add the following content to it -
1
package digit.service;
2
3
import digit.config.BTRConfiguration;
4
import digit.kafka.Producer;
5
import digit.web.models.BirthRegistrationApplication;
6
import digit.web.models.BirthRegistrationRequest;
7
import digit.web.models.SMSRequest;
8
import lombok.extern.slf4j.Slf4j;
9
import org.springframework.beans.factory.annotation.Autowired;
10
import org.springframework.stereotype.Service;
11
import org.springframework.web.client.RestTemplate;
12
13
import java.util.ArrayList;
14
import java.util.List;
15
@Slf4j
16
@Service
17
public class NotificationService {
18
19
@Autowired
20
private Producer producer;
21
22
@Autowired
23
private BTRConfiguration config;
24
25
@Autowired
26
private RestTemplate restTemplate;
27
28
private static final String smsTemplate = "Dear {FATHER_NAME} and {MOTHER_NAME} your birth registration application has been successfully created on the system with application number - {APPNUMBER}.";
29
30
public void prepareEventAndSend(BirthRegistrationRequest request){
31
List<SMSRequest> smsRequestList = new ArrayList<>();
32
request.getBirthRegistrationApplications().forEach(application -> {
33
SMSRequest smsRequestForFather = SMSRequest.builder().mobileNumber(application.getFatherMobileNumber()).message(getCustomMessage(smsTemplate, application)).build();
34
SMSRequest smsRequestForMother = SMSRequest.builder().mobileNumber(application.getMotherMobileNumber()).message(getCustomMessage(smsTemplate, application)).build();
35
smsRequestList.add(smsRequestForFather);
36
smsRequestList.add(smsRequestForMother);
37
});
38
for (SMSRequest smsRequest : smsRequestList) {
39
producer.push(config.getSmsNotificationTopic(), smsRequest);
40
log.info("Messages: " + smsRequest.getMessage());
41
}
42
}
43
44
private String getCustomMessage(String template, BirthRegistrationApplication application) {
45
template = template.replace("{APPNUMBER}", application.getApplicationNumber());
46
template = template.replace("{FATHER_NAME}", application.getFather().getName());
47
template = template.replace("{MOTHER_NAME}", application.getMother().getName());
48
return template;
49
}
50
51
}
All content on this page by eGov Foundation is licensed under a Creative Commons Attribution 4.0 International License.