zoukankan      html  css  js  c++  java
  • Quartz —— Spring 环境下的使用

    一、在 Spring 环境下 Quartz 的使用超级简单。

    二、具体使用

    1.添加对应的 spring-quartz 的配置文件。

    2.新建要执行定时任务的目标类和目标方法,不需要继承 Job 接口。如:

    复制代码
    /**
     * @author jfw
     * @create 2016-09-26-10:21
     */
    public class MyJob2 {
    
        public void targetMethod() {
            System.out.println("hello world, quartz, data:" + new Date());
        }
    
    }
    复制代码

    3.在 spring-quartz.xml 文件中配置

    (1)配置执行定时任务的目标类和目标方法。

    (2)配置 JobDetail,需要指定目标类和目标方法。

    (3)配置 Trigger,并在 Trigger 中指定 jobDetail,如果是 CronTrigger ,需要指定 Cron 表达式。

    (4)配置 Scheduler,指定 trigger ,以及一些别的属性。

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <bean class="com.nucsoft.quartz.job.MyJob2" id="myJob"/>
    
        <!-- 配置 JobDetail -->
        <bean class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean" id="jobDetail">
            <property name="targetObject" ref="myJob"/>
            <property name="targetMethod" value="targetMethod"/>
        </bean>
    
        <!-- 配置 CronTrigger -->
        <bean class="org.springframework.scheduling.quartz.CronTriggerFactoryBean" id="cronTrigger">
            <property name="jobDetail" ref="jobDetail"/>
            <property name="cronExpression" value="0/5 * * ? 9 *"/>
        </bean>
    
        <!-- 配置 Scheduler -->
        <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean" id="scheduler">
            <property name="triggers">
                <array>
                    <ref bean="cronTrigger"/>
                </array>
            </property>
            <property name="quartzProperties">
                <props>
                    <prop key="org.quartz.threadPool.threadCount">10</prop>
                </props>
            </property>
            <property name="startupDelay" value="10"/>
        </bean>
    
    </beans>

    说明:Spring 进一步降低了使用 Quartz 的难度,为创建 Quartz 的 JobDetail,Trigger,Scheduler 提供了便利的 FactoryBean,以便能够在 Spring 容器中注入。

    (1)通过 MethodInvokingJobDetailFactoryBean 来创建 JobDetail。

    (2)通过 CronTriggerFactoryBean 来创建 Trigger。

    (3)通过 SchedulerFactoryBean 来创建 Scheduler。

    注意:

    (1)Scheduler 中的属性 quartzProperties 可以指定 orgquartzquartz.properties 文件中的 key。

    (2)Scheduler 中的属性 startupDelay 指定容器启动多久后开始指定定时任务。

    (3)第一次启动定时任务和第二次执行定时任务之间时间间隔与表达式定义不同是因为,第一次是启动定时器,第二次才是真正的执行定时任务。

    4.测试

    /**
     * @author jfw
     * @create 2016-09-26-10:31
     */
    public class QuartzTest {
        public static void main(String[] args) {
            ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-quartz.xml");
        }
    
    }

    只需要启动容器即可。

    三、总结

    介绍了 Spring 环境下 Quartz 的使用,对比发现,在 Spring 下的使用更为简单,也学到了 Spring 整合第三方的内容时,大多是通过 FactoryBean 的方式,真正整合的是第三方

    本身的容器,如 Shiro 的 SecurtyManager,也如 Quartz 的 Scheduler。大容器整合小容器,小容器是一个独立的单元,简洁明了。

  • 相关阅读:
    晨读,难道只是为了完成任务而读的吗?
    集合还有这么优雅的运算法?
    Java中的TreeSet集合会自动将元素升序排序
    CSS3的几个变形案例……
    “老师,请您多关注一下我吧!!!”
    jsp中使用cookie时报错……
    为什么要有周考?周考是用来干什么的?
    今天,我们就来抽个奖!
    今天 ,给大家变个魔术!!!
    Google Maps Api 多坐标分类标记,在地图上显示海量坐标,并分组显示。
  • 原文地址:https://www.cnblogs.com/jiafuwei/p/6249016.html
Copyright © 2011-2022 走看看