Pages Navigation Menu

Coding is much easier than you think

Integrating Quartz Scheduler in Struts 2 Web Application

Posted by in Quartz Scheduler, Struts-2 | 1 comment

Quartz-Schedular
 
In our previous article we learn about Quartz 2 hello world example along with types of Triggers in Quartz 2, In this article we shall learn to Integrating Quartz 2 Scheduler in Struts 2 web Application. To know more about Quartz please visit its Official website.
 

Library required

 
Commonly Required Struts 2 Jars
commons-logging-1.1.1.jar
log4j-1.2.16.jar
quartz-2.2.1.jar
slf4j-api-1.6.6.jar
slf4j-log4j12-1.6.6.jar
 

Scheduler Job

 
Create a Quartz’s job

package com.quartz;

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

public class QuartzJob implements Job {
	public void execute(JobExecutionContext context)
			throws JobExecutionException {
		System.out.println("Struts 2 + Quartz 2.2.1");
	}
}

 

Servlet Listener

 
Now Create servlet listener class by implementing ServletContextListener interface and override contextInitialized and contextDestroyed methods with your logic’s.

  • In contextInitialized() method method I have written a code to start the Quartz Scheduler, and since this method will be executed automatically during Servlet container initialization, hence the code for Quartz scheduler job gets invoked, and so it runs for every 10 seconds.
  • contextDestroyed() method will be executed when the application shuts down, So in this function I have invoked the shutdown function of quartz scheduler

 

package com.quartz;

import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.quartz.CronScheduleBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.impl.StdSchedulerFactory;

public class QuartzListener implements ServletContextListener {
	Scheduler scheduler = null;

	@Override
	public void contextInitialized(ServletContextEvent servletContext) {
		System.out.println("Context Initialized");
		
		try {
			// Setup the Job class and the Job group
			JobDetail job = newJob(QuartzJob.class).withIdentity(
					"CronQuartzJob", "Group").build();

			// Create a Trigger that fires every 5 minutes.
			Trigger trigger = newTrigger()
			.withIdentity("TriggerName", "Group")
			.withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ?"))
			.build();

			// Setup the Job and Trigger with Scheduler & schedule jobs
			scheduler = new StdSchedulerFactory().getScheduler();
			scheduler.start();
			scheduler.scheduleJob(job, trigger);
		}
		catch (SchedulerException e) {
			System.err.println(e.getMessage());
		}
	}

	@Override
	public void contextDestroyed(ServletContextEvent servletContext) {
		System.out.println("Context Destroyed");
		try
		{
			scheduler.shutdown();
		}
		catch (SchedulerException e)
		{
			System.err.println(e.getMessage());
		}
	}
}

 
Recommended Article

web.xml

 
Configure the listener class QuartzSchedulerListener.java into the web.xml file as shown below

<web-app>
<display-name>Quartz 2 + Struts 2</display-name>
<filter>
  <filter-name>struts2</filter-name>
  <filter-class>
       org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
   </filter-class>
</filter>
 
<filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
   <listener-class>
      com.quartz.QuartzListener
   </listener-class>
 </listener>
</web-app>

 

Demo

 
Now on starting the Tomcat Server, the project gets started, and so the listener class QuartzSchedulerListener.java registered in web.xml will be fired, and following logs is obtained at the console
 
Struts 2 + Quartz logs
 

Read More

Integrating Quartz Scheduler with Java Web Application

Posted by in J2EE, Java, Quartz Scheduler | 2 comments

 
Quartz-Schedular
 
In our previous article we learn about Quartz 2 hello world example along with types of Triggers in Quartz 2, In this article we shall learn to Integrating Quartz 2 Scheduler in Java Web Application. Quartz, is an open source job scheduling framework, that let you schedule a task to run on a predefine date and time.
 

Library required

 
commons-logging-1.1.1.jar
log4j-1.2.16.jar
quartz-2.2.1.jar
slf4j-api-1.6.6.jar
slf4j-log4j12-1.6.6.jar
 

Scheduler Job

 
Create a Quartz’s job

package com.quartz;

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

public class QuartzJob implements Job {
	public void execute(JobExecutionContext context)
			throws JobExecutionException {
		System.out.println("Java web application + Quartz 2.2.1");
	}
}

 

Servlet Listener

 
Now Create servlet listener class by implementing ServletContextListener interface and override contextInitialized and contextDestroyed methods with your logic’s.

  • contextInitialized() method will be executed automatically during Servlet container initialization, which in turn calls the Quartz scheduler job, which gets executed every 10 seconds.
  • contextDestroyed() method will be executed when the application shuts down, So in this function I have invoked the shutdown function of quartz scheduler

 

package com.quartz;

import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.quartz.CronScheduleBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.impl.StdSchedulerFactory;

public class QuartzListener implements ServletContextListener {
	Scheduler scheduler = null;

