zoukankan      html  css  js  c++  java
  • spring集成quartz

    我用的spring是3.1.1和quartz-2.2.3

    首先是需要执行的job(不需要继承任何类或者实现任何接口)

    package org.quartz.spring.job;
    
    import java.util.Date;
    
    public class FirstJob {
    
        public void doJob(){
            System.out.println(new Date());
        }
    }

    配置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:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    
        <bean id="jobDetail"
            class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
            <property name="targetObject" ref="firstJob" />
            <property name="targetMethod" value="doJob" />
        </bean>
        <bean id="cron"
            class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"><!-- CronTriggerFactoryBean CronTriggerBean -->
            <property name="jobDetail">
                <ref bean="jobDetail"></ref>
            </property>
            <property name="cronExpression">
                <value>*/5 * * * * ?</value>
            </property>
        </bean>
        <bean id="schedulerFactoryBean"
            class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
            <property name="triggers">
                <list>
                    <ref bean="cron"></ref>
                </list>
            </property>
        </bean>
        <bean id="firstJob" class="org.quartz.spring.job.FirstJob" />
    </beans>

    当然了需要配置项目启动时加载quartz.xml文件

    web.xml中添加如下

      <context-param>
              <param-name>contextConfigLocation</param-name>
              <param-value>classpath:config/quartz.xml</param-value>
      </context-param> 

    项目启动后打印如下信息

    七月 07, 2016 12:18:10 上午 org.apache.catalina.startup.Catalina start
    信息: Server startup in 1890 ms
    2016-07-07 00:18:15
    2016-07-07 00:18:20
    2016-07-07 00:18:25
    2016-07-07 00:18:30
    2016-07-07 00:18:35

    完成

  • 相关阅读:
    Mybatis学习随笔3
    Mybatis学习随笔2
    Mybatis学习随笔
    Java校招面试-什么是线程安全/不安全
    装饰器2
    装饰器
    默认传参的陷阱
    处理日志文件
    第二天
    用户登录
  • 原文地址:https://www.cnblogs.com/vincentren/p/5648549.html
Copyright © 2011-2022 走看看