Friday, July 16, 2010

Java/J2ee - Servlet Listener & Quartz Scheduler

Here the concepts of Servlet Listeners & schedulers are shown with example.

Brief Description:
  1. Servlet Listeners are loaded at the time of application deployment in web server.
  2. contextInitialized() & contextDestroyed() are the overloaded functions which are called at the time of application load and unload respectively.
  3. Schedulers are the threads which could be scheduled to run at a particular time.
  4. Job class is actually having the duty of the job in the
    execute() method
  5. Defined Job could be assigned to a scheduler.
  6. CronExpression is an interesting one which is used to define the schedule interval for a particular scheduler.

I used this scheduler for scheduling some alerts in my First real time project "Telematics - ACRM" in my First company Defiance.
Note: We need to add quarts jars in the class path.

Servlet Listener Class

import java.text.ParseException;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.apache.log4j.Logger;
import org.quartz.CronExpression;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;

public class AlertServletListener implements ServletContextListener {
private SchedulerFactory sf = null;
private Scheduler sched = null;
private static transient Logger logger = Logger.getLogger(AlertServletListener.class);

public void contextDestroyed(final ServletContextEvent sce) {
try {
sched.shutdown(true);"AlertServer Shut Down");
} catch (final SchedulerException e) {"Err @ AlertServer Shut Down " + e.getMessage());

public void contextInitialized(final ServletContextEvent sce) {"AlertServer Starting");
try {
sf = new StdSchedulerFactory(); sched = sf.getScheduler(); final JobDetail job3 = new JobDetail("myJob3", "myJobGroup3", MessageProcessJob.class); final CronTrigger ct3 = new CronTrigger("myTrigger3", "myTriggerGroup3"); final CronExpression cexp3 = new CronExpression("1/35 * * * * ?"); ct3.setCronExpression(cexp3); sched.scheduleJob(job3, ct3); sched.start();"AlertServer Started Up"); Thread.sleep(10000L);
} catch (final ParseException e) {
} catch (final SchedulerException e) {
} catch (final InterruptedException e) {

Job Class

import org.apache.log4j.Logger;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class DistanceAlertsJob implements Job {

* logger logger
private static transient Logger logger = Logger.getLogger(DistanceAlertsJob.class);

public void execute(final JobExecutionContext arg0) throws JobExecutionException {"Distance Alert Job");

web.xml (Add the listener entry in web.xml)
..... <listener>


  1. I copy pasted the same thing but it's not running it' showing java.lang.ClassNotFoundException: org.quartz.SchedulerException...
    Plz tell me what to do..

    1. make sure that you have the quartz jar in a valid classpath.

  2. how to show remainders of the tasks with the help of scheduler with alerts