业务:今天有一端程序需要执行每三天进行清除数据的操作,涉及到定时调度的操作,今天先了解了一个比较简单的spring task任务调度,是spring的一个模块,感觉简单方便很好理解!下面推荐两种方法
第一种:使用xml文件配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">
<mvc:annotation-driven />
<!-- 扫描指定包下注解 -->
<context:component-scan base-package="com.ymkj.tipmap.controller,
com.ymkj.tipmap.listener,
com.ymkj.tipmap.task,
com.ymkj.tipmap.base.controller"/>
<!-- 放行所有的静态资源 -->
<mvc:default-servlet-handler />
<!-- 引用dubbo服务 -->
<!--
<dubbo:application name="TipMap-manager-web"/>
<dubbo:registry protocol="zookeeper" address="localhost:2181"/>
<dubbo:reference interface="com.ymkj.tipmap.service.TopicService" id="topicServiceImpl" timeout="30000"/>
<dubbo:reference interface="com.ymkj.tipmap.service.NoticeService" id="noticeServiceImpl" timeout="30000"/>
<dubbo:reference interface="com.ymkj.tipmap.service.UserService" id="userServiceImpl" timeout="30000"/>
<dubbo:reference interface="com.ymkj.tipmap.service.MoneyService" id="moneyServiceImpl" timeout="30000"/>
<dubbo:reference interface="com.ymkj.tipmap.service.RecordsService" id="recordsServiceImpl" timeout="30000"/>
<dubbo:reference interface="com.ymkj.tipmap.service.AdminService" id="adminServiceImpl" timeout="30000"/>
-->
<!-- <bean id="loginInterceptor" class="com.ymkj.tipmap.controller.LoginInterceptoer"></bean> -->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<mvc:exclude-mapping path="/admin/*"/>
<ref bean="loginInterceptoer"/>
</mvc:interceptor>
</mvc:interceptors>
<!-- 配置时间调度 -->
<!-- <task:annotation-driven/> -->
<task:scheduled-tasks>
<task:scheduled ref="mySpringTask" method="cron" cron="0 17/1 14 * * ?"/>
</task:scheduled-tasks>
</beans>
第二种:使用注解方式配置(推荐使用,方便快捷)
package com.ymkj.tipmap.task;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class MySpringTask {
private static final Logger logger = LoggerFactory.getLogger(MySpringTask.class);
//@Scheduled(cron="30 12 14 * * ?")
public void cron() {
logger.info("定时任务进行中.......");
//如果没有操作就退出平台
//下面写所需要的业务
}
}
详细请看:点击打开链接