users@glassfish.java.net

Re: JEE5 With Quartz

From: <glassfish_at_javadesktop.org>
Date: Mon, 17 Sep 2007 14:19:15 PDT

You can initialize the Quart scheduler either via a servlet or via a servlet lifecycle listener. Here is what I am currently using in my application. This is in my web.xml:

    <servlet>
        <servlet-name>QuartzInitializer</servlet-name>
        <servlet-class>org.quartz.ee.servlet.QuartzInitializerServlet</servlet-class>
        <init-param>
            <param-name>shutdown-on-unload</param-name>
            <param-value>true</param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
    </servlet>

I took a snippet of code that was posted to the Quarts forum and made my own EJB3JobLocalInvoker so that I can schedule Quartz triggers to invoke my EJB 3.0 EJB's through there local interface. It can easily be modified to invoke though a remote interface if needed.

I have a stateless session EJB that I use to interact with Quartz to schedule my jobs:

@Stateless(mappedName="com.canoga.nms.core.v1.schedule.quartz.QuartzSchedulerFacade")
public class QuartzSchedulerFacadeBean implements QuartzSchedulerFacadeRemote, QuartzSchedulerFacadeLocal {

    private Scheduler scheduler;
    
    @PostConstruct
    private void initialize() {
        try {
            SchedulerFactory schedFact = new org.quartz.impl.StdSchedulerFactory();
            this.scheduler = schedFact.getScheduler();
        } catch (SchedulerException ex) {
            Logger.getLogger(QuartzSchedulerFacadeBean.class.getName()).
                    log(Level.SEVERE, null, ex);
        }
    }

    @PreDestroy
    private void cleanup() {
        this.scheduler = null;
    }

    public void scheduleCronJob(String jobName, String jobGroup,
            Class jobClass, Map jobData, Date startDateTime,
            Date endDateTime, String triggerName, String triggerSpec,
            String triggerGroup) {
        scheduleCronJobImpl(jobName, jobGroup, jobClass, jobData, startDateTime, endDateTime, triggerName, triggerSpec, triggerGroup);
    }

    public void scheduleRemoteCronJob(String jobName, String jobGroup,
            Class jobClass, Map jobData, Date startDateTime,
            Date endDateTime, String triggerName, String triggerSpec,
            String triggerGroup) {
        scheduleCronJobImpl(jobName, jobGroup, jobClass, jobData, startDateTime, endDateTime, triggerName, triggerSpec, triggerGroup);
    }

    private void scheduleCronJobImpl(String jobName, String jobGroup,
            Class jobClass, Map jobData, Date startDateTime,
            Date endDateTime, String triggerName, String triggerSpec,
            String triggerGroup) {
        try {

            // Create the JobDetail
            JobDetail jobDetail = new JobDetail(jobName, jobGroup, jobClass);

            // Create the CronTrigger
            Trigger trigger = new CronTrigger(triggerName, triggerGroup,
                    triggerSpec);
            
            // Schedule the job
            this.scheduler.addJob(jobDetail, true);
        } catch (SchedulerException ex) {
            Logger.getLogger(QuartzSchedulerFacadeBean.class.getName()).
                    log(Level.SEVERE, null, ex);
        } catch (ParseException ex) {
            Logger.getLogger(QuartzSchedulerFacadeBean.class.getName()).
                    log(Level.SEVERE, null, ex);
        }
    }
}
[Message sent by forum member 'bbergquist' (bbergquist)]

http://forums.java.net/jive/thread.jspa?messageID=235708