zoukankan      html  css  js  c++  java
  • 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);
            }
        }
    }
  • 相关阅读:
    登录及注册页面
    多方式登录
    git笔记
    后台主页模块设计
    auth模块迁移后需新增字段
    使用idea构建SpringBoot源码
    Springboot相关面试问题
    Springboot自动加载工具-devtools的理解与使用
    SpringBoot项目的一些简单常用配置
    Java线程池及Executor框架的理解
  • 原文地址:https://www.cnblogs.com/Java-Starter/p/7396954.html
Copyright © 2011-2022 走看看