Quartz入门示例

    文章出处 http://www.cnblogs.com/phinecos/archive/2008/09/03/1283376.html

Spring scheduling.quartz 包中对 Quartz 框架进行了封装,使得开发时不用写任何 QuartSpring 的代码就可以实现 定时任务。 Spring 通过 JobDetailBean MethodInvokingJobDetailFactoryBean 实现 Job 的定义。后者更加实用,只需指定要运行的类,和该类中要运行的方法即可, Spring 将自动生成符合 Quartz 要求的 JobDetail

1, 创建一个 Web 项目, 加入 spring.jarquartz-1.6.0.jarcommons-collections.jarjta.jar commons-logging.jar 这几个包 .

     2, 创建一个类,在类中添加一个方法 execute, 我们将对这个方法进行定时调度.

package com.vista.quartz;

import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class HelloWorld {
     private static Log logger = LogFactory.getLog(HelloWorld.class);//日志记录器
        public HelloWorld()
        {
        }
        public void execute()
        {
            logger.info("Kick your ass and Fuck your mother! - " + new Date()+"正在运行中");
        }
}
3.
Spring 配置文件 applicationContext.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:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"
           default-autowire="byName">

    <!-- 要调用的工作类 -->
    <bean id="quartzJob" class="com.vista.quartz.HelloWorld"></bean>
    <!-- 定义调用对象和调用对象的方法 -->
    <bean id="jobtask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <!-- 调用的类 -->
        <property name="targetObject">
            <ref bean="quartzJob"/>
        </property>
        <!-- 调用类中的方法 -->
        <property name="targetMethod">
             <value>execute</value>
        </property>
    </bean>
    <!-- 定义触发时间 -->
    <bean id="doTime" class="org.springframework.scheduling.quartz.CronTriggerBean">
        <property name="jobDetail">
            <ref bean="jobtask"/>
        </property>
        <!-- cron表达式   每隔5秒运行一次-->
        <property name="cronExpression">
            <value>10,15,20,25,30,35,40,45,50,55 * * * * ?</value>
        </property>
    </bean>
    <!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序  -->
    <bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
               <ref bean="doTime"/>
            </list>
        </property>
    </bean>
</beans>

4.
, 先在控制台中对上面的代码进行测试,我们要做的只是加载 Spring 的配置文件就可以了,代码如下 :

package com.vista.quartz;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {
         System.out.println("Test start.");
         //applicationContext.xml放到classpath路径下 在没有eclipse中就是放在src目录下
         ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
         //如果配置文件中将startQuertz bean的lazy-init设置为false 则不用实例化
         //context.getBean("startQuertz");
      System.out.print("Test end..");

    }

}
5.然后将
Web.xml 修改如下,让 tomcat 在启动时去初始化 Spring

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
     <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/spring/applicationContext.xml//配置文件的存放目录
        </param-value>
    </context-param>
   
    <servlet>
        <servlet-name>SpringContextServlet</servlet-name>
        <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值