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);
        }
    }
}
复制代码

 

posted @   Rest探路者  阅读(1102)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
levels of contents
点击右上角即可分享
微信分享提示