【spring】Quartz&Task之间的对比

Quartz

  1. 默认多线程异步执行
  2. 单个任务时,在上一个调度未完成时,下一个调度时间到时,会另起一个线程开始新的调度。业务繁忙时,一个任务会有多个调度,可能导致数据处理异常。
  3. 多个任务时,任务之间没有直接影响,多任务执行的快慢取决于CPU的性能
  4. 触发方式 : (1)SimpleTrigger  (2)CronTrigger
  5. 需要在配置文件中实现配置Job
  6. 能被集群实例化,支持分布式部署
  7. 使用JobStoreCMT(JDBCJobStore的子类),Quartz 能参与JTA事务;Quartz 能管理JTA事务(开始和提交)在执行任务之间,这样,任务做的事就可以发生在JTA事务里。

Task

  1. 默认单线程同步执行
  2. 单个任务时,当前次的调度完成后,再执行下一次任务调度
  3. 多个任务时,一个任务执行完成后才会执行下一个任务。若需要任务能够并发执行,需手动设置线程池
  4. 触发方式: 
  5.      与Quartz的CronTrigger的表达式类似
  6. 可以使用注解标注定时任务

比较:

  1. 实现,Task注解实现方式,比较简单。Quartz需要手动配置Jobs。
  2. 任务执行,Task默认单线程串行执行任务,多任务时若某个任务执行时间过长,后续任务会无法及时执行。Quartz采用多线程,无这个问题。
  3. 调度,Task采用顺序执行,若当前调度占用时间过长,下一个调度无法及时执行; 
    Quartz采用异步,下一个调度时间到达时,会另一个线程执行调度,不会发生阻塞问题,但调度过多时可能导致数据处理异常
  4. 部署,Quartz可以采用集群方式,分布式部署到多台机器,分配执行定时任务

quartz示例:

  第一步:创建逻辑类

package com.springdemo.quartz;

public class HelloQuartzq {

    public void index(){
        System.out.println("quartz...");
    }
}

第二步:在spring配置文件spring-servlet.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:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc"
      xmlns:aop="http://www.springframework.org/schema/aop"
      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/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/task
        http://www.springframework.org/schema/task/spring-task-3.0.xsd
           http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    
    <bean id="hello1" class="com.springdemo.quartz.HelloQuartzq"></bean>
    <task:scheduled-tasks> 
       <task:scheduled ref="hello1" method="index" cron="*/5 * * * * ?"/> 
    </task:scheduled-tasks> 

第三步:启动项目,测试

七月 26, 2018 4:06:35 下午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["http-bio-8080"]
七月 26, 2018 4:06:35 下午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["ajp-bio-8009"]
七月 26, 2018 4:06:35 下午 org.apache.catalina.startup.Catalina start
信息: Server startup in 7635 ms
quartz...
quartz...
quartz...
quartz...

 Task示例:

第一步:创建逻辑类

package com.example.demo.job;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class TestJob {
    @Scheduled(cron = "0 0/1 * * * ?")
    public void everyHour(){
        System.out.println("now time:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
    }
}

第二步:修改spring配置文件spring-servlet.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:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc"
      xmlns:aop="http://www.springframework.org/schema/aop"
      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/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/task
        http://www.springframework.org/schema/task/spring-task-3.0.xsd
           http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    
     <!-- 配置扫描的包 -->
     <context:component-scan base-package="com.springdemo.*" />
     
     <mvc:annotation-driven />

第三步:启动项目,测试

猜你喜欢

转载自www.cnblogs.com/huangheng01/p/9372404.html