Tuesday, 3 December 2013

Struts2 scheduler demo example

I am sharing the struts2 quartz demo application among us. Currently struts2 framework does not support quartz plugin so we have to manually implement it using to create a Listener java class.

Before to start with scheduler demo example, first we understand what is scheduler?

Scheduler is a program to arrange the jobs, run into particular schedule time and we used frequently use it in our programming development.

Step by step example:

1. You need to download the following jar files to run this demo application.
  • antlr-2.7.6.jar
  • cglib-2.2.jar
  • commons-collections.jar
  • commons-fileupload-1.2.1.jar
  • commons-io-1.3.2.jar
  • commons-logging-1.0.4.jar
  • commons-logging-api-1.1.jar
  • freemarker-2.3.16.jar
  • javassist.jar
  • jcommon-1.0.16.jar
  • log4j-1.2.14.jar
  • ognl-3.0.jar
  • quartz.jar
  • spring-test-2.5.6.jar
  • struts2-core-2.2.1.jar
  • xwork-core-2.2.1.jar
2. now create the basic directory structure for your struts2 web application using your IDE.
3. now open your web.xml file and do your filterdispatcher entry in it.

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 
 <display-name>Struts 2 Web Application</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.aoiblog.demo.listener.QuartzSchedulerListener
 </listener-class>
 </listener>
 <welcome-file-list>
 <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
</web-app>
 
Note : Change your Listener class path as per your directory structure.

4. Now create the QuartzSchedulerListener java class in your package.

package com.aoiblog.demo.listener;
 
import java.text.ParseException;
import java.util.Map;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.impl.StdSchedulerFactory;
 
public class QuartzSchedulerListener implements ServletContextListener{
 
 public void contextDestroyed(ServletContextEvent arg0) {
 //
 }
 
 public void contextInitialized(ServletContextEvent arg0) {
 AoiBlogSchedulerTask task = new AoiBlogSchedulerTask();
 //specify your sceduler task details
 JobDetail aoijob = new JobDetail();
 aoijob.setName("aoiblog");
 aoijob.setJobClass(AoiBlogSchedulerJob.class);
 Map dataMap = aoijob.getJobDataMap();
 dataMap.put("schedulerAoiTask", task);
 try{
 //configure the scheduler time, run it every 5 seconds
 CronTrigger aoitrigger = new CronTrigger();
 aoitrigger.setName("runAOIJobTesting");
 aoitrigger.setCronExpression("0/10 * * * * ?");
 
 //schedule it
 Scheduler schedulerDemo = new StdSchedulerFactory().getScheduler();
 schedulerDemo.start();
 schedulerDemo.scheduleJob(aoijob, aoitrigger);
 
 }catch(ParseException e){
 e.printStackTrace();
 }catch(SchedulerException e){
 e.printStackTrace();
 }
 }
}

5.Now create the two more java class (AoiBlogSchedulerTask and AoiBlogSchedulerJob) within your package.

AoiBlogSchedulerJob :

package com.aoiblog.demo.listener;
 
import java.util.Map;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
 
public class AoiBlogSchedulerJob implements Job {
 
 public void execute(JobExecutionContext context)
 throws JobExecutionException {
 
 Map dataMap = context.getJobDetail().getJobDataMap();
 AoiBlogSchedulerTask task = (AoiBlogSchedulerTask) dataMap.get("schedulerAoiTask");
 task.printSchedulerMessage();
 }
}

 AoiBlogSchedulerTask: Your scheduler business logic will come here

package com.aoiblog.demo.listener;
public class AoiBlogSchedulerTask {
 public void printSchedulerMessage() {
 System.out.println("AOI Blog Scheduler Start.....");
 }
}

Now your scheduler business logic has been done , now we will do some struts2 related stuff.

6.Now we will create a struts.xml configuration file.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
 <constant name="struts.devMode" value="true" />
 <package name="default" namespace="/" extends="struts-default">
 <action name="testAction"
 >
 <result name="success">quartz.jsp</result>
 </action>
 </package>
</struts>

7. Create a struts action java class within your given package.

QuartzStruts2Action-

package com.aoiblog.demo.actions;
import com.opensymphony.xwork2.ActionSupport;
public class QuartzStruts2Action extends ActionSupport{
 public String execute(){
 System.out.println("AOI-BLOG am Calling ........");
 return SUCCESS;
 }
}
 
8. Create JSP file:
quartz.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
</head>
<body>
<h1>This is my first scheduler example</h1>
 </body>
</html>

Please note:

This application is just one type of patch to run scheduler within struts2 application because struts2 don’t have any plugin for scheduler.

Thanks to visit, This example is created on my machine and running fine, But  if you find any problem in demo example or if I did any mistake so please suggest, we are so much happy to take your inputs.

No comments:

Post a Comment