3.10.14.1. Spring 任务调度

Spring Framework 手册的 Task Execution and Scheduling 部分详细描述了这种机制。

TaskScheduler 可用于在中间层和客户端层的任何应用程序块(block)中运行任意的 Spring bean 对象方法。

spring.xml中的配置示例:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:task="http://www.springframework.org/schema/task"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.3.xsd">
  7. <!--...-->
  8. <task:scheduled-tasks scheduler="scheduler">
  9. <task:scheduled ref="sales_Processor" method="someMethod" fixed-rate="60000"/>
  10. <task:scheduled ref="sales_Processor" method="someOtherMethod" cron="0 0 1 * * MON-FRI"/>
  11. </task:scheduled-tasks>
  12. </beans>

在上面的例子中,声明了两个任务,它们调用 sales_Processor bean 的 someMethod() 方法和 someOtherMethod() 方法。从应用程序启动时起,将以固定的时间间隔(60 秒)调用 someMethod() 方法。根据 Cron 表达式定义的时间表调用 someOtherMethod() 方法(有关此表达式格式的描述,请参阅 http://quartz-scheduler.org/documentation/quartz-1.x/tutorials/crontrigger )。

任务的实际启动由 scheduled-tasks 元素的 scheduler 属性中指定的 bean 执行。它是 CubaThreadPoolTaskScheduler 类型的 bean,在 cuba 应用程序组件的 coreweb 模块中配置(参阅 cuba-spring.xmlcuba-web-spring.xml )。该类提供处理一些 CUBA 框架内特定任务的功能。

如果要向中间层的 Spring 定时任务执行的代码提供SecurityContext,请使用系统身份验证