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();
}
Solved! Go to Solution.
Views
Replies
Total Likes
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.
Views
Replies
Total Likes
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
Views
Replies
Total Likes
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();
Views
Replies
Total Likes
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.
Views
Replies
Total Likes
srinivasc11017710 wrote...
Thanks
. Could you please provideinputs on the below will behelpfull
1. so cani 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 cani add in try block or it must becatch 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
Views
Replies
Total Likes
Thanks it works.
Views
Replies
Total Likes