AEM cronjob based on certain time interval | Community
Skip to main content
Level 4
February 4, 2016
Solved

AEM cronjob based on certain time interval

  • February 4, 2016
  • 5 replies
  • 8633 views

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();
        }

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Lokesh_Shivalingaiah

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.

5 replies

smacdonald2008
Level 10
February 4, 2016

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

Level 4
February 4, 2016

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();

Lokesh_Shivalingaiah
Lokesh_ShivalingaiahAccepted solution
Level 10
February 4, 2016

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.

Jitendra_S_Toma
Level 10
February 4, 2016

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

Level 4
February 5, 2016

Thanks it works.