Spring对Quartz的支持

Notice:spring3.1.1好像还没有对quartz 2.x提供支持,这篇文章是针对quartz1.x的。

1.必须软件包(下面是Maven的依赖配置)

View Code
<dependency>
            <groupId>${spring.groupId}</groupId>
            <artifactId>spring-core</artifactId>
        </dependency>
        <dependency>
            <groupId>${spring.groupId}</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>
        <dependency>
            <groupId>${spring.groupId}</groupId>
            <artifactId>spring-beans</artifactId>
        </dependency>
        <dependency>
            <groupId>org.opensymphony.quartz</groupId>
            <artifactId>quartz-all</artifactId>
            <version>1.6.1</version>
        </dependency>
        <dependency>
            <groupId>${spring.groupId}</groupId>
            <artifactId>spring-context-support</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>3.2.1</version>
        </dependency>
        <dependency>
            <groupId>${spring.groupId}</groupId>
            <artifactId>spring-tx</artifactId>
        </dependency>

 

2.对org.quartz.Job接口的支持

Spring提供了一个抽象类org.springframework.scheduling.quartz.QuartzJobBean来简化Job接口的实现。如下面的代码清单:

public class TenSecondJob extends QuartzJobBean {
    private static int times = 0;

    @Override
    protected void executeInternal(JobExecutionContext arg0)
            throws JobExecutionException {
        int seconds = (times++) * 10;
        System.out.println("10 seconds trigger:" + seconds + "秒已经过去了!");
    }

}

3.对org.quartz.JobDetail类的支持

Spring提供了两种方式对JobDetail的支持,

3.1提供了一个方便Bean——org.springframework.scheduling.quartz.JobDetailBean继承了JobDetail。下面是这个类的类声明:

public class JobDetailBean extends JobDetail
        implements BeanNameAware, ApplicationContextAware, InitializingBean {}

因此我们可以在上下文配置文件中使用JobDetailBean配置JobDetail:

<bean id="jobDetail" class="org.springframework.scheduling.quartz.JobDetailBean">
        <property name="jobClass"
            value="com.sin90lzc.train.train_spring_quartz.TenSecondJob"></property>
    </bean>

 

3.2 提供了一个工厂Bean——org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean来生成JobDetail对象。

MethodInvokingJobDetailFactoryBean它会使用一个普通JavaBean的方法来实现Job的功能。这实际上就是生成了一个Job接口的代理对象。

我们先建立一个普通的JavaBean——JobBean:

public class JobBean {
    private int second = 0;

    public void printPerSecond() {
        System.out.println("second trigger:" + (second++) + "过去了!");
    }
}

printPerSecond方法提供了一个Job应该完成功能。那么下面就可以配置MethodInvokingJobDetailFactoryBean,由它来生成一个JobDetail对象

  <bean id="jobDetailCreateByFactaryBean"
        class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject">
            <bean class="com.sin90lzc.train.train_spring_quartz.JobBean" />
        </property>
        <property name="targetMethod" value="printPerSecond"></property>
        <property name="concurrent" value="true"></property>
    </bean>

 

4.Spring对org.quartz.Trigger接口的支持

4.1对org.quartz.SimpleTrigger的支持是提供了一个方便Bean——org.springframework.scheduling.quartz.SimpleTriggerBean。

<bean id="simpleTrigger"
        class="org.springframework.scheduling.quartz.SimpleTriggerBean">
        <property name="jobDetail" ref="jobDetail"></property>
        <property name="startDelay" value="0"></property>
        <property name="repeatInterval" value="10000"></property>
    </bean>

4.2对org.quartz.CronTrigger的支持是提供了一个方便Bean——org.springframework.scheduling.quartz.CronTriggerBean

<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
        <property name="jobDetail" ref="jobDetailCreateByFactaryBean"></property>
        <property name="cronExpression" value="* * * * * ? *"></property>
    </bean>

5.Spring对org.quartz.Scheduler接口的支持

Spring提供了一个工厂Bean来生成Scheduler对象,下面是该工厂Bean的声明:

public class SchedulerFactoryBean extends SchedulerAccessor implements FactoryBean<Scheduler>, BeanNameAware,
        ApplicationContextAware, InitializingBean, DisposableBean, SmartLifecycle {}

下面是该SchedulerFactoryBean的配置示例:

  <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="simpleTrigger" />
                <ref bean="cronTrigger" />
            </list>
        </property>
    </bean>

6.测试上面的代码与配置

public static void main(String[] args) throws Exception{
        ApplicationContext context = new ClassPathXmlApplicationContext(
                "applicationContext.xml");
        //TimeUnit.MINUTES.sleep(10);
    }

 

 

 

posted on 2012-07-06 01:00  TimLeung  阅读(1688)  评论(0编辑  收藏  举报

导航