May 13, 2020
Estimated Post Reading Time ~

Scheduler + Interview Questions

The scheduler is a service for scheduling other services/jobs (it uses the open-source Quartz library). The scheduler can be used in two ways, by registering the job through the scheduler API and by leveraging the whiteboard pattern that is supported by the scheduler.
more
For cron expression check this link

Interview Questions
Note:
To create a scheduler click this link

1. How to write scheduler by leveraging the whiteboard pattern?
Exampes of Jobs that are scheduled by leveraging the whiteboard pattern:
Scheduling with a cron expression - The following job is executed every minute by setting scheduler.expression to the cron expression "0 * * ?":

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;
import org.apache.felix.scr.annotations.Property;
@Component
@Service(value = Runnable.class)
@Property( name = "scheduler.expression", value = "0 * * * * ?")
public class ScheduledCronJob implements Runnable {
   /** Default log. */
   protected final Logger log = LoggerFactory.getLogger(this.getClass());
   public void run() {
     log.info("Executing a cron job (job#1) through the whiteboard pattern");
 }
}


Scheduling at periodic times - The following job is executed every ten seconds by setting scheduler.period to 10:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;
import org.apache.felix.scr.annotations.Property;
@Component
@Service(value = Runnable.class)
@Property( name = "scheduler.period", longValue = 10)
public class ScheduledPeriodicJob implements Runnable {
   /** Default log. */
   protected final Logger log = LoggerFactory.getLogger(this.getClass());
   public void run() {
     log.info("Executing a perodic job (job#2) through the whiteboard pattern");
 }
//
}


2. How to write scheduler by registering the job through the scheduler API?
We can write a scheduler using the Scheduler API.

 @Reference
 private Scheduler scheduler;

Defining the job
The following code sample defines a job object that writes a message in the logs:

final Runnable job = new Runnable() {
   public void run() {
    log.info("Executing the job");
  }
  };

Scheduling with a cron expression
To execute the job as defined above at 10:15am every Monday, Tuesday, Wednesday, Thursday, and Friday, you can use the addJob() method with the following parameters:

String schedulingExpression = "0 15 10 ? * MON-FRI";
this.scheduler.addJob("myJob", job, null, schedulingExpression, true);

Scheduling at periodic times
To execute the job as defined above every 3 minutes (180 seconds), you can use the addPeriodicJob() method with the following parameters:

long period = 3*60; //the period is expressed in seconds
this.scheduler.addPeriodicJob("myJob", job, null, period, true);
Scheduling at a given time
To execute the job as defined above at a specific date (on January 10th, 2020), you can use the fireJobAt() method with the following parameters:

SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
String date = "2020/01/10";
java.util.Date fireDate = formatter.parse(date);
this.scheduler.fireJobAt("myJob", job, null, fireDate);

3. What are the methods present in the scheduler API?
http://sling.apache.org/apidocs/sling6/org/apache/sling/commons/scheduler/Scheduler.html


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.