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); } } }
作者:Rest探路者
出处:http://www.cnblogs.com/Java-Starter/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意请保留此段声明,请在文章页面明显位置给出原文连接
Github:https://github.com/cjy513203427
分类:
resteasy
, SpringBoot
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?