1 00:00:01,220 --> 00:00:05,570 As you're just discussing inside of our order created listener right after we save our ticket we're 2 00:00:05,570 --> 00:00:10,220 going to want to publish an event saying that this ticket was just updated that is going to allow our 3 00:00:10,220 --> 00:00:16,730 different replicated services or services that have replicated data to stay in sync now to put this 4 00:00:16,760 --> 00:00:18,170 update together. 5 00:00:18,200 --> 00:00:22,820 I first want to give you a quick reminder on how we publish an event in general as we had spent a little 6 00:00:22,820 --> 00:00:26,390 bit of time since we have published an event inside of our ticket service. 7 00:00:26,390 --> 00:00:32,530 You might recall that we have a publishers directory inside there is the ticket updated publisher. 8 00:00:32,580 --> 00:00:37,830 So this is the classic we used to publish an event saying that a ticket has been updated to make use 9 00:00:37,830 --> 00:00:38,250 of this. 10 00:00:38,280 --> 00:00:44,840 We create an instance of the publisher we pass in a Nats client. 11 00:00:44,980 --> 00:00:51,360 So that might be something like Nats report clients and we'll call the publish function and is publish 12 00:00:51,360 --> 00:00:55,450 function is going to have all the properties that are going to be contained inside the event. 13 00:00:55,490 --> 00:01:01,070 Now unfortunately our ticket updated event definition is just a little bit out of date. 14 00:01:01,220 --> 00:01:06,230 In particular you'll notice that we are saying that this event is going to have an I.D. a version title 15 00:01:06,260 --> 00:01:14,540 price and user I.D. but our ticket also now has an order I.D. property tied to it as well this event 16 00:01:14,570 --> 00:01:16,970 object right here does not yet support that order. 17 00:01:16,970 --> 00:01:22,610 I.D. property chances are that whenever we update a ticket we're going to want to tell every other service 18 00:01:22,610 --> 00:01:24,540 whether or not this ticket is reserved. 19 00:01:24,620 --> 00:01:27,620 And we would do that by sharing that order I.D. property. 20 00:01:27,620 --> 00:01:32,330 So we should probably go back over to our common module very quickly and make sure that we add in to 21 00:01:32,330 --> 00:01:36,220 this event definition or the definition for ticket updated. 22 00:01:36,310 --> 00:01:40,230 You say that the event is going to have an order I.D. property as well. 23 00:01:40,250 --> 00:01:45,920 But before we do so there's one other very quick thing I want to mention I'm going to go back over to 24 00:01:45,920 --> 00:01:48,490 the order created listener at the top. 25 00:01:48,520 --> 00:01:58,630 I'm going to import the publisher that we're just taking a look at that we'll get the ticket updated. 26 00:01:58,720 --> 00:02:08,650 Publisher from up one directory publishers ticket update publisher and then very quickly I want to imagine 27 00:02:08,680 --> 00:02:12,040 how we're going to write this out right after that ticket not safe. 28 00:02:12,280 --> 00:02:17,350 Well just as we saw on that other file we would probably write out something that looks like new ticket 29 00:02:17,500 --> 00:02:23,470 updated publisher and then we would want to pass in our Nats client right here. 30 00:02:23,510 --> 00:02:26,870 Now there are two different ways that we can provide a Nats client. 31 00:02:26,900 --> 00:02:31,970 One is going to be very easy and direct the others can be a little bit harder to understand but ultimately 32 00:02:32,030 --> 00:02:33,920 a lot easier. 33 00:02:33,920 --> 00:02:38,720 So in order to provide a Nats client right here the easy solution that will work right away without 34 00:02:38,720 --> 00:02:49,970 any changes would be to import our Nats rapper from up to directories Nats rapper and then we could 35 00:02:49,970 --> 00:02:51,170 provide that to the publisher 36 00:02:54,030 --> 00:02:55,230 like so easy enough. 37 00:02:55,230 --> 00:02:56,490 No problem whatsoever. 38 00:02:56,520 --> 00:02:57,470 Right. 39 00:02:57,570 --> 00:02:59,690 Well there is a downside to this approach. 40 00:02:59,700 --> 00:03:05,790 The downside is that we are adding in yet another tie between two different files inside of our projects. 41 00:03:05,850 --> 00:03:10,700 So we are importing the Nats wrapper directly into order created listener. 42 00:03:10,890 --> 00:03:13,160 Normally that is unavoidable at some point time. 43 00:03:13,170 --> 00:03:15,150 We just have to tie different files together. 44 00:03:15,210 --> 00:03:15,870 And that's life. 45 00:03:15,870 --> 00:03:17,910 That's just how stuff works. 46 00:03:17,970 --> 00:03:23,460 However because you have already seen that testing a lot of these listeners revolves around somehow 47 00:03:23,490 --> 00:03:25,630 mocking this Nats rapper right here. 48 00:03:25,650 --> 00:03:30,390 If we had in a direct report for NATS rapper that might make testing a little bit more challenging down 49 00:03:30,390 --> 00:03:31,710 the line. 50 00:03:31,710 --> 00:03:36,420 So it turns out that there actually is a slightly easier way to approach this. 51 00:03:36,480 --> 00:03:40,690 It is just going to involve making one other very quick change inside of our common module. 52 00:03:40,830 --> 00:03:44,550 So let's take a look at a quick diagram and understand what the other option is. 53 00:03:46,700 --> 00:03:49,210 OK so this is what we are doing right now. 54 00:03:49,220 --> 00:03:50,960 We've got our order created listener. 55 00:03:50,960 --> 00:03:56,330 It is trying to create an instance of ticket updated publisher and we are doing so by importing the 56 00:03:56,330 --> 00:03:59,190 Nats rapper into the order created a listener. 57 00:03:59,210 --> 00:04:02,630 We're going to take that things client and then pass it off to the ticket. 58 00:04:02,630 --> 00:04:09,000 Updated publisher but there's something else I want you to recall the order created listener is a subclass 59 00:04:09,120 --> 00:04:11,490 of the listener base class. 60 00:04:11,520 --> 00:04:14,040 Let's go take a look at that listener base class definition. 61 00:04:14,040 --> 00:04:19,390 Very quickly we're going to see something really interesting our listener base class is defined inside 62 00:04:19,390 --> 00:04:26,220 of our common module so I'll go to my common module s our RC events and then find base listener T.S. 63 00:04:27,700 --> 00:04:28,340 inside of here. 64 00:04:28,360 --> 00:04:34,360 You might recall that whenever we create an instance of a listener or anything that extends a listener 65 00:04:34,360 --> 00:04:38,950 for that matter we are required to provide a Nats client. 66 00:04:38,950 --> 00:04:42,160 That's what this constructor function is saying right here. 67 00:04:42,160 --> 00:04:47,040 We then assign that to the distant client property and we had marked this stock client or the client 68 00:04:47,050 --> 00:04:54,980 property as private which means that it cannot be accessed by any other class even a subclass so technically 69 00:04:55,310 --> 00:04:56,520 our order created listener. 70 00:04:56,540 --> 00:04:58,480 That is what we are working on right now. 71 00:04:58,520 --> 00:05:01,530 It is a subclass or a child class of the listener. 72 00:05:02,030 --> 00:05:07,340 So inside of order create a listener they're technically already is a Nats client kind of wrapped up 73 00:05:07,340 --> 00:05:13,190 into this thing but unfortunately we had marked it as a private property which means that this child 74 00:05:13,190 --> 00:05:17,530 class cannot access that client property diagram format. 75 00:05:17,530 --> 00:05:20,290 This is what it looks like we've got our ordinary listener. 76 00:05:20,290 --> 00:05:26,050 It is extending the listener listener definitely already has a initialized Nats client is there ready 77 00:05:26,050 --> 00:05:27,420 to be taken. 78 00:05:27,490 --> 00:05:31,720 So if we could somehow get a handle on that thing we could very easily pass it through to the ticket 79 00:05:31,750 --> 00:05:36,920 update a publisher without having to worry about that Nats wrapper class or anything like that. 80 00:05:36,940 --> 00:05:41,170 The only reason we cannot do this right now is because we had marked that client property as private 81 00:05:41,320 --> 00:05:46,930 and again if a client or to if a property is marked as private we can not access it from anywhere outside 82 00:05:46,930 --> 00:05:50,200 the class even in a child class. 83 00:05:50,290 --> 00:05:55,090 So to fix this inside of our common module we could just update the modifier on that property. 84 00:05:55,090 --> 00:05:59,550 We could instead market as protected if we market as protected. 85 00:05:59,600 --> 00:06:05,060 That means that the child class of ordinary listener can access that property it can access the client 86 00:06:05,270 --> 00:06:07,520 take the client and pass it off to the ticket. 87 00:06:07,520 --> 00:06:13,430 Updated publisher and so this would be a way preferable solution because it's gonna make testing easier 88 00:06:13,430 --> 00:06:14,280 down the line. 89 00:06:14,280 --> 00:06:18,800 And it's also going to mean that we've got one less direct tie between two different files inside of 90 00:06:18,800 --> 00:06:21,440 a project which again is usually a pretty good thing. 91 00:06:23,220 --> 00:06:23,470 OK. 92 00:06:23,500 --> 00:06:27,790 So at this point what we've really established inside of this video is that we really need to go over 93 00:06:27,790 --> 00:06:30,740 to our common module and make two changes. 94 00:06:30,820 --> 00:06:35,730 The first change we need to make is a change to our ticket updated events. 95 00:06:35,830 --> 00:06:41,670 We need to make sure that the ticket update event also communicates an order ideas property and then 96 00:06:41,670 --> 00:06:44,090 we've also established that our base listener. 97 00:06:44,220 --> 00:06:50,320 We need to mark that client as a protected property instead of private now that we've established what 98 00:06:50,320 --> 00:06:50,980 we have to do. 99 00:06:50,980 --> 00:06:51,970 Quick pause right here. 100 00:06:51,970 --> 00:06:53,680 Make those two changes in the next video.