내 커뮤니티 업적 표시줄을 확대합니다.

Submissions are now open for the 2026 Adobe Experience Maker Awards.

Mark Solution

활동이 없어 이 대화는 잠겼습니다. 새 게시물을 작성해 주세요.

해결됨

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 채택된 해결책 개

Avatar

정확한 답변 작성자:
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.

원본 게시물의 솔루션 보기

5 답변 개

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

정확한 답변 작성자:
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

Avatar

Level 4

Thanks it works.