Tuesday, May 29, 2012

Quartz scheduler with Tomcat

How do you use Quartz Scheduler with Tomcat ?

I initially planned to use Quartz Scheduler as a standalone batch. But the bigger problem turned out to be UI for the scheduler. While there are some like Citrine and myscheduler available, I didn't quite feel comfortable with it. Here is how I designed a simple UI. I referred to one of the blogger neopatel to create this sample.

Steps as below
1) Create a JobRegistry which extends QuartzInitializerServlet. Override the doGet and schedule your job.
2) Create a web.xml with entry as below

<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>

<servlet>
        <description></description>
        <display-name>JobRegistery</display-name>
        <servlet-name>JobRegistery</servlet-name>
        <servlet-class>com.amazon.tool.main.RegisterJob</servlet-class>
    </servlet>

2) Create a JSP for front end to access the various jobs.
            List<JobExecutionContext> currentJobs = scheduler.getCurrentlyExecutingJobs();
            String jobName = null;
            Object groupName1 =null;
            for (JobExecutionContext jobCtx : currentJobs) {
                jobName = jobCtx.getJobDetail().getKey().getName();
                groupName1 = jobCtx.getJobDetail().getKey().getGroup();
             }

That's it. You can design a nice UI with the sample above. Please note that you have to hit the JobRegistry servlet once to schedule the job. If you don't want to manually hit it, which will typically be the case, you can register your scheduler during startup by having it in web.xml
Here is the link for the source.

I used Tomcat 6 and Quartz scheduler 2.1 for dev

Enjoy

No comments :

Post a Comment