在此记录两种定时任务的配置:

一种是quart定时器:

  <1>配置xml文件(定时任务配置)

  <!--定时任务 -->
<bean id="txfwBomc" class="shmc.framework.scheduling.JobDetailFactoryBean">
<property name="jobDataAsMap">
<map>
<entry key="targetObject" value="bomcManager"/> <!-- 定时任务所在类 -->
<entry key="targetMethod" value="bomcDataPropel"/> <!-- 定时任务实现方法 -->
</map>
</property>
<property name="concurrent" value="false" />
</bean> <!--定义时间间隔触发器 -->
<bean id="bomcTigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="txfwBomc"/>
<property name="cronExpression" value="0 0 1 * * ?" />
</bean>

  <2>定义实现类的bean

<bean id="bomcManager" class="com.test.service.BomcManager" parent="frameworkManager">
</bean>

  <3>启动定时任务

  <!-- 集群定时器调度工厂  -->
<bean id="clusterSchedule" class="shmc.framework.scheduling.SchedulerFactoryBean">
<property name="applicationContextSchedulerContextKey">
<value>applicationContext</value>
</property>
<property name="triggers">
<list>
         <!-- BOMC推送 -->
<ref bean="bomcTigger"/>
</list>
</property>
</bean>

  <4>业务层实现代码

  ………………

一种利用注解:

  <1>配置xml文件,需要引用spring-task-3.1.xsd文档。

<beans
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/task //名称空间的名字
http://www.springframework.org/schema/task/spring-task-3.1.xsd"> //模式文档的位置 <!-- 扫描包路径 -->
<context:component-scan base-package="com.src.xx" >
</context:component-scan>
<!-- 开启定时器-->
<task:annotation-driven/> </beans>

<注意:>上边开启定时任务时,没有设置线程池。而spring默认是单线程,多个任务执行起来时间会有问题(B任务会因为A任务执行起来需要20S而被延后20S执行)

所以要配置线程池如下:

    <!--  开启定时器-->
<task:annotation-driven scheduler="taskScheduler"/>   <!-- 配置定时任务的线程池 -->
<task:scheduler id="taskScheduler" pool-size="5"/>

<2>java代码,具体实现在业务层实现

package com.src.xx.controller.api.timer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; import com.src.xx.service.timer.XianshiTimerService; @Component("taskXianshi")
public class XianshiTimer { @Autowired
private XianshiTimerService xianshiTimerService; /**
* 定时任务
*/
@Scheduled(cron = "0 26 10 * * ?")
public void xsActivityRemind(){
xianshiTimerService.xsActivityRemind();
} }

● spring定时任务cronExpression时间:<注意:每个时间点中间必须有空格分隔>
  一个cronExpression的表达式从左到右定义:
  秒(0-59)
  分钟(0-59)
  小时(0-23)
  月份中的日期(1-31)
  月份(1-12或JAN-DEC)
  星期中的日期(1-7或SUN-SAT)
  年份(1970-2099)

最新文章

  1. docker for mac 学习记录
  2. web前端学习部落22群分享给需要前端练手项目
  3. JsonException: Max allowed object depth reached while trying to export from type System.Single
  4. centos中crontab(计时器)用法详解
  5. 生成API文档的软件
  6. 006. C#使用WMI操作远程计算机
  7. Java-对象数组排序
  8. 10 个超酷的 HTML5/CSS3 应用及源码
  9. Debug Intro
  10. Java笔记(十三)&hellip;&hellip;面向对象III继承(inheritance)
  11. canvas 俄罗斯方块
  12. Ubuntu各种软件的安装
  13. hdu1394(线段树求逆序对)
  14. python注释方法以及编码问题
  15. 三十天学不会TCP,UDP/IP网络编程 - RST的用法
  16. Nginx出现500 Internal Server Error 错误的解决方案
  17. 网球pt站 T3nnis 情况说明
  18. 月薪25K的90后程序员,他们都经历了什么?
  19. 数据分析---SQL(Access数据库)
  20. Python全栈之路----Python基础元素

热门文章

  1. 简单的jquery tab
  2. jQuery 实时监听&lt;input&gt;输入值的变化
  3. 关于sed用法
  4. could not get lock /var/lib/dpkg/lock
  5. TortoiseGit 相关操作
  6. linux suse系统防火墙端口开放配置
  7. xmpp openfire smack 介绍和openfire安装及使用
  8. C#代码
  9. 运行maven项目
  10. 关于UIView的方法animateWithDuration:animations:completion:的说明