View Javadoc

1   ////////////////////////////////////////////////////////////////////////////////
2   //CabaWeb
3   //Copyright (C) 2004  Thomas Vogt <Thomas.Vogt@TVC-Software.com>
4   //
5   //This library is free software; you can redistribute it and/or
6   //modify it under the terms of the GNU Lesser General Public
7   //License as published by the Free Software Foundation; either
8   //version 2.1 of the License, or (at your option) any later version.
9   //
10  //This library is distributed in the hope that it will be useful,
11  //but WITHOUT ANY WARRANTY; without even the implied warranty of
12  //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  //Lesser General Public License for more details.
14  //
15  //You should have received a copy of the GNU Lesser General Public
16  //License along with this library; if not, write to the Free Software
17  //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ////////////////////////////////////////////////////////////////////////////////
19  
20  package org.fhw.cabaweb.webfrontend.servlets;
21  
22  import javax.servlet.ServletException;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.struts.action.ActionServlet;
27  import org.apache.struts.action.PlugIn;
28  import org.apache.struts.config.ModuleConfig;
29  import org.fhw.cabaweb.calculation.timer.BerechnungsJob;
30  
31  import org.quartz.CronTrigger;
32  import org.quartz.JobDetail;
33  import org.quartz.Scheduler;
34  import org.quartz.SchedulerException;
35  import org.quartz.impl.StdSchedulerFactory;
36  
37  /***
38   * Die Klasse dient als Plugin für Struts um den Timer im Hintergrund laufen zu lassen.
39   * Das PlugIn hat gegenüber dem Servlet den Vorteil, dass es beim hot deployment gestoppt wird.
40   * Es wird dann nach dem Deployment neu gestartet, das ist beim Servlet nicht sichergestellt.
41   *
42   * @author  <a href="mailto:thomas.vogt@tvc-software.com">Thomas Vogt</a>
43   * @version Version 1.0 21.10.2004
44   */
45  public class TimerStrutsPlugin implements PlugIn
46  {
47      /*** Commons Logging Instanz */
48  	private static Log log = LogFactory.getLog(TimerStrutsPlugin.class);
49  
50      /*** Der Gruppenname */
51  	private String JOB_GROUP = "CabaWebGroup";
52  
53      /*** Der Scheduler */
54  	Scheduler sched;
55  
56  	/* (Kein Javadoc)
57  	 * @see org.apache.struts.action.PlugIn#init(org.apache.struts.action.ActionServlet, org.apache.struts.config.ModuleConfig)
58  	 */
59  	public void init(ActionServlet servlet, ModuleConfig moduleConfig) throws ServletException
60  	{
61  		log.info("Quartz starting");
62  
63  		try
64  		{
65  			sched = StdSchedulerFactory.getDefaultScheduler();
66  			sched.start();
67  		}
68  		catch (Exception e)
69  		{
70  			log.info("Quartz Scheduler failed to initialize: " + e.toString());
71  			throw new ServletException(e);
72  		}
73  
74  		log.info("Initializing jobs...");
75  		addJob(sched, "CalculationJob", BerechnungsJob.class, "0 0/5 * * * ?");
76  		log.debug("Quartz started");
77  	}
78  
79  	/***
80       * Die Methode erzeugt den Berechnungsjob und stellt ihn in den Scheduler.
81       * 
82  	 * @param sched Der Scheduler
83  	 * @param jobName Der Name des Jobs
84       * @param jobKlasse Klasse die zu den angegebenen Intervallen ausgeführt werden soll
85  	 * @param timing Das Timing (CRON Expression Stil)
86  	 */
87  	private void addJob(Scheduler sched, String jobName, Class jobKlasse, String timing)
88  	{
89  		JobDetail jobDetail = new JobDetail(jobName, JOB_GROUP, jobKlasse);
90          CronTrigger cronTrigger = new CronTrigger(jobName, JOB_GROUP);
91  		
92          try
93  		{
94              cronTrigger.setCronExpression(timing);
95              sched.scheduleJob(jobDetail, cronTrigger);
96  		}
97  		catch (Exception e)
98  		{
99  			e.printStackTrace();
100 		}
101 	}
102 
103 	/* (Kein Javadoc)
104 	 * @see org.apache.struts.action.PlugIn#destroy()
105 	 */
106 	public void destroy()
107 	{
108 		log.info("Quartz stopping");
109 
110 		try
111 		{
112 			sched.shutdown();
113 		}
114 		catch (SchedulerException ex)
115 		{
116 			ex.printStackTrace();
117 		}
118         
119 		sched = null;
120 	}
121 
122 }