A Simple Quartz/Spring Example

02:20PM May 26, 2007 in category Java by James Goodwill

My current project requires the ability to schedule and manage tasks based upon a time interval or specific time. After looking at Java's TimerTask, I started thinking that I might need something a little more powerful. A buddy of mine said he had great success using Quartz to manage scheduled tasks.

He said It is extremely easy to use and it works so well the Spring developer's have created helper classes to make it easier to integrate into a Spring application. It also has a larger feature set that is not included in the TimerTask. I am using Spring and I always like easy.

According to the Quartz project site, 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 - from the smallest stand-alone application to the largest e-commerce system. 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. The Quartz Scheduler includes many enterprise-class features, such as JTA transactions and clustering.

Quartz is freely usable, licensed under the Apache 2.0 license.

After about 10 minutes of reading, I had a working example deployed and ready to go. It was really too easy. To add Quartz job management to your Spring application, you only need the job class itself and a few new entries in your Spring application context.

Here is a simple Java class that will contain the job code that we want executed based upon an interval or specific time:

public class MyJob extends QuartzJobBean implements StatefulJob {

protected void executeInternal(JobExecutionContext ctx) throws JobExecutionException {
// place your Job code here
System.out.println("EXECUTING QUARTZ JOB");
}
}

As you can see, there is not much to it. It is a simple class. It has to extend the Spring class org.springframework.scheduling.quartz.QuartzJobBean. This class is just a simple implementation of the org.quartz.Job interface, which defines the executeInternal() method. When you create your Job class, you will just extend this class and put your code in your implementation of the executeInternal() method.

You may also notice the MyJob implements the StatefulJob interface. This interface tell Quartz that this job should not run concurrently (one job running at a time). To not implement this interface, if you want to be able to run multiple instances of this job at the same time.

Spring Context Entry:


<!-- Define the Job Bean that will be executed. Our bean is named in the jobClass property. -->
<bean name="myJob" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="com.gsoftware.common.util.MyJob"/>
</bean>

This bean definition is how define our job. The class, org.springframework.scheduling.quartz.JobDetailBean, is a Spring extension of the Quartz class org.quartz.JobDetail. It is used to communicate the detailed properties of a Job instance. In our simple example, we only define one property jobClass. This property defines the class that represents the job. In this case, it is our MyJob.

The next entry in added to the application context is a Quartz Trigger definition. Triggers define how Jobs get scheduled.

    <!-- Associate the Job Bean with a Trigger. Triggers define when a job is executed. -->
<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<!-- see the example of method invoking job above -->
<property name="jobDetail" ref="myJob"/>
<property name="startDelay" value="2000"/>
<property name="repeatInterval" value="10000"/>
</bean>

This Trigger definition represents a Trigger that will execute our exampleJob every 10 seconds, with a delay of 2 seconds.

The final step it to tell Quartz which Triggers you want to have scheduled. Using Spring we can do this with the org.springframework.scheduling.quartz.SchedulerFactoryBean as shown here:

    <!-- A list of Triggers to be scheduled and executed by Quartz -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<propertyy name="triggers">
<list>
<ref bean="simpleTrigger"/>
</list>
</property>
</bean>

This definition schedules one Trigger (our simpleTrigger) for execution.

That is it. All you need now is to load the application context. The TimerApp example application does this below:

public class TimerApp {

public static void main(String[] args) {

// By loading the bean factory, we will start the Quartz Scheduler
// The Quartz Scheduler will continue to run until explicitly shutdown.
LookupFactory.getInstance();
}
}

When you start the app, it will crank up the scheduler and run this Job every 10 seconds.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值