Cloud 5 - Sling Job Scheduler

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, Darin - I was thinking of doing an episode on job scheduling with the AEM as a cloud service - because there are 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 is Lemmy. - Let’s see what he has to say. Hey, Lemmy. How’s my schedule looking today? Darin, I think your schedule is free. Why do you keep asking me? Do you have a problem with lemons? 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. Darin, 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 let’s get back to cloud service, if you don’t mind.
Yeah. So, Darin, 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. 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 schedule 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. Darin, 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 schedule, if it’s a one time operation, - obviously it wouldn’t have cron scheduler. But the methods themselves - seem to be pretty self-explanatory with the key - being that start schedule job method in that one in this particular case - is the one that contacts the job manager and does the create and schedules - that 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 the 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 your feel free to use this in your own - more complex code as well.
Darin, 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 should emphasize more, is the resilience factor of your whatever work - you’re doing in that process method. So you got to understand that the instance - may go down at any particular time. So you have to be either - resumable or fault tolerant in the OSGi configuration - that we showed earlier, kind of encapsulate - some of those configurable items. So just some things to think about here.
Absolutely. So, yeah, so this is just a quick overview - of how to write jobs because it is a little bit - different in AEM 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
recommendation-more-help
4859a77c-7971-4ac9-8f5c-4260823c6f69