Spring Boot + Apache Kafka Tutorial - #17 - Wikimedia Producer and Event Handler Implementation


Welcome to Spring Boot + Apache Kafka Tutorial series. In this lecture, we will implement Wikimedia Producer and Event Handler step by step.

Lecture - #17 - Wikimedia Producer and Event Handler Implementation

Transcript:

Hi. Welcome back. In this lecture, we'll start implementing Wikimedia Produce. So let's go to the idea and let's go to the main package. So look at here we are in a model, not in a buried project. Okay. So go to main package, right click new and then choose class. Let's give a class name is Ricky. Media changes. What is it? Okay, so what is the name we are giving to the. What is it? So he did it and let's annotate this class with add service annotation from spring package. So we want to make this class as a spring bin by using add service annotation. Well, within this class, let's first creater logger instance. So let's have private static final and then logger from shell 40 and then logger record to logger factory and then call get logger method and then pass this Wikimedia class name here. Class. Perfect. We have created a logger to log the messages. Next, let's go ahead and let's inject copycat template from Spring Kafka Library. So we're going to use Kafka template to send a message to the capture broker. So let's first declare a capture template and then we use constructor, basically brilliant injection to inject this Kafka template. So let's first pass key value as a string type. Okay. And let's say the objective is Kafka template. Now let's computer constructor, right click, generate constructor, select this variable and then click on. Okay. Now we have created a constructor for this class. Well, whenever spring finds a single constructor for a spring bin, that spring will inject this dependency. We don't have to basically add add or to add an additional here. All right. So just ignore this annotation. Spring will automatically inject this dependency whenever it will find a single parameter constructor in the spring bin. Okay, great. Next, let's create a method which will read a real time Wikimedia team data. So let's create a method like this. The method is public and the return type is white. And the method name something like send a message. All right. And within this method, let's have a topic, name, post, string topic equal to the topic name. We have given Wikimedia and the Square present change. So this is the topic name we have given. Right. So let me go to Gaga topic on P here and here you can see well, this is the topic name that we have given to the cupcake topic. All right. So let's go back to cumulative. Just put it earlier. Now, in order to read real time Wikimedia streaming data, we are going to use event source. So here I'm going to write the comment to read real time stream data from Wikimedia or huge event source. Well, in order to create an event source and to read the real time you enter data from the Wikimedia, we have to use a couple of libraries to let go and exact a couple of libraries. So go to the browser in a new tab. Just type okay to DB event source marine and in there and go ahead and click on this post link and go to 2.5.0 Origin. And it is a marine dependency that we are going to use that is orchestrated by happen under source, but it is a dependency that we going to use to create a new integer to read the events from the Wikimedia. So let me copy the dependency, go to English idea and go to politics in middle of this particular module. So we're not going to add the dependency to that parent project, in fact, when there is a dependency to that model. Okay, so go to politics in this model and create a dependency section. You are here dependencies. And within this dependency, I'll just paste this dependency. Okay, perfect. Now, if we can see the data. These are data, real time streaming data from the Wikimedia. So it has a decent rate. So we also need to use objects and libraries in order to deal with this JSON. So just go ahead and search for Jackson. Jason Moran, you Dinda and go to this first link. Okay. And go to this two point 13.2. All right. And just grab this marine dependency and go to politics animal again and simply Petco here. Now we have added Jackson credibility. Let's elsewhere to one more Jackson dependency that is data mining dependency. So go ahead and click on this and use the latest version and just copy this dependency and go to politics animal again. And you'd better draw here. Now, we have added these three dependencies. Okay, so here this dependency we have added in order to create a source to read the real time data from the Wikimedia. And these two dependencies we have added to deal with the JSON data. Right. So now if you can see here this icon. So go ahead and click on Lower Mountain Icon here. Well, as soon as we loaded Mountain News, we got this. Yet it cannot resolve this dependency. Well, basically, this or K2 to be open eventually. So this dependency needs a change to dependency. So let's return to dependency for this dependency. So go to browser, go to this mountain repository. And here, just type HTP or case to DP. You do enter and go ahead and, you know, click on this link. So if we can see there squandered square up dot. Okay. So this is the dependency when you do it. So go ahead and click on it and this golden and click on this 4.9.3. So this is the latest release of this dependency. So go ahead and select this. My own dependency. Just copy it again, just right below here. Paste it into dependency and go ahead and click on this mountain icon. So this will load this dependency from the Internet. Now, we have added all the required dependencies to the permanent symbol for it. Now we are going to write the event source code to read the real time even data from the Wikimedia. Now let's go back to our bulldozer class. That is Wikimedia Ginger's producer. Now we're going to write the event source code, which will read realtime stream data from the Wikimedia. Well, in order to handle the events, we want to create a separate class that is working with the change handler. So let me create a class to right click on the package new and then choose class and let's your class name is Ricky media changes handler topic and this handler class will implement event handler you know your entire interface. Make sure that you choose your internal interface from com dot launch. Exactly dot even source package. Now we need to read a couple of minutes from this interface and we need to support the implementation. So just more so in this class and then click on Implement Method and then choose all these methods and click on. Okay. All right. Now, in this class, we need to only implement on message method. Okay. So these other methods like on open and closed on comment on here. So these are the methods we are not going to implement because these are the methods that we're not going to use. We're going to only use on message method. Well, whenever there is a new event in Wikimedia, then this on message method will be trigger and then it will read that event. Okay. Now what we'll do will create a constructor for this class. So before that, let's declare copycat template and just pass key type as a string value type as a string and then copycat template. And also, let's declare one string variable. It takes a topic value. But we'll get my pick now in its creator parameterized constructor, right click generate constructor and select these two rivers and then click on okay. Now we have parameterized constructor and these two parameters we pass whenever we create an instance of this. Glass. All right, perfect. Now just save it and let's go to on message method. So, you know, on message matter, we want to basically called up copycat template. So we put that here. I'm going to add a logger to private static final and then logger. So logger front shell project. And then this should be another instance. And then logger factory don't get logger and then pass the lastname wikimedia changes handler dot class perfect. So go to on message metal over here and just add the loggers statements. So logger dot info and then pass the message. Let's say string dot it and type the message, something like event data, something like this and then placeholder that is into this and then pass the message. So let's say message, you don't get the data. Okay. Now we have added our logging statement to log the messages. I'm not going to use Kafka template to send the. You went to the copy. So let's see. Kafka template dot calls animator. So you can see here that a lot of already send methods we're going to call appropriate one. So let's choose second one. It takes a topic. So go ahead and call this second one and then pass post argument as a topic. Second argument is method you and dot get data. Okay, perfect. Now we have created Wikimedia changes handler. So this will basically trigger whenever there is a new event, you know, Wikimedia. So basically if you can see here, we have implemented on message method, right? So whenever there's a new event in Wikimedia, then this handler will be triggered. And within this handler method, this on message control record. And within this on message method, we have it in the logic to send this message to the topic using copycat template provided send method. Okay. It's pretty simple. Now let's go back to our producer class that is Wikimedia and is polluted. So within this producer, we are going to call this Wikimedia unit handler class. Let's create a new internal object. So let's type. You intend to make sure that you choose you in general from come dot launch directly dot event source package and then this should be a you intend to call to new and then call the implementation class that is cumulative agenda and then pass post parameter as the capture template second parameter as the topic public. Now we have created you and handler. Next, we need to define the event source, whatever that is. First, happy with it. So let me simply copy that detail from here. So this is the recipe within it provides real time accumulate stream data. Right. So let me copy this link and let me pass here by. Right now we need to pass this other to the event source. Next we need to create a new insert which will basically connect to the source that is Wikimedia source and it will read although you ended up. So let's create the event source and make sure that your juju ensures problem cannot launched. Actually not even source package and then call builder. So first we need to create a builder event source builder and then we create the event source object from doing so, build it next new and then you insert dot builder and then you intend to add a push. Parameters are confirmed as the order. So just call you whatever dot create and then pass you all by now we operator event source builder. No not creator. Event source object from this builder. So let's say you insert and then you're in search of critical builder dot build method. Perfect. We have created you into source object. Well, if we can see the internal implementation, even tender event handler internally uses executable service to create threads. Okay. So now what we need to do is we need to, you know, start this event source in a separate thread. So just call your answer. Dot, dot. Okay, perfect. I'm going to provide the slip. Slip for 10 minutes. So just call minutes and then call slip and then 10 minutes. Okay. After 10 minutes. They should be stopped. Okay, so let me see what they are here. So this should be. So this should handle this exception. Okay, perfect. Now, what we have done, basically, we have created a Wikimedia genius handler. So this handler will be triggered whenever there is a, you know, Wikimedia, and then this event source will basically connect to the source that is Wikimedia source. And the source is this. And then this event source will pull all the realtime stream data from this source and then it will trigger the respective handler. Okay. And then within a handler, we have written a code to send that event to the topic. Isn't it?
1 2

Comments