Here the concepts of Servlet Listeners & schedulers are shown with example.
Brief Description:
web.xml (Add the listener entry in web.xml)
..... <listener>
<listener-class>
com.boss.pageflows.schedular.AlertServletListener
</listener-class>
</listener>
.....
- Servlet Listeners are loaded at the time of application deployment in web server.
- contextInitialized() & contextDestroyed() are the overloaded functions which are called at the time of application load and unload respectively.
- Schedulers are the threads which could be scheduled to run at a particular time.
- Job class is actually having the duty of the job in the execute() method.
- Defined Job could be assigned to a scheduler.
- 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.
Code:
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);
@Override
public void contextDestroyed(final ServletContextEvent sce) {
try { sched.shutdown(true); logger.info("AlertServer Shut Down");
} catch (final SchedulerException e) { logger.info("Err @ AlertServer Shut Down " + e.getMessage());
}
}
@Override
public void contextInitialized(final ServletContextEvent sce) {
logger.info("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(); logger.info("AlertServer Started Up"); Thread.sleep(10000L);
} catch (final ParseException e) { e.printStackTrace();
} catch (final SchedulerException e) { e.printStackTrace();
} catch (final InterruptedException e) { e.printStackTrace();
}
}
}
private SchedulerFactory sf = null;
private Scheduler sched = null;
private static transient Logger logger = Logger.getLogger(AlertServletListener.class);
@Override
public void contextDestroyed(final ServletContextEvent sce) {
try { sched.shutdown(true); logger.info("AlertServer Shut Down");
} catch (final SchedulerException e) { logger.info("Err @ AlertServer Shut Down " + e.getMessage());
}
}
@Override
public void contextInitialized(final ServletContextEvent sce) {
logger.info("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(); logger.info("AlertServer Started Up"); Thread.sleep(10000L);
} catch (final ParseException e) { e.printStackTrace();
} catch (final SchedulerException e) { e.printStackTrace();
} catch (final InterruptedException e) { e.printStackTrace();
}
}
}
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);
@Override
public void execute(final JobExecutionContext arg0) throws JobExecutionException {
logger.info("Distance Alert Job");
Sysytem.out.println("Hello");
}
}
web.xml (Add the listener entry in web.xml)
..... <listener>
<listener-class>
com.boss.pageflows.schedular.AlertServletListener
</listener-class>
</listener>
.....
I copy pasted the same thing but it's not running it' showing java.lang.ClassNotFoundException: org.quartz.SchedulerException...
ReplyDeletePlz tell me what to do..
make sure that you have the quartz jar in a valid classpath.
Deletehow to show remainders of the tasks with the help of scheduler with alerts
ReplyDelete