Parameterized Jobs and Dependency Injection With Quartz.NET
July 29, 2024
• 2,173 views
Scheduled jobs in Quartz NET are boring. What can you do without any dependencies or parameters?!
Don't worry. We can spice things up.
In this video, we'll look at:
- THREE ways to pass parameters into jobs
- How to leverage built-in dependency injection for dependent services
- Moar Quartz awesomeness!
Let's dive in and see what options are available to us in our ASP NET Core apps!
View Transcript
sports.net is a really powerful job scheduling framework that we have access to in the net ecosystem but how do we go about parameterizing the jobs that we can go create hi my name is Nick centino and I'm a principal software engineering manager at Microsoft in this video I'm going to walk through several different ways that we can pass data into jobs including with dependency injection now if you haven't seen my previous videos on quartz.net I'll put a link up here you can go check that out to walk you through the basics of getting everything set up inside of an asp onet core application with SQL light and you can go change that to different providers if you don't want sqlite in this video we'll look at a handful of different ways that we can pass data into the jobs that we create and I'll finish
it all off with dependency injection that sounds interesting remember to subscribe to the channel and check out my pin comment for those courses on dome train with that said let's jump over to visual studio and check out quartz okay so the job that I've created is the same one that I'm going to have used in the previous videos it's a very simple one it just says hello world what happens if we want to start passing data into this job so that it's not just printing hello world with this hardcoded string right here what if we wanted this to be configurable the first way that we're going to look at is passing things in on the job data context the way that this looks is that we can ask the context of this job execution context uptop we'll ask the job detail to get the job
data map and then we're going to get the parameter that we want I'm just calling it message in this case but you'll see context job detail job data map and then getting the key so that we can ask for the value right so message from job detail I can go ahead and pass that in here let's do it this way though I'm going to make this so that we have a string interpolation here co-pilot maybe no okay job detail we'll do this and then I'm going to pass it in thanks co-pilot for saving the day at the end there we'll print this out I'm still going to use a completed task here none of this is done asynchronously it's all uh Sync API calls but now that we have this we actually have to pass the data in right this is just going to be
pulling the data out but we haven't put anything into it so we scroll up a little bit higher we can see that when we're creating this job detail this is a spot that we can pass in something to the job data so if I go ahead and uncomment this by the way you might notice in my videos I hide things in comments so if you see little comments littered around it's probably a spoiler alert for something cool coming later in this case it's going to be passing data into these different things so on the job detail we can set job data with this job data map sort of like a dictionary and then we can pass this information in before we move on this is just a quick reminder that I do have a course on C refactoring available on dome train refactoring is one
of the most critical skills that you can learn as a software engineer and this helps you continue to build upon applications that already exist making sure that they can scale and have extensibility I walk you through a bunch of various techniques and give you some examples that we walk through together to see how we can apply these techniques to refactor the code check out the pin comment and the links in the description to get this course now back to the video when we go schedule this right below we pass in the job detail and the job trigger this job detail is configured to have this message that means with this key right this is the same one we use all the way down here where we're getting that message and just to kind of link it all together we were setting the job detail job
data map message not something else pay attention because this is going to be important as we go forward if we go run this we should be able to see this thing get printed to the console and I'll pull it up here there we go job detail message on job detail right so this is the little prefix I hardcoded and this is the parameter coming from the job detail now there's another spot very much like the job detail and that's going to be the job trigger so you can do a very similar thing where we're going to use this job data I'm going to put it right here we can combine both of these I'll set job data still on the detail I will set job data on the trigger as well and that way when we come down here we can go add this as
well to the console very similarly right we'll ask the context for the trigger then we're going to get the job data map and then get the message off of that as well you can use different keys for these things I'm just keeping it consistent with message and then I'm going to write that out to the console as well make sure not to have a copy paste mistake and then I will say job trigger here so now we can go run both of these together if I pull the console up here we should see that we get two console right lines there we go job detail and Trigger as well right so there's two different ways that we can pass data in again the trigger is separate from the job detail these things are decoupled in quartz so depending on where you want to align your
data you can still pass data into the job when it's running from both of these different things now there's one more spot before jumping over to dependency in that we can get more parameterized job options so I'm going to go look all the way back up at the top here when we're setting things up when we're getting access to our scheduler off of the app and asking for it from a required service collection here what we can do is set some context information so on the scheduler there is a context and we can go ahead and add sort of another key value pair just like we saw in the other scenario and that means that we can go pull this information off of the scheduler context so scheduler detail and Trigger are all things that can pass data into the running job you see what
this looks like very similar pattern right off of the context we'll ask for the scheduler then we're going to ask for the Schuler's context and then we'll ask for the message and maybe co-pilot will autocomplete for us thank you very much co-pilot if we go run this now we should get three console right lines with three different strings and there is the console running and there we go three different ones job detail trigger and schedule right so three different ways for different use cases of passing data in keep in mind that theer is going to be able to have that context available for every job and Trigger that are done through that schedule right so you want to think about it kind of like a hierarchy for who has access to what so if you want all of the triggers and jobs that are done
through that scheduler to have access to things on this context that's where you're going to want to put that information we've seen three different ways to pass things into here but what about dependency in this is one final thing that I want to look at for this example so if we look at our test job it's not taking in any dependencies right there's no services or anything fancy being passed in there's just parameters that we're having from either these three things that we just saw what I'm going to do is add a primary Constructor here it's going to pass in a dependency called dependency so I'm not very creative but this is going to be the thing that we're passing in I have it hidden again in plain sight and here it is it's not not very exciting but it's just going to be something
that calls console right line and before we run it it's going to say this is the dependency so just to keep the theme of everything going here that means if I take this dependency and replace the console right line what we're doing is we're using a p in little dependency this service that's passed in for us this will get done through dependency injection and if I scroll back up we can thank all of this setup here off of the asp.net core web app using this pattern because we're going to be able to use the I service provider that's available to our application so as long as we register the service onto our dependency collection we should be able to access it we haven't done that yet so there's still one more step if I go ahead and take this uncomment it right before we go
build the application We'll add a Singleton for our dependency now we should be able to resolve it which means if we go all the way down here when this test job has to get constructed by Quartz it will know that it can ask the dependency container for this dependency and that means we should be able to access it in here not only will we have data from three different spots we'll also have a dependency passed in for us through the dependency injection framework let's go ahead and run this and see how it works here's the console and we should see a bunch of noise right so every time it writes to the console says this is the dependency so just sort of proving that we had this extra thing past in through the dependency injection framework along with the three different types of parameters as
we can see quartz is a really powerful and flexible job scheduling framework and I'm personally using it in my own side projects as I'm building up my service brand ghost I'm able to leverage Courts for different things related to social media posts and different types of interactions so I'm leveraging it that way to create a Content schedule or a Cadence for posting social media content and there's so much that you can do with it I'm sort of just scratching the surface as I start to build out brand ghost with it now there are competitor Frameworks like hangfire that allow you to do job scheduling I personally haven't gone and explored those yet because quartz does everything and more that I need for brand ghost so you might want to check those out if you're looking for more capabilities that quartz does not have I hope
you found this useful thanks so much for watching and I'll see you next time
Frequently Asked Questions
What is Quartz.NET and how can it be used for job scheduling?
Quartz.NET is a powerful job scheduling framework in the .NET ecosystem that allows developers to schedule and execute jobs at specified intervals. In my video, I walk through how to set it up in an ASP.NET Core application and demonstrate various ways to pass data into jobs.
How can I pass parameters into jobs using Quartz.NET?
In the video, I show several methods for passing parameters into jobs, including using the job data context, job detail, job trigger, and scheduler context. Each of these allows you to configure jobs with different data points, ensuring flexibility in how jobs are executed.
What is dependency injection and how does it work with Quartz.NET?
Dependency injection is a design pattern that allows us to pass dependencies into classes rather than hardcoding them. In the context of Quartz.NET, I demonstrate how to set up a job that takes a dependency through the ASP.NET Core service provider, allowing for better management and testing of job behavior.
These FAQs were generated by AI from the video transcript.