Dashboard > WebWork 1 > WebWork CookBook > Integrating Webwork and Quartz
  WebWork 1 Log In View a printable version of the current page.  
  Integrating Webwork and Quartz
Added by Anders Hovmöller, last edited by Hani Suleiman on Apr 25, 2004  (view change)
Labels: 
(None)

Integrating Webwork with Quartz is very easy and also very powerful. The following class performs the glue between Quartz and Webwork:

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import webwork.action.ActionContext;
import webwork.dispatcher.GenericDispatcher;

public class WebworkJob implements Job
{
  public void execute(JobExecutionContext context) throws JobExecutionException
  {
    try
    {
      ActionContext.setParameters(context.getJobDetail().getJobDataMap());
      GenericDispatcher dispatcher = new 
                             GenericDispatcher(context.getJobDetail().getName());
      dispatcher.prepareValueStack();
      dispatcher.executeAction();
      dispatcher.finish();
      dispatcher.finalizeContext();
    }
    catch (Exception e)
    {
      throw new JobExecutionException(e);
    }
  }
}

To schedule webwork actions you simply create a job where

  • the name of your job is the name of the WW action to execute (no ".action" suffix).
  • all the parameters you want to send to the WW action is contained in the JobDataMap of the JobDetail

(I have set up the Quartz scheduler as a servlet according to the javadocs of org.quartz.ee.servlet.QuartzInitializerServlet.)

I have a mail action that I can schedule with the following code:

Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();

JobDetail jobDetail = new JobDetail("email.send", 
                                     scheduler.DEFAULT_GROUP, WebworkJob.class);

Map m = jobDetail.getJobDataMap();
m.put("to", "me@bogusdomain.com");
m.put("subject", "quartz test");
m.put("body", "This is a quartz test, Hey ho");
m.put("smtpServer", "smtp.bogusdomain.com");
m.put("from", "quartz@bogusdomain.com");

SimpleTrigger trigger = new SimpleTrigger("myTrigger", 
                                          scheduler.DEFAULT_GROUP, 
                                          new Date(), null, 0, 0L);

scheduler.deleteJob("email.send", scheduler.DEFAULT_GROUP);
scheduler.scheduleJob(jobDetail, trigger);

Site powered by a free Open Source Project / Non-profit License (more) of Confluence - the Enterprise wiki.
Learn more or evaluate Confluence for your organisation.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.2.9 Build:#527 Sep 07, 2006) - Bug/feature request - Contact Administrators