- web.xml文件,通过监听器加载spring总配置文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="dataservice" version="3.0">
<display-name>timer application</display-name>
<!-- spring监听器加载applicationContext.xml配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
2.spring总配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 导入spring task配置文件-->
<import resource="applicationContext-task.xml"/>
<!-- 导入服务消费者配置 -->
<import resource="applicationContext-dubbo-consumer.xml" />
</beans>
3.spring task配置文件 (注意这个task约束:xmlns:task=“http://www.springframework.org/schema/task”)
<?xml version="1.0" encoding="UTF-8"?>
<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:tx="http://www.springframework.org/schema/tx"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd ">
<!-- 自动扫描的包名 -->
<context:component-scan base-package="com.bjpowernode.p2p.timer"/>
<!-- 定义调度器:定义一个大小为10的线程池调度器 -->
<!--<task:scheduler id="myScheduler" pool-size="10"/>-->
<!-- 定义执行器 :定义一个大小为10的线程池执行器-->
<!--<task:executor id="myExecutor" pool-size="10"/>-->
<!-- 启用注解方式,即Spring定时器注解开关配置 -->
<!--<task:annotation-driven scheduler="myScheduler" executor="myExecutor"/>-->
<!-- spring-task定时任务-->
<task:annotation-driven/>
</beans>
4.定时管理类(由spring创建对象)
package com.bjpowernode.p2p.timer;
import com.bjpowernode.p2p.service.loan.IncomeRecordService;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
//spring注入此类 <context:component-scan
// base-package="com.bjpowernode.p2p.timer"/>所起作用
@Component
public class TimerManager {
private Logger logger = LogManager.getLogger(TimerManager.class);
@Autowired
private IncomeRecordService incomeRecordService;
// @Scheduled(cron = "0/5 * * * * ?")
public void generateIncomePlan() {
logger.info("------------生成收益计划开始------------");
incomeRecordService.generateIncomePlan();
logger.info("------------生成收益计划结束------------");
}
// Scheduled 因为 <task:annotation-driven/> 这个标签才起作用的; //cron 表达式有两种形式,在此表示任务每隔5s执行一次
@Scheduled(cron = "0/5 * * * * ?")
public void generateIncomeBack() {
logger.info("------------收益返还开始------------");
incomeRecordService.generateIncomeBack();
logger.info("------------收益返还结束------------");
}
}