Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn More

View all

Sign in to view all badges

How can I create a background job in AEM as a Cloud?

Avatar

Avatar
Level 1
watopin
Level 1

Likes

0 likes

Total Posts

1 post

Correct Reply

0 solutions
View profile

Avatar
Level 1
watopin
Level 1

Likes

0 likes

Total Posts

1 post

Correct Reply

0 solutions
View profile
watopin
Level 1

19-03-2021

I tried 2 ways to do a periodical background job.
 

1:sling job. This was executed on both local SDK and AEM cloud, but executed many times when the specified time come.

2:commons scheduler. This is good on a local SDK, but this did nothing on AEM cloud environment.

 

So I'm stuck.

Either way, How can I do a precise background job?

 

Here is my sample code1 : sling jobs way(service and consumer).executed many times when the specified time come.

 

import org.apache.sling.event.jobs.JobManager;
import org.apache.sling.event.jobs.JobBuilder.ScheduleBuilder;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component
public class MyJobService {
    private static final Logger logger = LoggerFactory.getLogger(MyJobService.class);
    
    @reference
    private JobManager jobManager;

    public static final String JOB_TOPIC = "my/sample/jobtopic";
    @activate
    public void startScheduledJob() {
        ScheduleBuilder scheduleBuilder = jobManager.createJob(JOB_TOPIC).schedule();
       
        scheduleBuilder.hourly(9, 0); // execute daily at AM9:00
        if (scheduleBuilder.add() == null) {
            logger.error("myjobservice error");
        }
    }
}
import org.apache.sling.event.jobs.Job;
import org.apache.sling.event.jobs.consumer.JobConsumer;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(
    immediate = true,
    service = JobConsumer.class,
    property = {
        JobConsumer.PROPERTY_TOPICS + "=my/sample/jobtopic"
    }
)
public class MyJobConsumer implements JobConsumer {
    private static final Logger logger = LoggerFactory.getLogger(MyJobConsumer.class);
    @Override
    public JobResult process(Job job) {
        String topic = job.getTopic();
        logger.info("this message is from myjobconsumer. topic is " + topic);
        return JobResult.OK;
    }  
}

 

 

sample code2 : commons scheduler(just runnable component).This code above is good on local SDK. On AEM cloud, doesn't work.

 

import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(service = Runnable.class, property = { Scheduler.PROPERTY_SCHEDULER_EXPRESSION + "=0 30 * * * ? " })
public class Myjob implements Runnable {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    
    @Override
    public void run() {
        logger.info("this message is from myjob");
    }
}

 

 Please help me.

aem as a cloud service scheduler Sling Jobs

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar
Coach
Employee
Jörg_Hoh
Employee

Likes

1,113 likes

Total Posts

3,145 posts

Correct Reply

1,072 solutions
Top badges earned
Coach
Give back 600
Ignite 5
Ignite 3
Ignite 1
View profile

Avatar
Coach
Employee
Jörg_Hoh
Employee

Likes

1,113 likes

Total Posts

3,145 posts

Correct Reply

1,072 solutions
Top badges earned
Coach
Give back 600
Ignite 5
Ignite 3
Ignite 1
View profile
Jörg_Hoh
Employee

19-03-2021

Regarding the Sling Jobs: That works fine. You just need to be aware that the authoring service is provided by multiple AEM instances, which are clustered. Check the documentation at https://sling.apache.org/documentation/bundles/apache-sling-eventing-and-job-handling.html#job-distr... how to deal with clustered environments. 

 

Regarding the Sling Scheduler: That should work fine. It fires every hour at 30 min, that means at 2:30am, 3:30am, ...

Answers (2)

Answers (2)

Avatar

Avatar
Ignite 1
Level 3
khamat_bn
Level 3

Likes

31 likes

Total Posts

54 posts

Correct Reply

12 solutions
Top badges earned
Ignite 1
Give Back 3
Give Back
Boost 5
Boost 3
View profile

Avatar
Ignite 1
Level 3
khamat_bn
Level 3

Likes

31 likes

Total Posts

54 posts

Correct Reply

12 solutions
Top badges earned
Ignite 1
Give Back 3
Give Back
Boost 5
Boost 3
View profile
khamat_bn
Level 3

19-03-2021

Hi  @watopin ,

 

Sling Job will work for this and the issue for the sling job that you poiniting is because of the Sling job lifecycle. 

If you check the sling job instances which are created for the same Topic is more, and that's what we need to manage. 

There Should be only one instance available for One Topic.

 

We can achieve this one by removing the Scheduled Sling Job first 

IMG_6594.jpg

IMG_6593.jpg

 once we have removed the Scheduled Job then we can start our Sling Job.

 

If we not do this way then every time new instance of Schedule job will created and every install of that job will trigger at required time.

Avatar

Avatar
Springboard
Level 4
bilal_ahmad
Level 4

Likes

66 likes

Total Posts

108 posts

Correct Reply

16 solutions
Top badges earned
Springboard
Validate 10
Validate 1
Ignite 1
Establish
View profile

Avatar
Springboard
Level 4
bilal_ahmad
Level 4

Likes

66 likes

Total Posts

108 posts

Correct Reply

16 solutions
Top badges earned
Springboard
Validate 10
Validate 1
Ignite 1
Establish
View profile
bilal_ahmad
Level 4

19-03-2021

Hey @watopin try this:

import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(service = Runnable.class, property = { "scheduler.expression=0 30 * * * ?" })
public class Myjob implements Runnable {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    
    @Override
    public void run() {
        logger.info("this message is from myjob");
    }
}

 

Thanks,

Bilal.