BrandGhost

Listen Up! - Quartz NET Listeners Explained

Hey! Did you hear that? Listen up! After this tutorial, you'll be able to listen more effectively to the jobs and triggers you use when scheduling in Quartz NET. Enhanced observability and hook-up points, for the win! Quartz NET provided three major types of listeners: - Jobs - Triggers - Schedulers And each one has different capabilities. Wait until you see what the scheduler listener can do... Holy!
View Transcript
courts.net is a powerful job scheduling framework that we can leverage in our C applications but there's some more fancy things that we can do with it aside from just scheduling jobs hi my name is Nick centino and I'm a principal software engineering manager at Microsoft in previous videos which I'll link above if you haven't watched them yet I described how we can go create and schedule jobs in courts.net pass information around and all sorts of cool stuff like that in this video we're going to look at extending some other things in this particular case it's going to be listeners inside of courts.net if that sounds interesting remember to subscribe subsribe to the channel and check out that pin comment for my courses on dome train now let's go jump over to visual studio and check out some different listeners so the application that I have on the screen right now inside of visual studio is a very basic aspet core app if you haven't watched the other video like I said please go check that out because I walk through how we can go set up quartz jobs with triggers so you can see that from line 42 to 58 on my screen right now and then from there we end up using a scheduler and then we schedule jobs on that scheduler as you may guess in this video though we're going to look at three different types of listeners that we can use in chs.net and they're going to have three different interfaces so there's the I job listener the I trigger listener and then the I schedule listener as well I'm going to go through different implementations of them and we're going to walk through what we can do but I do want to explain what we're going to do in this case in this current setup to be able to work with them what I'm going to do is highlight code from line 64 to 72 here and this is going to be what we're going to leverage from the dependency injection framework to get required services and then add those on to the scheduler inside of courts.net to explain what's going on here these required Services that's just using dependency injection you can see up at the Top Line 30 and 31 here I'm just registering on these custom listeners and that means when we go back down to this code we can pull them off and add them onto the schedu itself so there's a listener manager that we're going to be leveraging in order to do that you might be saying well what the heck do these things do that's a great question so I'm going to go jump to their implementations and they're pretty simple so if I scroll down a little bit lower here we can see the custom job listener so that's going to be this code on the screen right now and there's a whole bunch of stuff that we can do but in this case what I'm doing is just hooking up some console right lines that way when we go to run this application we can see where this thing actually hooks up before we continue on this is just a quick reminder that I do have courses available on dome train if you're just getting started in your programming journey and you want to learn C you can head over to dome train I have a getting started in C course it's approximately 5 hours of content taking you from absolutely no experience to being able to program in C and after that I have my deep dive course which will take you to the next level with another 6 hours of content so that you can start building basic applications head over to D train and check it out let's head back to the video the job listener is just something that's going to allow us to get access to different points in time when a job is being scheduled we can see when a job execution is going to be vetoed so that will be something that is able to intercept and tell the scheduler hey don't go run this thing so we can see when that happens in here job to be executed gets run before the job is executed and job was executed as you might guess is when the job is finished executing I might as well jump over to the trigger listener as well so we can see the implementation of both of these things before we go run it we have the I trigger listener interface and again we're going to look through all the methods that we see inside of here I forgot to mention in the previous one there is a name that you can set as well but if we walk through the different method calls that we have here we can see the trigger complete so this is going to happen when the quartz trigger has finished running the trigger fired is when it is fired obviously misfired is a state that you can look at in courts when the jobs aren't firing at the right time so it's missed its firing opportunity and then this last one is an interesting one because I mentioned the veto one in the previous listener but this veto job execution allows the trigger to indicate whether or not it should be vetoed so the return type here you can see is Boolean if we return false we're saying we are not vetoing the execution but we do have this opportunity to return a true as well and that tells the scheduler hey look don't go run this thing at all that means we're able to go back up to the job listener and on this vetoed one this would execute if we did return a true from that other listener now these are two listeners we can hook up and I did mention that there's also this custom scheduler listener as well so this one here I didn't want to go wire it up in this demo because uh if I keep scrolling look how many different things are to implement you can go do this I didn't want to go do this and try to have a listener for everything it just felt like Overkill but I wanted to show you just how much control in terms of being able to observe the scheduler that you have so I hope just by scrolling through this I'll do it one more time so you can see right there are tons of different methods that you can basically go write some hookup code and be able to uh log things do whatever you want right so there's literally hundreds of lines here plenty of different hookup points so anyway I just wanted to illustrate and put that on the screen for you so you can see all of these different things but that is the I scheduler listener you can go ahead and put that onto your dependency container as well if you want to implement this and if that's the case you would do this similar type of thing if I scroll all the way back up here you'd want to go add that onto your list listen manager like I said I'm not doing that we're only going to go look at these two listeners but I think at this point in time we can safely go run this and then we can go look for our console right lines so let's go ahead and press play and see what's up okay and if we jump over to the console we can see that we get the trigger fired let me try to move this all over and we can see this is going to be the trigger fired one right here trigger fired and then we print out the key right so that's the next line The veto job execution is going to be this part here and we are returning false right so if we see more stuff printed after that that's because the job execution was allowed to go that means we went from this trigger fired to veto job execution which is a decision we can make with our listener I'm going to scroll back up now because we go to the job one so this is job to be executed that means on line 325 down to 332 that code is now getting executed and what we see a little bit below that um right here this part is the actual job running so I didn't show you that in this video I did mention it in the previous videos but these two lines here this comes from the actual job execution itself not the listeners now we go down to here line 345 job was executed that's right here and we also put the job key so that's why you see that second line and then finally we go all the way down to here line 360 to 369 and that's going to be the trigger complete this code demonstrates that we were able to have these hookup points where we can go run our own logic in this case I'm just writing to the console maybe you want to have Telemetry maybe you want to have some other state manag in your application but you have that opportunity with these listeners what I wanted to show as well is that there is this opportunity for us to be able to go return a true to go veto this job execution so I'm changing line 397 to to 398 now it's going to return a true value and this should veto so when the scheduler asks our listener hey do you want to veto this thing we say absolutely we're going to veto it and we shouldn't see the other pieces execute after so let's go run it and see it okay with the console output here we can see the trigger fired just like before the veto job execution gets to run so that's line 390 to 3.99 so as we expected we return true and that means the job execution vetoed method that's all the way back up here we can see that this now gets run because this was vetoed so once that's vetoed we don't see anything else from the trigger we don't see anything else from the job and you can see that we didn't go run the job's body where it was writing out to the console so it truly did veto the job's execution so leveraging these listeners like I've said a few times now you have all of these opportunities to have all of these hookup points run your own logic or have better observability again we have this custom scheduler listener that has just an outrageous number of methods that you can go check out so you have lots of flexibility and lots of opportunities for extension there are some gchas with this kind of stuff because it's not just as straightforward as writing any code that you want in here even though I was getting away with some simple console right lines so if you want to learn more about some best practices with listeners you can go ahead and check out this video next when it's uploaded thanks and I'll see you next time

Frequently Asked Questions

What are the different types of listeners in Quartz.NET?

In Quartz.NET, there are three main types of listeners that I discussed: the IJobListener, the ITriggerListener, and the ISchedulerListener. Each of these interfaces allows you to hook into different points in the job scheduling process.

How can I implement a listener in my Quartz.NET application?

To implement a listener in your Quartz.NET application, you would typically create a class that implements one of the listener interfaces, like IJobListener or ITriggerListener. Then, you can register this listener with the scheduler using dependency injection, as I demonstrated in the video.

What happens when a job execution is vetoed using a listener?

When a job execution is vetoed using a listener, the scheduler will not execute the job. I showed this in the video by returning true from the veto method, which prevented the job from running and allowed us to see how the listener can control the execution flow.

These FAQs were generated by AI from the video transcript.
An error has occurred. This application may no longer respond until reloaded. Reload