We've got a rough start to our ticket created listener put together.
We're not going to start to pivot and work on the ticket update a listener just you know we are not
by any means done with this code right now.
But as I mentioned at the start of this section I really want to focus on some of those different concurrency
issues now that we have both publishers and listeners inside of rap.
So we're gonna work on the listener for ticket updated we're gonna see that there's very quickly some
really big concurrency issues and we're going to come back through both these listeners and make some
adjustments to them.
So inside my listeners directory inside my order service I'll make a new file of tickets updated.
Listener Scott T S and then inside of here we're going to add some very similar imports to what we've
got up here at the very top so inside of ticket updated we will add in and import for message from node
that streaming will get subjects listener and ticket updated event from our common module
I'm going to get tickets from up to directories models.
Ticket.
And then finally our Q Group name from you.
Group Name Then comes our actual listener definition.
So export class tickets.
Updated listener.
This is going to be extending our listener abstract class and the event that we are trying to listen
for is ticket updated though ticket updated event then inside of here we'll go through all that hassle
with subject again.
So put in subject as subject stop ticket updated equal to subjects start ticket.
Updated then our Q Group name
and then finally async on message they're gonna receive some data that's gonna be a type ticket updated
event at data and as the second argument we'll get our message of type message well now inside of here.
Well pretty straightforward.
In theory we need to find the ticket that is being updated the ticket that is mentioned inside of this
data object and just update the data and save it.
That's pretty much it.
So at the end the day in theory it's gonna look kind of similar to what we just did inside of ticket
created.
The only differences this time around we are updating data instead so step one let's pull the ticket
out of our ticket collection using that ticket model will say ticket is a weight ticket that find by
Idi.
The idea is going to be contained inside this data argument as I.D. so we'll say Ada dot I.D. There
is a chance that we will not find a ticket we are looking for.
So if we don't find the ticket we are looking for take a love a value of No.
So very similar to what I've done on some of our root handlers in the past will say if there is no ticket
right now we're just gonna throw a very simple error.
So we'll say throw new error tickets not found.
We will come back and address error handling in a way greater detail very shortly.
But remember right now we're just trying to focus on some of this async issue stuff now that we found
the ticket we can go through and update presumably the title and the price.
Remember that's the only properties we really care about here.
So on the ticket we are going to set the title and the price.
Let's make sure we pull off those properties from ideas or from data as well.
So let's say concert title and price is coming from data that's better.
After setting those properties will then save the updated ticket.
So a weight ticket dot save.
Well that is pretty much it for our business logic.
And so after we've executed all of our business logic again that we want to call our message dot ACH
function that's going to acknowledge the message until that streaming server that we have successfully
processed it.
Little called message dot EQ at the bottom like so and well once again that's pretty much it kind of
a simple on message implementation so let's save this right here.
The last thing to do is make sure that these two listeners actually get used at some point time.
Right now we have added in these two files these two listeners but we don't actually make use of these
classes anywhere inside of our project.
So we need to figure out a suitable location to create an instance of these listeners and have them
start listening for events.