文章目录 开头 定时器 用法 解析 注册 scheduler scheduled-tasks 调度执行 scheduler初始化 任务调度 Trigger Cron解析 调度 总结 异步执行 配置 原理 开头 从功能上来说,spring-task这个组件主要包括了两个/两种功能: 任务的定时调度/执行,对应xml配置的task:scheduler和task:scheduled-tasks标签 <task:scheduler id="scheduler" pool-size="3" /> <bean id="task" class="task.Task"/> <task:scheduled-tasks /> </task:scheduled-tasks> 定义了一个定时任务,每隔5秒执行Task的print方法,Task: public class Task { public void print scheduled-tasks 其解析的源码较长,在此不再贴出,解析之后形成的BeanDefinition结构如下图: taskScheduler属性即指向task:scheduler标签,如果没有配置
spring-task-3.0.xsd "> <bean id="segmentTask" class="com.week7i.share.SegmentTask"/> <task:scheduled-tasks /> </task:scheduled-tasks> </beans> ② spring-context-task-pool.xml <? 任务会依次执行,如果一个时间超出,后面的任务一直在等待,影响业务--> <task:scheduler id="sgScheduler" pool-size="10"/> <task:scheduled-tasks /> </task:scheduled-tasks> </beans> ③ spring-context-task-pool-annotation.xml <?
-- 定时器相关,用于发放奖金 --> <task:scheduled-tasks> <task:scheduled ref="cycleBonusTimer" method="run" cron= /> </task:scheduled-tasks> public class CycleBonusTimer { public void run(){ //业务 } } Spring 注解方式
task:annotation-driven executor="executor" scheduler="scheduler" proxy-target-class="true"/> <task:scheduled-tasks /> </task:scheduled-tasks> 启动项目 /** * 启动类 * 创建者 科帮网 * 创建时间 2017年7月19日 * */ @EnableAutoConfiguration
com.inth" /> <bean name="taskJob" class="com.inth.product.web.task.ChangeStateTask"></bean> <task:scheduled-tasks fixed-rate="5000"/> <task:scheduled ref="taskJob" method="doJob2" fixed-delay="5000"/> </task:scheduled-tasks
threadPoolTaskExecutor" pool-size="10" queue-capacity="10"/> 执行任务 执行任务很简单,使用<task:scheduled-tasks <task:scheduled-tasks> <task:scheduled ref="simpleService" method="doService" "expensiveTaskService" method="doService" cron="*/2 * * * * *"/> </task:scheduled-tasks <task:scheduled-tasks scheduler="myScheduler"> <task:scheduled ref="beanA" method="methodA" fixed-delay fixed-rate="5000"/> <task:scheduled ref="beanC" method="methodC" cron="*/5 * * * * MON-FRI"/> </task:scheduled-tasks
property> </bean> <task:annotation-driven/> <bean id="myTask" class="com.example.MyTask" /> <task:scheduled-tasks /> </task:scheduled-tasks> 在上面的配置中,我们首先创建了一个线程池任务调度器,cron并设置了定时任务轮询时间,*/5 * * * * ? 表示5执行一次。
task:annotation-driven executor="executor" scheduler="scheduler" proxy-target-class="true"/> <task:scheduled-tasks /> </task:scheduled-tasks> 启动项目 /** * 启动类 * 创建者 科帮网 * 创建时间 2017年7月19日 * */ @EnableAutoConfiguration
com.pojo.system.manager.sigar.impl.SpaceStatisticsServiceImpl"></bean> //通过task标签,定义定时功能 <task:scheduled-tasks /> </task:scheduled-tasks> 实现代码: @Service public class SpaceStatisticsServiceImpl implements SpaceStatisticsService
com.pojo.system.manager.sigar.impl.SpaceStatisticsServiceImpl"></bean> //通过task标签,定义定时功能 <task:scheduled-tasks /> </task:scheduled-tasks> 实现代码: @Service public class SpaceStatisticsServiceImpl implements SpaceStatisticsService
//表示五秒钟执行一次 --> <task:scheduled-tasks> <task:scheduled ref="taskCool" method="testJob /> </task:<em>scheduled-tasks</em>> </beans> 注:上面主要的配置文件中一定要加入task的命名空间和schema。
bean id="demoServiceXmlConfig" class="cn.howtodoinjava.service.DemoServiceXmlConfig" /> <task:scheduled-tasks method="demoServiceMethod" cron="#{applicationProps['cron.expression']}"></task:scheduled> </task:scheduled-tasks
scheduled-tasks元素 可以通过 scheduled-tasks 配置要调度的任务。 <task:scheduled-tasks scheduler="myScheduler"> <task:scheduled ref="beanA" method="methodA" fixed-delay fixed-rate="5000"/> <task:scheduled ref="beanC" method="methodC" cron="*/5 * * * * MON-FRI"/> </task:scheduled-tasks
-- 配置文件实现 如果使用配置文件实现 把注释解开即可然后 删除掉代码的注解--> <task:scheduled-tasks scheduler="scheduler"> /> </task:scheduled-tasks> </beans> 附: cronExpression的配置说明 字段 允许值 允许的特殊字符 秒 0-59 , - * / 分 0-59
task http://www.springframework.org/schema/task/spring-task-3.2.xsd"> 第三步:spring配置文件中设置具体的任务 1 <task:scheduled-tasks /> 3 </task:scheduled-tasks> 注: 项目中使用了Spring注解的形式,所以这里ref直接指向TaskJob这个类, job指向TaskJob类中job方法. cron
task http://www.springframework.org/schema/task/spring-task-3.2.xsd"> 第三步:spring配置文件中设置具体的任务 1 <task:scheduled-tasks /> 3 </task:scheduled-tasks> 注: 项目中使用了Spring注解的形式,所以这里ref直接指向TaskJob这个类, job指向TaskJob类中job方法. cron
相关的代码中启动未连接则不需要Mock 任务示例 # 任务相关的示例类扫描不要注释,只注释任务的执行计划(如下),避免任务执行<task:scheduler id="xxx_task" pool-size="4"/><task:scheduled-tasks </task:scheduled-tasks> 线程、监听器等示例 <context:component-scan base-package="com.xxx.notify"/> <context:component-scan
xxx.crm.service.impl.AccountStatusTaskScanner" /> <task:scheduler id="taskScheduler" pool-size="5" /> <task:scheduled-tasks /></task:scheduled-tasks> 该方案较方案1,2有很大改进,但仍存在以下问题: 步骤复杂、分散,任务量增大的情况下,很难扩展 使用写死服务器Host的方式执行task,存在单点风险
ipBlacklist" value="192.168.7.231" /> </map> </property> </bean> spring task配置 <task:scheduled-tasks /> </task:scheduled-tasks> 待执行任务类 @Component public class SimpleTask { private static int i =
); // 统计信息 Task<TaskStatistics> GetTaskStatisticsAsync(); Web管理界面 组件提供了完整的基于AMIS的Web管理界面,访问路径:/api/scheduled-tasks