ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Scheduling Jobs in a Java Web Application
Pages: 1, 2, 3

The second configuration step is to include some lines into the web.xml file to initialize the Quartz servlet org.quartz.ee.servlet.QuartzInitializerServlet, because it will add the SchedulerFactory to the ServletContext so we can access it from our Struts plugin. The SchedulerFactory is where we will get our Scheduler object from the Struts plugin. Aside from the struts-config.xml and web.xml entries, you will need a quartz.properties file located in the classes directory of your web application. It is also possible to pass the location of this file to the QuartzInitializerServlet as a parameter in the web.xml file.



  <servlet>

     <servlet-name>QuartzInitializer</servlet-name>
     <display-name>Quartz Initializer Servlet</display-name>

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

  <init-param>
    <param-name>start-scheduler-on-load</param-name>
    <param-value>false</param-value>
    </init-param>

 </servlet>

It is possible to completely remove the usage of the SchedulerPlugIn and Struts completely, but the extra layer of abstraction can be useful if you decide to replace Quartz in favor of some other scheduling framework in the future. Keeping everything loosely coupled will make life easier in the long run. If you use another MVC framework, you can use the code from inside of the SchedulerPlugIn.init() method to achieve the same result with another framework. It is also possible to achieve the same initialization through the use of the ServletContextListener introduced in the Servlet 2.3 specification.

Now that we have our web application configured, we can create and deploy a .war file and watch the console log to see the output from the SchedulerPlugIn. Before we do that, though, let's see how we can submit a job to the scheduler.

In any class of our web application, we can refer to the singleton instance SchedulerPlugIn and schedule some work for execution. The first thing we need is a Trigger to tell the job when to run and how often to run. Quartz has support for several types of triggers. For this example, we will use a CronTrigger.

Trigger trigger = new CronTrigger("trigger1", "group1");
trigger.setCronExpression("0 0 15 ? * WED");

The Trigger above will execute the job every Wednesday at 3:00 p.m. Now all we have to do is to create a JobDetail object and pass that and the trigger to the scheduleWork() method of the SchedulerPlugIn.

    JobDetail jobDetail =
        new JobDetail("Hello World Job",
                      "Hello World Group",
                      HelloWorld.class,
                      true, true, true);    
    //Schedule The work
    SchedulerPlugIn.scheduleWork(scheduledJobDetail, trigger);

Where's the Work?

Now that we've decided on our Trigger, we can get on to scheduling our work. It seems like we're done, but all we did was schedule a job to run; we have yet to complete the most important step. Notice that HelloWorld.class was passed into the JobDetail constructor. This class is where the actual work is being done. HelloWorld extends the Quartz class Job and overrides the execute() method. execute() will get called when the Scheduler decides it is time to run this particular job. Let's look at the code.

import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;

//extend the proper Quartz class
public class HelloWorld extends Job {

    //override the execute method
    public void execute(JobExecutionContext context) {

    // Every job has it's own job detail
    JobDetail jobDetail = context.getJobDetail();
    // The name is defined in the job definition
    String jobName = jobDetail.getName();
    //Every job has a Job Data map for storing extra information
    JobDataMap dataMap = jobDetail.getJobDataMap();
       
       System.out.println("Hello World!!!");
   }
 }

For the sake of testing, you probably want to adjust the timing of the trigger to something a little more frequent, so that you can see your HelloWorld in action. After all, you probably don't want to have to wait until 2:00 a.m. to make sure the scheduled task has actually run. Instead, you might want a Trigger that runs every ten seconds:

Trigger trigger = new SimpleTrigger("trigger1", "group1");
trigger.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY);
trigger.setRepeatInterval(10000L); // milliseconds

Notice that this Trigger didn't use a cron-like syntax. Quartz has a variety of options and configuration methods to suit all of your scheduling needs.

Pages: 1, 2, 3

Next Pagearrow