Quartz Spring demo 详解

Quartz是一个基于Java的作业调度管理的轻量级框架,目前在很多企业应用中被使用,它的作用类似于java.util中的Timer和TimeTask、数据库中的job等,但Quartz的功能更强大、更灵活。从Quartz2开始,你可以使用POJO作为一个任务(Job),这种开发方式进一步降低了代码的耦合度,如果跟Spring进行整合,使用起来将更加方便简单。Spring中继承并简化了Quartz,下面根据给出的demo看看在Spring中怎样配置Quartz

1、先上工作代码

/* 

*Description:这是一个执行调度的具体类,在Spring配置文件中指定调用此类的work方法
 */
package com.tsinghua.test;
import java.util.Date;
/*
 * 使用spring+Quartz执行任务调度的具体类
 * */
public class MyJob {

    /*
     * Description:具体工作的方法,此方法只是向控制台输出当前时间,
     * 输入的日志在:%tomcatRoot%\logs\tomcat7-stdout.yyyy-MM-dd.log中,
     * 其中,yyyy-MM-dd为部署的日期,经试验发现默认情况下并不是每天都生成一个stdout的日志文件
     * @return 返回void
     * */
    public void work()
    {
         System.out.println("当前时间:"+new Date().toString());  
    }
}//End of MyJob

2、配置quartz.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    <!-- 工作的bean -->
    <bean id="myJob" class="com.tsinghua.test.MyJob" />
    <!-- job的配置开始 -->
    <bean id="myJobDetail"
        class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject">
            <ref bean="myJob" />
        </property>
        <property name="targetMethod">
            <value>work</value>
        </property>
    </bean>
    <!-- job的配置结束 -->
    <!-- 调度的配置开始 -->
    <bean id="crontestJobTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
        <property name="jobDetail">
            <ref bean="myJobDetail" />
        </property>
        <property name="cronExpression">
            <value>0/1 * * * * ?</value>
        </property>
    </bean>
    <!-- 调度的配置结束 -->
    <!-- 启动触发器的配置开始 -->
    <bean name="startQuertz" lazy-init="false" autowire="no"
        class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="crontestJobTrigger" />
            </list>
        </property>
    </bean>
    <!-- 启动触发器的配置结束 -->
</beans>

3.配置web.xml,具体配置文件如下:

<?xml version="1.0" encoding="UTF-8"?> 
      <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <display-name>SpringQuartzDemo</display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <!-- 說明:Spring的配置文件設置必須在啟動Spring Bean工廠監聽之前,否則會報錯, .Net配置文件好像沒有先後順序 -->
    <!-- Spring配置文件開始 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/classes/quartz.xml</param-value>
    </context-param>
    <!-- Spring配置文件結束 -->
    <!-- 启动 Spring Bean 工厂监听開始 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- 启动 Spring Bean 工厂监听結束 -->
</web-app>

4、在Tomcat中调试,即可在Console窗口看到输出

参考:

http://www.cnblogs.com/yaowen/p/3779284.html

http://blog.csdn.net/jackfrued/article/details/16918149

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值