spring配置定时任务

一、xml设置定时任务:

配置文件的开头要有:

<?xml version="1.0" encoding="GBK"?>
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        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/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">

配置:

<!-- 设置定时计划,ref配置要执行的bean,method配置要执行的方法
                 可以设置在服务启动后延迟一段时间执行,设置属性fixed-delay,单位为毫秒,比如:fixed-delay="1000",表示服务启动后延迟1秒后开始执行
                 也可以设置间隔一段时间执行一次,设置属性fixed-rate,单位为毫秒,比如fixed-rate="1000",表示每隔1秒执行一次
                 也可以设定执行的时间,设置属性cron,该表达式用于设置执行的时间,格式为"* * * * * *",每个字段依次表示:"秒 分钟 小时 日期 月份 星期",
                 *表示任意值,?表示不设置值,比如:cron="0 0 10 * * *",表示每天上午10点启动 -->
        <task:scheduled-tasks scheduler="consumeScheduler">
                <!-- 每1800秒执行一次导入机构人员程序 -->
                <task:scheduled ref="importPartyTimerTask" method="importParty" fixed-delay="300000"/>
                <task:scheduled ref="ImportTransactionTimerTask" method="importTransaction" fixed-delay="1800000"/>
                <task:scheduled ref="asyncEventTimerTask" method="run" fixed-delay="300000"/>
        </task:scheduled-tasks>
        
        <!-- 创建ThreadPoolTaskScheduler实例,pool-size设定线程的数量 -->
        <task:scheduler id="consumeScheduler" pool-size="3"/>
</beans>


二、注解方式

/**
 * 用户统计定时器
 * 
 * @author sumeng
 * */
@Component
public class UserReportScheduler {
private static final Logger logs = LoggerFactory
.getLogger(UserReportScheduler.class);
@Autowired
private UserReportService userReportService;


/**
* 定时启动。每天凌晨 执行一次 每天凌晨将前一天的用户登录数据保存到表中

* @throws Exception
* */
@Scheduled(cron = "0 0 0 * * ?")
public void recordUserLoginLog() throws Exception {
logs.info("UserReportScheduler定时器启动...");
Date curDate = new Date();
// 取得前一天
Date lastDate = getDateByNum(curDate, -1);
List<UserGameStatus> results = userReportService.getActiveUser(
toMinTimeOfDay(lastDate), toMaxTimeOfDay(lastDate));
//写入
if(results != null && results.size() > 0) {
for(UserGameStatus status : results) {
String userid = status.getUserid();
String playtime = status.getPlaytime();
userReportService.insertUserReportLoginInfo(userid, playtime);
}
logs.info("UserReportScheduler导入完成");
}else {
logs.info("UserReportScheduler没有数据导入");
}
}


/**
* 根据日偏移量获取时间
* @param date
* @param day
* @return
* */
private Date getDateByNum(Date date, int day) throws Exception {
Calendar ca = Calendar.getInstance();
ca.setTime(date);
ca.add(Calendar.DATE, day);
date = ca.getTime();
return date;
}


/**
* 获取日期的最小时间
* @param date
* */
private String toMinTimeOfDay(Date date) throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat(DateFormat.YYYY_MM_DD_HH_mm_ss.getValue());
date = DateUtil.toMinTimeOfDay(date);
if(date != null) {
return sdf.format(date);
}
return null;
}


/**
* 获取日期的最大时间
* @param date
* */
private String toMaxTimeOfDay(Date date) throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat(DateFormat.YYYY_MM_DD_HH_mm_ss.getValue());
date = DateUtil.toMaxTimeOfDay(date);
if(date != null) {
return sdf.format(date);
}
return null;
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值