Thanks Wiebe. I saw your name on a post or two in the Quartz forum and was
hoping you'd respond to this here. I really appreciate it!

Brian Barnett

-----Original Message-----
From: Wiebe de Jong [mailto:[EMAIL PROTECTED] 
Sent: Thursday, September 30, 2004 12:28 PM
To: 'Struts Users Mailing List'; 'Thomas Vogt'
Subject: RE: Struts and Quartz Scheduler

Starting Quartz from a servlet is not a good idea, use a Plugin instead.

Reason:

I am running on the JBoss application server, and the threads that Quartz
creates on startup are attached to the app server when using the servlet
method. Everything works fine, but when your webapp is shutdown, the threads
keep going. The threads do not stop until the app server is stopped. So, if
you do a hot deploy (just replace the .ear) of your application, which I do
quite often during development, you'll end up with multiples of your jobs
executing at the same time.

Using plugin:

If you use a Struts plugin, the threads stop when the webapp stops, even
when doing hot deploys. In the plugin, the init() method gets called once
when Struts starts up and the destroy() method gets called once when Struts
shuts down. 

In my code below, I am called jobs implemented as stateless session beans.
Your jobs might be implemented differently.

Code:

Web.xml: 
Nothing here because you are not using a servlet

Struts-config.xml:
Add this line:

  <plug-in className="com.mycompany.myapp.QuartzPlugin" />

QuartzPlugin.java:

package com.mycompany.myapp;

import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.commons.logging.*;
import org.apache.struts.action.*;
import org.apache.struts.config.*;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.jobs.ee.ejb.EJBInvokerJob;

import java.util.*;

public class QuartzPlugin implements PlugIn {

  private static Log log = LogFactory.getLog(QuartzPlugin.class);

  private String ECOM_GROUP = "eComGroup";

        Scheduler sched;

  public void init(ActionServlet servlet, ModuleConfig moduleConfig)
        throws ServletException {

    log.info("Quartz starting");
    
//      Scheduler sched;
    try {
      sched = StdSchedulerFactory.getDefaultScheduler();
      sched.start();
    } catch (Exception e) {
      log.info("Quartz Scheduler failed to initialize: " + e.toString());
      throw new ServletException(e);
    }
    
    log.info("Initializing jobs...");

    addJob(sched, "Heartbeat", "HeartbeatJob", "execute", "0 0/15 * * * ?");
// every 15 minutes
    addJob(sched, "ExpiredTrial", "ExpiredTrialJob", "execute", "0 0 20/24 *
* ?"); // every day at 8pm

    log.debug("Quartz started");  
  }
    
  private void addJob(Scheduler sched, String jobName, String jndiName,
String methodName, String timing) {
    JobDetail jd = new JobDetail(jobName, ECOM_GROUP, EJBInvokerJob.class);
    jd.getJobDataMap().put(EJBInvokerJob.EJB_JNDI_NAME_KEY, jndiName);
    jd.getJobDataMap().put(EJBInvokerJob.EJB_METHOD_KEY, methodName);
    Object[] jdArgs = new Object[0];
    jd.getJobDataMap().put("args", jdArgs);
    CronTrigger cronTrigger = new CronTrigger(jobName, ECOM_GROUP);   
    try {
      cronTrigger.setCronExpression(timing);
      sched.scheduleJob(jd, cronTrigger);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  
  public void destroy() {
    log.info("Quartz stopping");

                try {
                        sched.shutdown();
                } catch (SchedulerException ex) {
                        ex.printStackTrace();
                }
                sched = null;
  }
        
}


I think I'll put this into my blog.

Wiebe de Jong
http://frontierj.blogspot.com/


-----Original Message-----
From: Thomas Vogt [mailto:[EMAIL PROTECTED] 
Sent: Wednesday, September 29, 2004 10:08 PM
To: Struts Users Mailing List
Subject: AW: Struts and Quartz Scheduler

You need a class to be executed as Job

public final class BerechnungsJob implements Job
{
     /* (Kein Javadoc)
      * @see org.quartz.Job#execute(org.quartz.JobExecutionContext)
      */
     public void execute(JobExecutionContext context) throws
JobExecutionException
     {
        ...
     }
}

and you need a servlet that looks like this to init the Job Trigger

32  /***
33   * Die Klasse dient als Listener für den Servlet Container um den Timer
im Hintergrund laufen zu lassen.
34   *
36   * @version Version 1.0 27.08.2004
37   */
38  
39  public class TimerServlet implements Servlet
40  {
41  
42      /***
43       * @see javax.servlet.Servlet#init(ServletConfig)
44       */
45      public void init(ServletConfig arg0) throws ServletException
46      {
47          SchedulerFactory schedFact = new
org.quartz.impl.StdSchedulerFactory();
48          JobDetail jobDetail = new JobDetail("Calculation Timer",
"Calculation Timer", BerechnungsJob.class);
49          CronTrigger trigger = new CronTrigger("Calculation Timer",
"Calculation Timer");
50  
51          try
52          {
53              trigger.setCronExpression("0 0/5 * * * ?");
54              Scheduler sched = schedFact.getScheduler();
55              sched.start();
56              sched.scheduleJob(jobDetail, trigger);
57          }
58          catch (Exception e)
59          {
60              e.printStackTrace();
61          }
62      }
63  
64      /***
65       * @see javax.servlet.Servlet#getServletConfig()
66       */
67      public ServletConfig getServletConfig()
68      {
69          return null;
70      }
71  
72      /***
73       * @see javax.servlet.Servlet#service(ServletRequest,
ServletResponse)
74       */
75      public void service(ServletRequest arg0, ServletResponse arg1)
throws ServletException, IOException
76      {
77      }
78  
79      /***
80       * @see javax.servlet.Servlet#getServletInfo()
81       */
82      public String getServletInfo()
83      {
84          return null;
85      }
86  
87      /***
88       * @see javax.servlet.Servlet#destroy()
89       */
90      public void destroy()
91      {
92      }
93  
94  }

which is to be made public and started in web.xml so it is started on
deployment.

Hope this helps

Thomas



--------- Original-Nachricht --------
Von: Struts Users Mailing List <[EMAIL PROTECTED]>
An: 'Struts Users Mailing List ' <[EMAIL PROTECTED]>
Betreff: Struts and Quartz Scheduler
Datum: 30/09/04 06:06

> Has anyone here integrated Quartz with Struts? I'm having a hard time
> finding examples on this combination.
> 
> I'm using the latest version of Quartz, 1.4.2, and I'm initializing it in
> web.xml like this:
> 
>       &lt;servlet&gt;
>               &lt;servlet-name&gt;
>                       QuartzInitializer
>               &lt;/servlet-name&gt; 
>               &lt;display-name&gt;
>                       Quartz Initializer Servlet
>               &lt;/display-name&gt; 
>               &lt;servlet-class&gt;
>                       org.quartz.ee.servlet.QuartzInitializerServlet
>               &lt;/servlet-class&gt; 
>               &lt;load-on-startup&gt;
>                       1
>               &lt;/load-on-startup&gt;
>               &lt;init-param&gt;
>
&lt;param-name&gt;shutdown-on-unload&lt;/param-name&gt;
>                       &lt;param-value&gt;true&lt;/param-value&gt;
>               &lt;/init-param&gt;
>       &lt;/servlet&gt;        
> 
> Anything you could share with me would be greatly appreciated. If you have
a
> quartz.properties file you could share, info on how you access the
> QuartInitializerServlet, submit jobs, etc., I'd love to see them. Sample
> code would be great.
> 
> If you know of a good document that explains it, please let me know about
> it.
> 
> Thanks a bunch,
> Brian Barnett
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 
> 
> 

________________________________________________
Message sent using UebiMiau 2.7.8



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to