Skip to main content

Quartz scheduler example

Quartz scheduler example


 Step 1:
 Create a HelloJob class which implements Job Interface

 import org.quartz.Job;
 import org.quartz.JobExecutionContext;
 import org.quartz.JobExecutionException;

 public class HelloJob implements Job {

 public void execute(JobExecutionContext arg0) throws JobExecutionException{

 //System.out.println("Hello World Quartz Scheduler: " + new Date());

 try {

 System.out.println("trigger Start");

 URL url=new URL("http://localhost:8080/QuartzServletExample/MyServlet");

 URLConnection conn = url.openConnection();


 BufferedReader in = new BufferedReader(new InputStreamReader(


 String inputLine;

 while ((inputLine = in.readLine()) != null)



 System.out.println("trigger End");

 } catch (Exception e) {

 // TODO Auto-generated catch block





 Step 2:

 Create a servlet and name it MyServlet and write the businesslogic in doget()

 Step 3:

 Create a HelloSchedule class

 import java.util.Date;

 import org.quartz.JobDetail;

 import org.quartz.Scheduler;

 import org.quartz.SchedulerFactory;

 import org.quartz.SimpleTrigger;

 import org.quartz.impl.StdSchedulerFactory;

 public class HelloSchedule {

 public HelloSchedule() throws Exception {

 SchedulerFactory sf = new StdSchedulerFactory();

 Scheduler sched = sf.getScheduler();


 JobDetail jd = new JobDetail("myjob", sched.DEFAULT_GROUP,


 SimpleTrigger st = new SimpleTrigger("mytrigger", sched.DEFAULT_GROUP,

 new Date(), null, SimpleTrigger.REPEAT_INDEFINITELY,

 3L * 1000L);

 sched.scheduleJob(jd, st);

 // from cron job use CronTrigger instead of SimpleTrigger

 //CronTrigger ct=new CronTrigger("cronTrigger","group2","0 0/1 * * * ?");

 // sched.scheduleJob(jd,ct);

 //  sched.start();



 Step 4:

 Create an servlet and named it InitializeServlet and call HelloSchedule from init() of this servlet

 public void init() throws ServletException {

 try {

 System.out.println("Initializing NewsLetter PlugIn");
 HelloSchedule objPlugin = new HelloSchedule();
 catch (Exception ex) {


 change web.xml












 Now run the program and deploy the war in server.
 To kill a scheduled process

 first create a listener class and implements  ServletContextListener interface.

 public class KillScheduledProcess implements ServletContextListener{

 public void contextDestroyed(ServletContextEvent arg0) {

 // TODO Auto-generated method stub


 final Thread mainThread = Thread.currentThread();

 Runtime.getRuntime().addShutdownHook(new Thread() {

 public void run() {

 try {

 Scheduler scheduler = null;




 if (scheduler != null &amp;&amp; scheduler.isStarted()){





 catch (SchedulerException e)




 } catch (Exception e) {

 // TODO Auto-generated catch block







 public void contextInitialized(ServletContextEvent arg0) {

 // TODO Auto-generated method stub




 add the listener in web.xml




 Now when you undeploy the application it will automatically destroy the scheduler.


Popular posts from this blog

jdbc mysql example in java

Here’s an example to show you how to connect to MySQL database via a JDBC driver.To connect to MySQL from Java, you have to use the JDBC driver from MySQL. You find the latest MySQL JDBC driver under the following URL:
Create a example database and named it test.Now create a a table name it user. Create table user ((userid INT NOT NULL AUTO_INCREMENT,
username VARCHAR(30) NOT NULL, PRIMARY KEY (userid)); Now add the record in your table; Now create a project in eclipse and mysql-connector jar in its classpath.package com.test;
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; publicclass JavaJDBCExample { private Connection connect = null; private Statement statement = null; private ResultSet resultSet = null; publicvoid readDatabase() { try { // this will load the MySQL driver Class.forName("com.mysql.jdbc.Driver"); // setup the connection wi…

singleton design pattern

Java Singleton design pattern is one of the design pattern which suggest that only one instance of a Singleton object is created by the JVM. This is useful when exactly one object is needed to coordinate actions across the system Example: public class Singleton{ privatestatic Singleton singleton = null; private Singleton(){ } publicstaticsynchronized Singleton getInstance( ) { if(singleton == null) { singleton = new Singleton(); } returnsingleton; } publicvoid test( ) { System.out.println("test"); } @Override protected Object clone() throws CloneNotSupportedException { // TODO Auto-generated method stub returnnew CloneNotSupportedException(); } } publicclass SingletonTest { publicstaticvoid main(String[] args){ Singleton singleton = Singleton.getInstance(); try{ Singleton

jaxb example

Download <strong>eclipselink.jar</strong>,

 Create a java project and add a class in it.Now create a properties file




 package pck;

 import javax.xml.bind.JAXBContext;

 import javax.xml.bind.Marshaller;

 import javax.xml.bind.annotation.XmlElement;

 import javax.xml.bind.annotation.XmlRootElement;

 import net.sf.json.JSONException;

 @XmlRootElement (name="Person")

 public class Person {

 @XmlElement (name="age")

 public int age;

 @XmlElement (name="name")

 public String name;

 public static void main(String[] args) throws JSONException, Exception {

 Person me = new Person();

 me.age = 27; = "Biswajit";


 JAXBContext jc = JAXBContext.newInstance(Person.class);

 Marshaller marshaller = jc.createMar…