	@Override
	public void contextInitialized(ServletContextEvent servletContext) {
		System.out.println("Context Initialized");
		
		try {
			// Setup the Job class and the Job group
			JobDetail job = newJob(QuartzJob.class).withIdentity(
					"CronQuartzJob", "Group").build();

			// Create a Trigger that fires every 5 minutes.
			Trigger trigger = newTrigger()
			.withIdentity("TriggerName", "Group")
			.withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ?"))
			.build();

			// Setup the Job and Trigger with Scheduler & schedule jobs
			scheduler = new StdSchedulerFactory().getScheduler();
			scheduler.start();
			scheduler.scheduleJob(job, trigger);
		}
		catch (SchedulerException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void contextDestroyed(ServletContextEvent servletContext) {
		System.out.println("Context Destroyed");
		try
		{
			scheduler.shutdown();
		}
		catch (SchedulerException e)
		{
			e.printStackTrace();
		}
	}
}

 
Recommended Article

 

web.xml

 
Configure the listener class QuartzSchedulerListener.java into the web.xml file as shown below

<listener>
<listener-class>com.quartz.QuartzListener</listener-class>
</listener>

 

Demo

 
Now on starting the Tomcat Server, the project gets started, and so the registered listener class QuartzSchedulerListener.java will be fired, and following output is obtained at the console
 
Integrating Quartz Scheduler with Java Web Applications
 

Read More

Quartz Scheduler – Scheduling Job in Java via Quartz Scheduler

Posted by in Java, Quartz Scheduler

Quartz-Schedular
 
Quartz scheduler to help Java application to scheduler a job/task to run at a specified date and time.
 

Library

 
commons-logging-1.1.1.jar
log4j-1.2.16.jar
quartz-2.2.1.jar
slf4j-api-1.6.6.jar
slf4j-log4j12-1.6.6.jar
 

Note To deploy Quartz on Application server like JBoss, oracle or weblogic, you may need addition Quartz dependency, read this guide.

 

Quartz Job

 
Quartz job is defined what you want to run

package com.quartz;

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

public class QuartzJob implements Job {
	public void execute(JobExecutionContext context)
			throws JobExecutionException {
		JobKey jobKey = context.getJobDetail().getKey();
		System.out.println("Quartz " + "Job Key " + jobKey);
	}
}

 

Quartz Trigger

 
Quartz trigger is defined when the Quartz will run your above Quartz’s job
 
There are 2 types of triggers in Quartz 2

  • SimpleTrigger – Allows to set start time, end time, repeat interval.
  • CronTrigger – Allows Unix cron expression to specify the dates and times to run your job.
  •  
    SimpleTrigger – Run every 10 seconds.
     

    Trigger trigger = newTrigger().withIdentity("TriggerName", "Group1")
                    .withSchedule(SimpleScheduleBuilder.simpleSchedule()
    		.withIntervalInSeconds(10).repeatForever()).build();
    

     
    CronTrigger – Run every 10 seconds
     

    Trigger trigger = newTrigger()
    		.withIdentity("TriggerName", "Group2")
    		.withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ?")).build();
    

     
    Scheduler
     
    Scheduler class links both “Job” and “Trigger” together and execute it.
     

    Scheduler scheduler = new StdSchedulerFactory().getScheduler();
    scheduler.start();
    scheduler.scheduleJob(job, trigger);
    

     
    Recommended Article

    Full Example
     
    Quartz 2 full examples with SimpleTrigger and CronTrigger.
     

    package com.quartz;
    
    import static org.quartz.JobBuilder.newJob;
    import static org.quartz.TriggerBuilder.newTrigger;
    
    import org.quartz.CronScheduleBuilder;
    import org.quartz.JobDetail;
    import org.quartz.Scheduler;
    import org.quartz.SchedulerException;
    import org.quartz.SimpleScheduleBuilder;
    import org.quartz.Trigger;
    import org.quartz.impl.StdSchedulerFactory;
    
    public class QuartzSchedulerDemo {
    
    public static void SimpleTriggerExample() throws SchedulerException {
    
    	JobDetail job = newJob(QuartzJob.class).withIdentity(
    			"SimpleQuartzTrigger", "Group1").build();
    
    	Trigger trigger = newTrigger()
    			.withIdentity("TriggerName", "Group1")
    			.withSchedule(SimpleScheduleBuilder.simpleSchedule()
    			.withIntervalInSeconds(10).repeatForever())
    			.build();
    
    	Scheduler sched = new StdSchedulerFactory().getScheduler();
    	sched.scheduleJob(job, trigger);
    	sched.start();
    }
    
    public static void CronTriggerExample() throws SchedulerException {
    	JobDetail job = newJob(QuartzJob.class).withIdentity(
    			"CronQuartzTrigger", "Group2").build();
    
    	Trigger trigger = newTrigger()
    			.withIdentity("TriggerName", "Group2")
    			.withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ?"))
    			.build();
    
    	Scheduler scheduler = new StdSchedulerFactory().getScheduler();
    	scheduler.start();
    	scheduler.scheduleJob(job, trigger);
    }
    
    public static void main(String[] args) throws SchedulerException {
    	SimpleTriggerExample();
    	CronTriggerExample();
    }
    }
    

     
    Do Read

    Read More