1 00:00:00,680 --> 00:00:04,580 We've now spent a pretty good amount of time learning about gnats and I would say that you pretty much 2 00:00:04,580 --> 00:00:09,040 know the basics of what you need to know to really make use of it inside of some application. 3 00:00:09,050 --> 00:00:12,740 Now at this point I can't take a look at the code that we've written now inside this listener file and 4 00:00:12,740 --> 00:00:17,420 the publisher file and I just want to point out that there is a lot of boilerplate inside of here to 5 00:00:17,420 --> 00:00:21,670 just get to the point where we can actually receive a message and do something with it. 6 00:00:21,680 --> 00:00:24,440 So in total we essentially a forty two lines of code. 7 00:00:24,680 --> 00:00:26,890 We're only just about one line. 8 00:00:26,930 --> 00:00:33,470 Really just this right here has some logic related to really running some information or processing 9 00:00:33,470 --> 00:00:35,000 some incoming event. 10 00:00:35,030 --> 00:00:40,100 This line really is the only one where we receive some event data and do something with it. 11 00:00:40,130 --> 00:00:45,500 So it took us 41 lines to get one line of essentially business logic put together. 12 00:00:45,500 --> 00:00:48,520 Now eventually we're going to have a lot of listeners inside of application. 13 00:00:48,520 --> 00:00:51,370 There are going to be many different kinds of events we have to listen to. 14 00:00:51,470 --> 00:00:56,750 And as you can imagine we do not want to write out this entire 41 lines of setup for every different 15 00:00:56,750 --> 00:00:58,940 event that we're going to try to deal with. 16 00:00:58,940 --> 00:01:06,560 So with that in mind starting this video we're going to try to refactor a lot of this logic around publishing 17 00:01:06,590 --> 00:01:11,180 and receiving messages to make it a lot easier to publish and receive. 18 00:01:11,330 --> 00:01:15,250 We're going to write out an initial implementation to make all this stuff a lot easier. 19 00:01:15,260 --> 00:01:19,220 Inside of this test project that we're currently working on and eventually we're going to move all this 20 00:01:19,220 --> 00:01:25,310 logic over to our common module so we can use it across all of our different services let's first begin 21 00:01:25,310 --> 00:01:30,170 by getting a better idea of exactly how we're going to make this stuff more usable in nature. 22 00:01:30,170 --> 00:01:33,460 So we're going to first begin on the listener side of things. 23 00:01:33,630 --> 00:01:35,870 We're going to create a class called listener. 24 00:01:35,870 --> 00:01:38,210 This is going to be an abstract class. 25 00:01:38,210 --> 00:01:41,320 Remember abstract classes are not meant to be used directly. 26 00:01:41,330 --> 00:01:44,290 Instead we're going to subclass them in order to use them. 27 00:01:44,330 --> 00:01:46,280 I'll show you more on what that means in just a moment. 28 00:01:46,280 --> 00:01:50,620 Right now let's just take a look at the properties are going to define inside this class. 29 00:01:50,900 --> 00:01:58,330 The entire goal the class is to essentially automate as much of all this stuff as possible so inside 30 00:01:58,330 --> 00:02:00,810 this class we're going to have a property called subject. 31 00:02:00,940 --> 00:02:05,120 There's gonna be a string that's gonna be the name of the channel that we're going to listen to. 32 00:02:05,170 --> 00:02:06,890 We're going to have an on message function. 33 00:02:06,910 --> 00:02:12,450 This is where we're going to actually run our business logic or implement the business logic we're going 34 00:02:12,450 --> 00:02:17,880 to make sure that our class listener receives a stay on client to use to listen for incoming events 35 00:02:17,880 --> 00:02:18,910 and whatnot. 36 00:02:19,090 --> 00:02:21,830 We're gonna make sure this thing has a Q group name. 37 00:02:21,990 --> 00:02:24,930 We're going to make sure that it has an equate property. 38 00:02:24,990 --> 00:02:30,210 We discussed the goal of this manual act mode right here and as we saw there was that automatic 30 second 39 00:02:30,210 --> 00:02:35,270 time out but it turns out that when we create the subscription we can also customize that 30 seconds. 40 00:02:35,460 --> 00:02:41,200 So we might for some listeners want to customize that equate down to maybe five seconds or so. 41 00:02:41,220 --> 00:02:45,720 So we'll make sure that we add in some properties just to allow ourselves to customize that window of 42 00:02:45,720 --> 00:02:51,860 how long we have to acknowledge an incoming message we'll make a method that's going to make it a lot 43 00:02:51,890 --> 00:02:54,250 easier to create these options right here. 44 00:02:54,260 --> 00:02:56,490 So essentially just wrap up those lines of code. 45 00:02:56,600 --> 00:03:00,230 We'll have a listen method which is where we're going to actually set up our subscription. 46 00:03:00,230 --> 00:03:04,610 And then finally a helper method that will call pass message that is going to take a look at the incoming 47 00:03:04,610 --> 00:03:05,390 message. 48 00:03:05,480 --> 00:03:08,590 Figure out what kind of data is inside there and pass it in some way. 49 00:03:08,600 --> 00:03:14,720 So essentially like this right here more or less where we pulled out the data and then we figured out 50 00:03:14,720 --> 00:03:17,030 whether or not that data was a string or a buffer. 51 00:03:17,120 --> 00:03:24,690 And eventually pass that data into actual object from the string or the Jason that is some very purposefully 52 00:03:24,690 --> 00:03:26,970 just going through these properties very quickly right now. 53 00:03:27,000 --> 00:03:32,090 Because once we start writing some code they will all make a lot more sense lasting on mentioned is 54 00:03:32,120 --> 00:03:37,910 what we really mean by abstract class and why these two properties are labeled as abstract. 55 00:03:37,910 --> 00:03:41,800 So the real idea here is that we're not going to use class of listener directly. 56 00:03:42,140 --> 00:03:48,090 Instead we are going to subclass this listener into things like ticket create a listener and order updated 57 00:03:48,110 --> 00:03:52,220 listener and then inside these subclasses we're going to put together some different options to make 58 00:03:52,220 --> 00:03:59,000 sure that it listens specifically for say a ticket created event or in order updated event. 59 00:03:59,060 --> 00:04:03,800 So in other words we're gonna customize the subject we're listening to the subclass and we're also going 60 00:04:03,800 --> 00:04:09,530 to customize the on message method inside the subclass as well to make sure that when we receive some 61 00:04:09,530 --> 00:04:15,740 event inside of a ticket created versus order updated we can do some business logic or do some appropriate 62 00:04:15,740 --> 00:04:19,750 processing for a ticket created event or in order updated event. 63 00:04:19,790 --> 00:04:21,500 So that's the general idea. 64 00:04:21,510 --> 00:04:25,340 So right now really just going to focus on creating class listener will then take a look at how we can 65 00:04:25,340 --> 00:04:29,080 somehow subclass it into something that's more usable. 66 00:04:29,080 --> 00:04:29,440 OK. 67 00:04:29,450 --> 00:04:30,280 Quick pause right here. 68 00:04:30,290 --> 00:04:32,120 Let's start putting the thing together in just a moment.