spring实现定时任务

1.创建一个定时任务控制器

1/** 2 * 描述:定时器 3 * 作者:温海金 4 * 最后更改时间:下午5:29:49 5 */ 6public class TaskTimerScanner { 7    @Resource 8    private TaskTimerScannerI taskTimerScanner; 9    /** 10     * 功能描述:若30分钟没有付款则更改订单表的状态为订单失效 同时将包车状态改为已取消 11     */ 12    public int updateOrderStatus2Invalid() { 13           return  taskTimerScanner.updateOrderStatus2Invalid();//调用业务层实现具体的定时业务 14    } 15}

2.添加一个定时任务配置:spring-tasks.xml

1<?xml version="1.0" encoding="UTF-8"?> 2<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:c="http://www.springframework.org/schema/c" xmlns:cache="http://www.springframework.org/schema/cache" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:task="http://www.springframework.org/schema/task" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" 3    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 4        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd 5        http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd 6        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 7        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd 8        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd 9        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd 10        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 11        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd 12        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 13        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> 14 15 16          17        <!-- Spring定时器注解开关-->   18        <task:annotation-driven />   19         20        <!-- cron表达式:* * * * * *(共6位,使用空格隔开,具体如下) cron表达式:*(0-59) *(分钟0-59) *(小时0-23)*(日期1-31) *(月份1-12或是JAN-DEC) *(星期1-7或是SUN-SAT) 例如@Scheduled(cron="0/5 * * * * ? ")表示初始化的时候执行一次然后每5秒执行一次-->   21        <!--   <task:scheduled ref="scheduledTaskManager" method="invalidOrderStatus" cron="10/5 * * * * *"/>   --> 22        <task:scheduled-tasks scheduler="myScheduler">   23            <!--30分钟没有付款则更改订单表的状态为订单失效 --> 24            <task:scheduled ref="myTaskTimerScanner" method="updateOrderStatus2Invalid" cron="0/5 * * * * ? "/>  25        </task:scheduled-tasks>   26        <task:scheduler id="myScheduler" pool-size="10"/> 27 28</beans>

3.在spring的主配置文件中配置一个bean管理定时任务控制类,同时引入spring-tasks.xml

1<bean id="myTaskTimerScanner" class="com.lantaiyuan.ebus.custom.scheme.TaskTimerScanner"/> 2 3<!-- 引入spring的定时器 --> 4    <import resource="classpath:spring/spring-tasks.xml" />
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

Java获得今日零时零分零秒的时间(Date型)

publicDatezeroTime()throwsParseException{    DatetimenewDate();    SimpleDateFormatsimpnewSimpleDateFormat("yyyyMMdd00:00:00");    SimpleDateFormatsimp2newS

mysql设置时区

mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0