- 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
Integrating Quartz Scheduler in Struts 2 Web Application
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.
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