Java任务调度入门-Quartz&SpringTask对比

quartz

  • 引入quartz

    <!-- https://mvnrepository.com/artifact/org.quartz-scheduler/quartz -->
    <dependency>
        <groupId>org.quartz-scheduler</groupId>
        <artifactId>quartz</artifactId>
        <version>2.3.0</version>
    </dependency>
    
  • quartz.properties

    • 默认加载路径为类的根路径,即开发时,放在mvn项目的resources根目录下
  • scheduler任务调度器

    • DirectSchedulerFactory 或者 StdSchedulerFactory
  • trigger触发器,用于定义任务调度时间规则

    • SimpleTriggerCronTrigger,etc
  • job任务,即被调度的任务

    • 实现org.quartz.Job接口
    • @PersistJobDataAfterExecution@DisallowConcurrentExecution

Quartz 任务调度的核心元素是 scheduler, trigger 和 job,其中 trigger 和 job 是任务调度的元数据, scheduler 是实际执行调度的控制器。

  1. 一个job可以被多个Trigger 绑定,但是一个Trigger只能绑定一个job!

  2. 每次执行时的JobDataMap是clone获得的,所以基础数据类型,String值不会变,对象是引用,对象里的属性值会变

  3. 不能重复添加同一个任务

spring quartz

  • 引入

    <!-- 不能少 -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>4.0.9.RELEASE</version>
    </dependency>
    
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.0.9.RELEASE</version>
    </dependency>
    
    <!-- spring-context-support是核心 -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
      <version>4.0.9.RELEASE</version>
    </dependency>
    
    <!-- quartz自然也不能漏 -->
    <dependency>
        <groupId>org.quartz-scheduler</groupId>
        <artifactId>quartz</artifactId>
        <version>2.3.0</version>
    </dependency>
    
  • 配置

    <?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">
    
        <!--<context:annotation-config/>-->
        <!--<context:component-scan base-package="quartz"/>-->
      
      
    		<!-- 任意bean,不用特意实现quartz的job接口 -->
        <bean id="helloSpring" class="quartz.HelloSpring"/>
      	<!-- 创建一个JobDetail -->
        <bean id="helloSpringJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
            <property name="targetObject" ref="helloSpring"/>
            <property name="targetMethod" value="execute"/>
            <property name="concurrent" value="false"/>
        </bean>
    
      	<!-- 给jobDetail创建一个触发器 -->
        <bean id="helloTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
            <property name="jobDetail" ref="helloSpringJob"/>
            <property name="startDelay" value="3000"/>
            <property name="repeatInterval" value="2000"/>
        </bean>
      
      	<!-- 触发器添加到调度器里 -->
        <bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
            <property name="triggers">
                <list>
                    <ref bean="helloTrigger"/>
                </list>
            </property>
        </bean>
    
    </beans>
    

spring-task

  • 引入

    在spring的配置文件中添加task命名空间即可xmlns:task="http://www.springframework.org/schema/task"&http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd

  • 配置

    <?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:task="http://www.springframework.org/schema/task"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">
    
    
        <bean id="helloSpring" class="quartz.HelloSpring"/>
        <!-- 调度器 -->
        <task:scheduler id="taskScheduler" pool-size="10"/>
      	<!-- job列表 -->
        <task:scheduled-tasks scheduler="taskScheduler">
            <task:scheduled ref="helloSpring" method="execute" fixed-delay="1000"/>
        </task:scheduled-tasks>
    </beans>
    

    #更多

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值