在spring中使用定时很简单,首先需要在配置文件中加上task的命名空间
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.0.xsd"
在spring中定时任务有两种方式
1. 注解方式
1. Spring-task.xml
<?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:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"
default-autowire="byName" default-lazy-init="false">
<!--配置包扫描-->
<context:component-scan base-package="com.huawei.system.timer" />
<!-- 定时任务配置 scheduler 方式 注解 -->
<task:executor id="executor" pool-size="5"/>
<task:scheduler id="scheduler" pool-size="10"/>
<task:annotation-driven executor="executor" scheduler="scheduler"/>
</beans>
2. 定时任务类
package com.huawei.system.timer;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.annotation.Resource;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.huawei.resource.system.service.LogService;
import com.huawei.resource.utils.TSMDebugLog;
import com.huawei.system.service.IPersonalLineService;
import com.huawei.system.service.core.ICoreProjectService;
@Component
public class PpmTask {
@Resource
@SuppressWarnings("rawtypes")
private ICoreProjectService coreProjectService;
@Resource
private IPersonalLineService personalLineServiceImpl;
//声明日志打印类
private TSMDebugLog log = new TSMDebugLog(PpmTask.class);
@Resource
private LogService logService;
/**
* @Title: synPpmData
* @Description: 每月12号凌晨3点同步PPM数据
* @param 参数
* @return void 返回类型
* @throws
*/
@Scheduled(cron = "0 0 3 12 * *")
public void synPpmData() {
String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());
//日志入口
log.enterFuncDebugLog();
//打印字符串
log.atFuncDebugLog("执行时间为:" + date);
try {
/*
获取当前年份作为参数传递,因为PPM只能查询到当年的数据
* */
Integer intYear = Calendar.getInstance().get(Calendar.YEAR);
// 调用方法执行业务逻辑
coreProjectService.updateDataByPpmData(intYear.toString());
} catch (Exception e) {
//打印异常
log.writeException(e);
logService.addLog("PpmTask", "核算管理", "同步PPM数据", "失败", date);
//出口日志
log.exitFuncDebugLog("failed");
}
}
/**
* @Title: clearHtmlFile
* @Description: 每天凌晨4:30清空生成的HTML文件
* @param 参数
* @return void 返回类型
* @throws
*/
@Scheduled(cron = "0 30 4 * * ?")
public void clearHtmlFile() {
String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());
//日志入口
log.enterFuncDebugLog();
//打印字符串
log.atFuncDebugLog("执行时间为:" + date);
try {
personalLineServiceImpl.deleHtml();
} catch (Exception e) {
//打印异常
log.writeException(e);
logService.addLog("PpmTask", "部门人力基线", "删除预览生成的html静态文件", "失败", date);
//出口日志
log.exitFuncDebugLog("failed");
}
}
}
注意:
(1). 类上要加上@Component表示包扫描
(2). 方法上要加上@Scheduled,在注解上写cron表达式
3. 在web.xml中引入Spring-task.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-*.xml,classpath:spring-task.xml</param-value>
</context-param>
2. 在配置文件中实现
//定时注解驱动
<task:annotation-driven />
//进行定时任务的类,将其定义为一个bean
<bean id="getUserService" class="com.bjc.service.impl.UserService"></bean>
//通过task标签,定义定时功能
<task:scheduled-tasks>
<task:scheduled ref="getUserService" method="getUser" cron="59 59 23 * * ?" />
</task:scheduled-tasks>