How can I create a background job in AEM as a Cloud?
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);
@3214626
private JobManager jobManager;
public static final String JOB_TOPIC = "my/sample/jobtopic";
@580286
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);
@9944223
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());
@9944223
public void run() {
logger.info("this message is from myjob");
}
}
Please help me.