0

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 make job1 run and it will work. But I have a questions on the code.

In the below code

  1. Why is job1.run() invoked in a catch block? Can we not add it to the 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 must it be in the catch block?
Thread newThread = new Thread(job1);
newThread.start();

I see the cronjob code in the above link

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();
    }
}
toniedzwiedz
  • 17,895
  • 9
  • 86
  • 131
  • 1
    I tried to format the question, so it is readable. Needs to be reviewed though. But I think you copied some strange parts out of the example. – Thomas Feb 04 '16 at 12:01
  • I have answer for your first question.You can write it in try but it is written in catch ,because if the job not exist it will go for try and if the job exist it will move to catch itself.It is something like if-else.When the job is already there,try block will send an exception and will run the job.you can check it,on your system. – Shivani Garg Feb 06 '16 at 00:27

1 Answers1

1

According to the Javadoc, addJob, addPeriodicJob and fireJobAt will throw an Exception if the job cannot be added. The docs do not suggest anything regarding the cause of such failures.

The snippet on the Apache Sling Scheduler documentation page that you quoted in your question catches and ignores these exceptions.

Looking at the implementation provided, job1 is just a regular runnable so executing the run method manually in the catch block does not affect the Scheduler at all.

What it seems to be doing is attempt to add the job and in case of failure, silently ignore it and run it manually so that it prints "Executing job1"

There are at least two serious problems with this approach:

  1. The code ignores the fact that something went wrong while adding the job and pretends this never happens (no logging, nothing)
  2. It runs the job manually giving you the impression that it has been scheduled and just ran for the first time.

As to why it's happening? I have no idea. It's just silly and It's certainly not something I'd like to see in actual, non-tutorial code.

The API using such a generic exception to signal failure is also quite unfortunate.

Coincidentally, Sling 7 deprecates addJob, addPeriodicJob and fireJobAt and replaces them all with schedule. The schedule method returns a boolean so it doesn't give any more information about what exactly happened but it doesn't require you to use ugly try catch blocks.

If you're unable to use the latest version of Sling, make sure to use a logger and log the exceptions. Running your jobs manually, whatever they are, probably won't make much sense but that's something you need to decide.

toniedzwiedz
  • 17,895
  • 9
  • 86
  • 131