About Quartz

Quartz is a full-featured, open source job scheduling system that can be integrated with, or used along side virtually any J2EE or J2SE application. Quartz can be used to create simple or complex schedules for executing tens, hundreds, or even tens-of-thousands of jobs; jobs whose tasks are defined as standard Java components or EJBs.

  • Integrating Quartz with the framework requires some glue code.

Coding a Quartz ActionJob

ActionJob.java
package com.trantek.sit.action;

import com.opensymphony.xwork.ActionProxy;
import com.opensymphony.xwork.ActionProxyFactory;
import com.opensymphony.xwork.interceptor.component.ComponentInterceptor;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import java.util.HashMap;

public class ActionJob implements Job
{
    public void execute(JobExecutionContext context) throws JobExecutionException
    {
        try
        {
            HashMap ctx = new HashMap();
            ctx.put(ActionContext.PARAMETERS, context.getJobDetail().getJobDataMap());
            ctx.put(ComponentInterceptor.COMPONENT_MANAGER, ???);
            ctx.put(???, ???)
            ServletDispatcher.createContextMap()
            ActionProxy proxy = ActionProxyFactory.getFactory().
                    createActionProxy("", context.getJobDetail().getName(), ctx);

            proxy.execute();
        }
        catch (Exception e)
        {
            throw new JobExecutionException(e);
        }
    }
}

To schedule Actions to be run by Quartz, create a Job where

  • The name of your job is the name of the Action to execute (no ".action" equivalent suffix).
  • All the parameters being sent to the Action are provided by the JobDataMap of the JobDetail object.

    According to the Javadocs of org.quartz.ee.servlet.QuartzInitializerServlet, the Quartz scheduler is setup as a servlet.

Sample Quartz Email Action

The following code schedules an e-mail action:

Scheduling an Email Alert
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);
  • No labels

3 Comments

  1. Unknown User (jsmecham)

    It's not quite clear how in fact you would add the ComponentManager as configured by WebWork so that dependency injection would occur when triggered by Quartz...

  2. Unknown User (elfuhrer)

    The previous WebWorkJob class that was posted breaks out of the entire loop of the application and not even the ActionContext is available,
    what are the parameters that need to be passed to the Component Manager so that the webwork job stays in the appropriate context?

  3. Unknown User (elfuhrer)

    To append to the issue, the ServletActionContext is null within the WebWorkJob Class. Any clue?