Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

AEM cronjob based on certain time interval

Avatar

Level 4

I  am trying to create a cronjob in cq using a time interval

I see on the link https://sling.apache.org/documentation/bundles/scheduler-service-commons-scheduler.html i could job1 and it will work .But i questions on the code.

In the  below code 

1.Why the job1.run() is invovked in catch block .cannot we add it try block
2.can i replace  the catch block instead of job1.run() using  thread using start and can i add in try block or it must be catch block
Thread newThread= new Thread(job1);
            newThread.start();


protected void activate(ComponentContext componentContext) throws Exception {
        //case 1: with addJob() method: executes the job every minute
        String schedulingExpression = "0 * * * * ?";
        String jobName1 = "case1";
        Map<String, Serializable> config1 = new HashMap<String, Serializable>();
        boolean canRunConcurrently = true;
        final Runnable job1 = new Runnable() {
            public void run() {
                log.info("Executing job1");
            }
        };
        try {
            this.scheduler.addJob(jobName1, job1, config1, schedulingExpression, canRunConcurrently);
        } catch (Exception e) {
            job1.run();
        }

1 Accepted Solution

Avatar

Correct answer by
Level 10

Scheduler uses a whiteboard pattern to run the thread. So when scheduler.addJob() will call job.run() at the configured times. In the example, they are calling it manually job.run() if there is any exception while adding a job. We may not have to call job.run() explicitly in try block and no compulsion to have that in the catch block aswell.

View solution in original post

5 Replies

Avatar

Level 10

We have a community article that shows another example of this: 

Scheduling Adobe Experience Manager Jobs using Apache Sling

The job will fire according to the schedule that is setup. Even in this example - there is job.run in the Exception block - but what determines when the job is fired is the cron expression. For example:

 String schedulingExpression = "0 15 10 ? * MON-FRI"; //10:15am every Monday, Tuesday, Wednesday, Thursday and Friday

Avatar

Level 4

Thanks.Could you please  provide  inputs on the  below  will be helpfull

 

1.so can i move the job1.run() to try block and remove it from catch block is that fine??
2.can i replace  the catch block instead of job1.run() using  thread using start and can i add in try block or it must be catch block
Thread newThread= new Thread(job1);
            newThread.start();

Avatar

Correct answer by
Level 10

Scheduler uses a whiteboard pattern to run the thread. So when scheduler.addJob() will call job.run() at the configured times. In the example, they are calling it manually job.run() if there is any exception while adding a job. We may not have to call job.run() explicitly in try block and no compulsion to have that in the catch block aswell.

Avatar

Level 9

srinivasc11017710 wrote...

Thanks.Could you please  provide  inputs on the  below  will be helpfull

1.so can i move the job1.run() to try block and remove it from catch block is that fine??
2.can i replace  the catch block instead of job1.run() using  thread using start and can i add in try block or it must be catch block
Thread newThread= new Thread(job1);
            newThread.start();

 

1. Yes, I don't think that is required at all. The scheduler calls the run method and no need to invoke thread explicitly.

2. You don't need it. Just put the scheduler time for 1 min and debug it.

--Jitendra