Springboot+resteasy定时任务
- 定时任务
需求:按每日统计点赞数、评论数、热度的增加量(不是现有量)
1.每天零点执行:首先遍历出user的统计字段
然后插入到新创建的表中。
2.每天一点执行:根据时间段将两表的数据相减
创建增量字段,更新记录下来
package com.xgt.task; import com.xgt.bean.StatisticsUserBean; import com.xgt.bean.UserBean; import com.xgt.dao.entity.StatisticsUser; import com.xgt.dao.entity.User; import com.xgt.service.StatisticsService; import com.xgt.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Configurable; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.util.List; /** * Created by Administrator on 2017/8/14. */ @Component//当组件不好归类的时候,我们可以使用这个注解进行标注。 @Configurable//自动注入bean @EnableScheduling//开启计划任务的支持。 public class StatisticsUserStorage { @Autowired private UserService userService; @Autowired private StatisticsService statisticsService; @Scheduled(cron = "0 0 0 * * ?")//每天零点执行 private void initialStatistics(){ List<User> statisticsList = statisticsService.selectUserAsStatistics(); for (User statistics:statisticsList){ UserBean user = new UserBean(); user.setUserId(String.valueOf(statistics.getUserId())); user.setHeat(statistics.getHeat()); user.setLikeCount(statistics.getLikeCount()); user.setShareTimes(statistics.getShareTimes()); statisticsService.insertStatistics(user); } } @Scheduled(cron = "0 0 1 * * ?")//每天凌晨一点执行 private void generateStatistics(){ List<StatisticsUser> statisticsUserList = statisticsService.subtractTwoTables(); for (StatisticsUser statisticsUser:statisticsUserList){ StatisticsUserBean statisticsUserBean = new StatisticsUserBean(); statisticsUserBean.setLikeCountIncrement(statisticsUser.getLikeCountIncrement()); statisticsUserBean.setHeatIncrement(statisticsUser.getHeatIncrement()); statisticsUserBean.setShareTimesIncrement(statisticsUser.getShareTimesIncrement()); statisticsService.updateStatistics(statisticsUserBean); } } }