zoukankan      html  css  js  c++  java
  • 定时任务案例1:每周生成提醒

     1 package nd.sdp.idea.modules.schedule;
     2 
     3 import nd.sdp.idea.modules.idea.entity.Idea;
     4 import nd.sdp.idea.modules.idea.service.IdeaService;
     5 import nd.sdp.idea.modules.reminder.entity.CyclePendingIdeaReminder;
     6 import nd.sdp.idea.modules.reminder.service.CPIReminderService;
     7 import org.joda.time.DateTime;
     8 import org.joda.time.format.ISODateTimeFormat;
     9 import org.slf4j.Logger;
    10 import org.slf4j.LoggerFactory;
    11 import org.springframework.scheduling.annotation.Scheduled;
    12 import org.springframework.stereotype.Component;
    13 
    14 import javax.annotation.Resource;
    15 import java.util.Date;
    16 import java.util.List;
    17 
    18 /**
    19  * 周期性待处理提醒任务
    20  *
    21  * @author yanguanyu(290536)
    22  * @since 0.1 created on 2016/10/20.
    23  */
    24 @Component
    25 public class CyclePendingReminderTask {
    26 
    27     private static final Logger logger = LoggerFactory.getLogger(CyclePendingReminderTask.class);
    28 
    29     @Resource
    30     private IdeaService ideaService;
    31     @Resource
    32     private CPIReminderService cpiReminderService;
    33 
    34     private static final String DATE_FORMAT = "dd/MM/yyyy";
    35 
    36     //每周一0点0分0秒触发,处理上上周
    37     @Scheduled(cron = "0 0 0 ? * MON ")
    38     public void weeklyRemind() {
    39         logger.info("CyclePendingReminderTask.weeklyRemind");
    40         logger.info("周期性待处理提醒任务开始");
    41         String now = DateTime.now().toString(DATE_FORMAT);
    42         //往前推2周,上上周周一
    43         String from = DateTime.parse(now, ISODateTimeFormat.dateElementParser())
    44                 .minusWeeks(2).toString(DATE_FORMAT);
    45         //上上周周日
    46         String to = DateTime.parse(from, ISODateTimeFormat.dateElementParser())
    47                 .plusWeeks(1).minusDays(1).toString(DATE_FORMAT);
    48         //上上周周一0点时间戳
    49         long fromTime = DateTime.parse(from, ISODateTimeFormat.dateElementParser()).getMillis();
    50         //上周周一0点时间戳
    51         long toTime = DateTime.parse(to, ISODateTimeFormat.dateElementParser()).plus(1).getMillis();
    52         List<String> userIdList = ideaService.getUserIdList();
    53         for (String userId : userIdList) {
    54             List<Idea> ideaList = ideaService.findIdeasByCreateAt(userId, fromTime, toTime);
    55             //有创建想法才会有提醒
    56             if (ideaList.size() > 0) {
    57                 CyclePendingIdeaReminder reminder = new CyclePendingIdeaReminder();
    58                 reminder.setUserId(userId);
    59                 reminder.setFrom(from);
    60                 reminder.setTo(to);
    61                 reminder.setFinished(false);
    62                 cpiReminderService.save(reminder);
    63             }
    64         }
    65         logger.info("周期性待处理提醒任务完成");
    66     }
    67 
    68     public static void main(String[] args) {
    69         Date date = DateTime.parse("2016-10-17", ISODateTimeFormat.dateElementParser()).minusWeeks(2).toDate();
    70         System.out.println(date);
    71         date = DateTime.parse("2016-10-03", ISODateTimeFormat.dateElementParser()).plusWeeks(1).minusDays(1).toDate();
    72         System.out.println(date);
    73         System.out.println(DateTime.now().toString(DATE_FORMAT));
    74     }
    75 }
  • 相关阅读:
    名门暗战
    redis安装相关下载
    Git:git diff 命令详解
    【教程】Win7-64位安装OpenSSL详细过程
    Linux 下MQ的安装和配置亲测
    用命令创建MySQL数据库
    WebSphere MQ中的CCSID
    Netty:option和childOption参数设置说明
    BeanNameAware接口和BeanFactoryAware接口
    再理解tcp backlog
  • 原文地址:https://www.cnblogs.com/wihainan/p/6042858.html
Copyright © 2011-2022 走看看