Cloud 5 - Sling Job Scheduler
Last update: Thu Dec 05 2024 00:00:00 GMT+0000 (Coordinated Universal Time)
CREATED FOR:
- Intermediate
- Admin
- Developer
Job Scheduling and consumption is different from a non-clustered AEM deployment. This video shows some gotchas and things to look out for when running these tasks.
Transcript
Hey there, Darren. I was thinking of doing an episode on job scheduling with AEM as a cloud service because there’s some specific nuances the audience here might like to know about. What do you think? I think it’s a good idea. But first I want to check my schedule. I created this digital assistant. His name is Lemmy. Let’s see what he has to say. Hey, Lemmy. How’s my schedule looking today? Darren, I think your schedule is free. Why do you keep asking me? Do you have a problem with Lemons? Whoa, whoa, whoa. I don’t have any problem with Lemons. Good. I wouldn’t want to have to send my friend Johnny Banana to have a word with you. Darren, did you program Lemmy with some anger issues? I’m really not sure I want to meet his friend Johnny Banana at this point. Let’s get back to cloud service if you don’t mind. Yeah, so Darren, let’s talk a little bit about scheduled jobs and how they work in AEM as a cloud service. Now, as you know, right, the code must always know that it’s running inside of a cluster, which is just the way AEM as a cloud service is. We can think of it as similar to the old on-prem cluster aware interface, which we don’t use anymore in AEM as a cloud service because there’s always more than one instance running. And remember, you have to write your code so that it must be prepared that an instance might be stopped at any point in time. This seems like a pretty good use case for Sling jobs and the Sling scheduled jobs. Absolutely, right? Those can be a little bit different in AEM as a cloud service. So remember, when you’re writing your jobs, the goal is to ensure guaranteed execution so the developer needs to control how the job is actually handled inside of a cluster, which as we talked about is all the time in AEM as a cloud service. Now, in order to do this, you actually need three things, and we’re going to look at some examples here. But first you need a class that schedules your job. You need a class that consumes your job topic, and then you need an OSGI config for that job topic. Does that make sense? Makes sense. Let’s check out some code. Yeah, great idea. Let’s first of all look at the actual OSGI component code, and here is a class that you see here that actually schedules the job. Again, very straightforward, but you can see that the code checks if a scheduled job exists using the get scheduled jobs method of the job manager class. In addition, you can see that it removes a scheduler based on the job topic. Darren, anything else you want to point out in this code? I think it’s pretty self-explanatory. Just taking a quick look at it, I mean, you can see the cron expression itself, so you can see how to schedule the job. I mean, if the job doesn’t need a cron scheduler, if it’s a one-time operation, obviously it wouldn’t have a cron scheduler. But the methods themselves seem to be pretty self-explanatory with the key being that start scheduled job method in that one, in this particular case, is the one that contacts the job manager and does the create and schedules it based on cron. So pretty straightforward, I think. Yeah, yeah, I think that’s pretty straightforward. Now, if we look at the job consumer here, you’ll see again, we have a component here and we have the job consumer. And in this case, we simply have the method here that will actually do the work when that is actually called. So you can see here that we’re using the job consumer again, just in a very simple example way. But feel free to use this in your own more complex code as well. Darren, anything else you want to point out about the consumer? I mean, this one can’t get any more simple. So I mean, it literally is the job topic and the process method. But the key being here, and this is something you shouldn’t, I couldn’t emphasize more, is the resilience factor of your whatever work you’re doing in that process method. So you’ve got to understand that the instance may go down at any particular time. So you have to be either resumable or fault tolerant. And the OSGI configuration that we’ve showed earlier kind of encapsulates some of those configurable items. Absolutely. So yeah, so this is just a quick overview of how to write jobs, because it is a little bit different in AM as a cloud service because your code is always running in a cluster. But this will give you some great ways to write your code effectively.
Content covered in this video
- Overview of jobs in the cloud
- Job Consumer and scheduler code examples
Additional Resources
Watch related videos on the Cloud 5 season 2 page.
recommendation-more-help
4859a77c-7971-4ac9-8f5c-4260823c6f69