Listening to a Kafka topic

Let's create a listener NotificationListener class that will be used to listen to messages on the Kafka notification topic and send email and SMS notifications to the customer:

@Component
public class NotificationListener {
@StreamListener(NotificationStreams.INPUT)
public void sendMailNotification(@Payload Notification notification) {
System.out.println("Sent notification to email: "+notification.getEmail()+" Message: "+notification.getMessage());
}
@StreamListener(NotificationStreams.INPUT)
public void sendSMSNotification(@Payload Notification notification) {
System.out.println("Notified with SMS to mobile: "+notification.getMobile()+" Message: "+notification.getMessage());
}
}

The NotificationListener class has two methods—sendMailNotification() and sendSMSNotification(). These methods will be invoked by Spring Cloud Stream with every new Notification message object on the Kafka notification topic. These methods are annotated with @StreamListener. This annotation makes the method listener cause it to receive events for stream processing.

This chapter doesn't have the complete code for this event-driven application, you can find the complete code in the GitHub repository at https://github.com/PacktPublishing/Mastering-Spring-Boot-2.0.

Let's run this application and test how this event-driven works. First, we have to ensure we run Kafka and Zookeeper as we discussed in the previous section. The Kafka server will be run at http://localhost:9092.

Now let's run EurekaServer, ApiZuulService, AccountService, CustomerService, and NotificationService. Let's open Eureka dashboard on the browser:

As you can see, all services are running now, let's create a Customer object to trigger the event to Kafka. Here I am using Postman as a REST client, we will discuss in Postman in Chapter 11Testing Spring Boot Application. Let's see the following diagram, where we have created a new customer using the http://localhost:8080/api/customers/customer API endpoint through Zuul API Gateway:

As you can see, we have entered a new customer record in the database. As we have discussed, whenever a new customer is created, it will trigger a message to Kafka to send email and SMS notifications using the Notification microservice. Let's see the following console output of the Notification microservice:

We have created a new customer using Customer service and it will trigger a notification to be sent to the customer using the Kafka broker. It is a message-driven asynchronous call.

Similarly, whenever we create an account record for a new customer, Kafka will listen for another new notification message for the account creation:

Let's verify the console of the Notification microservice:

As you can see,  we have created an account record for the customer, it has triggered a message to Kafka to send email and SMS notifications to the customer, let's check the customer record for the customer we just created by visiting http://localhost:8080/api/customers/customer/2001:

As you can see, the customer has complete information including an associated account object. So, in this chapter, we created an event-driven microservice using the Spring Cloud Stream, Kafka Event Bus, Spring Netflix Zuul, and Spring Discovery service. You can find the complete code for this chapter in the GitHub repository https://github.com/PacktPublishing/Mastering-Spring-Boot-2.0.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.30.210