spring Quartz 和 Spring Task

http://blog.csdn.net/mseeworld/article/details/53331197

1. Quartz设置同步模式时:一个任务的两次执行的时间间隔是:“执行时间”和“trigger的设定间隔”的最大值 
2. Task默认同步模式:一个任务的两次执行的时间间隔是:“执行时间”+“trigger的设定间隔”,即一个任务完成执行后,才开始trigger计时

Spring Quartz 特点: 
1. 默认多线程异步执行 
2. 一个任务在上一次调度未完成执行,下一次调度时间到时,会另起一个线程开始新的调度。在业务繁忙时,一个任务或许会有多个线程在执行,导致数据处理异常。 
3. 单任务同步:配置属性,可以使一个任务的一次调度在未完成时,而不会开启下一次调度 
4. 多个任务同时运行,任务之间没有直接的影响,多任务执行的快慢取决于CPU的性能 
5. SchedulerFactoryBean不能使用注解来配置?还是我没找到注解的方法?

Spring Task特点: 
1. 默认单线程同步执行 
2. 一个任务执行完上一次之后,才会执行下一次调度 
3. 多任务之间按顺序执行,一个任务执行完成之后才会执行另一个任务 
4. 多任务并行执行需要设置线程池 
5. 全程可以通过注解配置

Spring Quartz配置: 
1. 配置Job,job的targetObject可以使用spring注解@Service(value = “findObjectService”)配置的bean 
2. 配置Trigger 
3. 配置Scheduler


<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-3.0.xsd">
  <bean id="findObjectJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> 
    <property name="targetObject" ref="findObjectService" /> 
    <property name="targetMethod" value="startJob" /> 
    <property name="concurrent" value="false" />
  </bean> 
  <bean id="findObjectTrigger" 
        class="org.springframework.scheduling.quartz.SimpleTriggerBean">
    <property name="jobDetail" ref="findObjectJob" />
    <property name="repeatInterval" value="1000" />  
    <property name="startDelay" value="5000" />   
  </bean>
  <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="triggers">
      <list>
        <ref bean="findObjectTrigger" />
      </list>
    </property>
  </bean>
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

Spring Task配置: 
1. 执行方法添加@Scheduled注解 
2. 配置线程池

@Scheduled(cron = "0/1 * *  * * ? ")
public void startJob() {......}

<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-3.1.xsd
        http://www.springframework.org/schema/context  
        http://www.springframework.org/schema/context/spring-context-3.1.xsd
        http://www.springframework.org/schema/task  
        http://www.springframework.org/schema/task/spring-task-3.1.xsd">
<task:executor id="executor" pool-size="5" />
<task:scheduler id="scheduler" pool-size="5" />
<task:annotation-driven executor="executor" scheduler="scheduler" />
<context:annotation-config />  
<context:component-scan base-package="com.test1,com.test2"/>  

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值