定时任务案例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.enums.OriginalityType; 5 import nd.sdp.idea.modules.idea.service.IdeaService; 6 import nd.sdp.idea.modules.reminder.entity.IdeaSelectionReminder; 7 import nd.sdp.idea.modules.reminder.service.ISReminderService; 8 import org.joda.time.DateTime; 9 import org.joda.time.format.ISODateTimeFormat; 10 import org.slf4j.Logger; 11 import org.slf4j.LoggerFactory; 12 import org.springframework.scheduling.annotation.Scheduled; 13 import org.springframework.stereotype.Component; 14 15 import javax.annotation.Resource; 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 IdeaSelectionReminderTask { 26 27 private static final Logger logger = LoggerFactory.getLogger(IdeaSelectionReminderTask.class); 28 29 @Resource 30 private IdeaService ideaService; 31 @Resource 32 private ISReminderService isReminderService; 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 weeklySelectionRemind() { 39 logger.info("IdeaSelectionReminderTask weeklySelectionRemind start."); 40 logger.info("周精选任务开始"); 41 DateTime nowTime = DateTime.now(); 42 String now = nowTime.toString(DATE_FORMAT); 43 String from = DateTime.parse(now, ISODateTimeFormat.dateElementParser()) 44 .minusWeeks(1).toString(DATE_FORMAT); 45 String to = DateTime.parse(now, ISODateTimeFormat.dateElementParser()) 46 .minusDays(1).toString(DATE_FORMAT); 47 doWeekly(from, to); 48 logger.info("周精选任务完成"); 49 logger.info("IdeaSelectionReminderTask weeklySelectionRemind finish."); 50 } 51 52 //可供接口外部触发 53 public void doWeekly(String from, String to) { 54 long fromTime = DateTime.parse(from, ISODateTimeFormat.dateElementParser()).getMillis(); 55 DateTime toDateTime = DateTime.parse(to, ISODateTimeFormat.dateElementParser()); 56 long toTime = toDateTime.plusDays(1).getMillis();//下一天0点 57 int month = toDateTime.getMonthOfYear();//周所属月份,按周日所在的月份来看 58 int quarter = (month - 1) / 3 + 1; 59 int year = toDateTime.getYear();//周所属年份,按周日所在的年来看 60 List<String> userIdList = ideaService.getUserIdList(); 61 for (String userId : userIdList) { 62 List<Idea> ideaList = ideaService.findOriginalities( 63 userId, fromTime, toTime, null); 64 if (ideaList.size() > 0) { 65 IdeaSelectionReminder reminder = 66 buildReminder(userId, OriginalityType.week, from, to, fromTime, toTime, 67 year, quarter, month); 68 isReminderService.save(reminder); 69 } 70 } 71 } 72 73 //每月一号0点0分0秒触发 74 //当中再判断当前月份进行季度和年度的处理操作 75 @Scheduled(cron = "0 0 0 1 * ? ") 76 public void monthlySelectionRemind() { 77 logger.info("IdeaSelectionReminderTask monthlySelectionRemind start."); 78 DateTime nowTime = DateTime.now(); 79 int month = nowTime.getMonthOfYear(); 80 String now = nowTime.toString(DATE_FORMAT); 81 //年度处理: 1 82 if (month == 1) { 83 logger.info("年度精选任务开始"); 84 String from = DateTime.parse(now, ISODateTimeFormat.dateElementParser()) 85 .minusYears(1).toString(DATE_FORMAT); 86 String to = DateTime.parse(now, ISODateTimeFormat.dateElementParser()) 87 .minusDays(1).toString(DATE_FORMAT); 88 doMonthly(from, to, OriginalityType.year); 89 logger.info("年度精选任务完成"); 90 } 91 //季度处理: 3(4) 6(7) 9(10) 12(1) 92 if (month == 4 || month == 7 || month == 10 || month == 1) { 93 logger.info("季度精选任务开始"); 94 String from = DateTime.parse(now, ISODateTimeFormat.dateElementParser()) 95 .minusMonths(3).toString(DATE_FORMAT); 96 String to = DateTime.parse(now, ISODateTimeFormat.dateElementParser()) 97 .minusDays(1).toString(DATE_FORMAT); 98 doMonthly(from, to, OriginalityType.quarter); 99 logger.info("季度精选任务完成"); 100 } 101 //月份处理 102 logger.info("月精选任务开始"); 103 String from = DateTime.parse(now, ISODateTimeFormat.dateElementParser()) 104 .minusMonths(1).toString(DATE_FORMAT); 105 String to = DateTime.parse(now, ISODateTimeFormat.dateElementParser()) 106 .minusDays(1).toString(DATE_FORMAT); 107 doMonthly(from, to, OriginalityType.month); 108 logger.info("月精选任务完成"); 109 logger.info("IdeaSelectionReminderTask monthlySelectionRemind finish."); 110 } 111 112 public void doMonthly(String from, String to, OriginalityType type) { 113 DateTime fromDT = DateTime.parse(from, ISODateTimeFormat.dateElementParser()); 114 //修正开始时间为所在周的周一 115 fromDT = getMondayDateTime(fromDT); 116 DateTime toDT = DateTime.parse(to, ISODateTimeFormat.dateElementParser()); 117 int month = toDT.getMonthOfYear(); 118 int quarter = (month - 1) / 3 + 1; 119 int year = toDT.getYear(); 120 long fromTime = fromDT.getMillis(); 121 long toTime = toDT.getMillis(); 122 List<String> userIdList = ideaService.getUserIdList(); 123 //todo 这里不管有没有待选项都生成记录,如果要求没有不能提醒,则需要在筛选接口里面做特殊处理,把没有待选项的提醒标记为完成 124 for (String userId : userIdList) { 125 IdeaSelectionReminder reminder = 126 buildReminder(userId, type, from, to, fromTime, toTime, 127 year, quarter, month); 128 isReminderService.save(reminder); 129 } 130 } 131 132 133 private IdeaSelectionReminder buildReminder(String userId, OriginalityType type, String from, String to, 134 long fromTime, long toTime, int year, int quarter, int month) { 135 IdeaSelectionReminder reminder = new IdeaSelectionReminder(); 136 reminder.setUserId(userId); 137 reminder.setType(type); 138 reminder.setFrom(from); 139 reminder.setFromTime(fromTime); 140 reminder.setTo(to); 141 reminder.setToTime(toTime); 142 reminder.setYear(year); 143 reminder.setQuarter(quarter); 144 reminder.setMonth(month); 145 reminder.setFinished(false); 146 return reminder; 147 } 148 149 // 获取date所在周的周一的时间 150 private static DateTime getMondayDateTime(DateTime date) { 151 int dayOfWeek = date.getDayOfWeek(); 152 return date.minusDays(dayOfWeek-1); 153 } 154 155 public static void main(String[] args) { 156 System.out.println(DateTime.now().getMonthOfYear()); 157 System.out.println(DateTime.parse("2017-01-01", ISODateTimeFormat.dateElementParser()).minusMonths(3)); 158 DateTime dt = DateTime.parse("2015-11-01", ISODateTimeFormat.dateElementParser()); 159 System.out.println(getMondayDateTime(dt)); 160 } 161 }
package nd.sdp.idea.modules.schedule;
import nd.sdp.idea.modules.idea.entity.Idea;
import nd.sdp.idea.modules.idea.enums.OriginalityType;
import nd.sdp.idea.modules.idea.service.IdeaService;
import nd.sdp.idea.modules.reminder.entity.IdeaSelectionReminder;
import nd.sdp.idea.modules.reminder.service.ISReminderService;
import org.joda.time.DateTime;
import org.joda.time.format.ISODateTimeFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.List;
/**
* 想法精选提醒(周,月,季度,年)
*
* @author yanguanyu(290536)
* @since 0.1 created on 2016/10/20.
*/
@Component
public class IdeaSelectionReminderTask {
private static final Logger logger = LoggerFactory.getLogger(IdeaSelectionReminderTask.class);
@Resource
private IdeaService ideaService;
@Resource
private ISReminderService isReminderService;
private static final String DATE_FORMAT = "dd/MM/yyyy";
//每周一0点0分0秒触发
@Scheduled(cron = "0 0 0 * * MON ")
public void weeklySelectionRemind() {
logger.info("IdeaSelectionReminderTask weeklySelectionRemind start.");
logger.info("周精选任务开始");
DateTime nowTime = DateTime.now();
String now = nowTime.toString(DATE_FORMAT);
String from = DateTime.parse(now, ISODateTimeFormat.dateElementParser())
.minusWeeks(1).toString(DATE_FORMAT);
String to = DateTime.parse(now, ISODateTimeFormat.dateElementParser())
.minusDays(1).toString(DATE_FORMAT);
doWeekly(from, to);
logger.info("周精选任务完成");
logger.info("IdeaSelectionReminderTask weeklySelectionRemind finish.");
}
//可供接口外部触发
public void doWeekly(String from, String to) {
long fromTime = DateTime.parse(from, ISODateTimeFormat.dateElementParser()).getMillis();
DateTime toDateTime = DateTime.parse(to, ISODateTimeFormat.dateElementParser());
long toTime = toDateTime.plusDays(1).getMillis();//下一天0点
int month = toDateTime.getMonthOfYear();//周所属月份,按周日所在的月份来看
int quarter = (month - 1) / 3 + 1;
int year = toDateTime.getYear();//周所属年份,按周日所在的年来看
List<String> userIdList = ideaService.getUserIdList();
for (String userId : userIdList) {
List<Idea> ideaList = ideaService.findOriginalities(
userId, fromTime, toTime, null);
if (ideaList.size() > 0) {
IdeaSelectionReminder reminder =
buildReminder(userId, OriginalityType.week, from, to, fromTime, toTime,
year, quarter, month);
isReminderService.save(reminder);
}
}
}
//每月一号0点0分0秒触发
//当中再判断当前月份进行季度和年度的处理操作
@Scheduled(cron = "0 0 0 1 * ? ")
public void monthlySelectionRemind() {
logger.info("IdeaSelectionReminderTask monthlySelectionRemind start.");
DateTime nowTime = DateTime.now();
int month = nowTime.getMonthOfYear();
String now = nowTime.toString(DATE_FORMAT);
//年度处理: 1
if (month == 1) {
logger.info("年度精选任务开始");
String from = DateTime.parse(now, ISODateTimeFormat.dateElementParser())
.minusYears(1).toString(DATE_FORMAT);
String to = DateTime.parse(now, ISODateTimeFormat.dateElementParser())
.minusDays(1).toString(DATE_FORMAT);
doMonthly(from, to, OriginalityType.year);
logger.info("年度精选任务完成");
}
//季度处理: 3(4) 6(7) 9(10) 12(1)
if (month == 4 || month == 7 || month == 10 || month == 1) {
logger.info("季度精选任务开始");
String from = DateTime.parse(now, ISODateTimeFormat.dateElementParser())
.minusMonths(3).toString(DATE_FORMAT);
String to = DateTime.parse(now, ISODateTimeFormat.dateElementParser())
.minusDays(1).toString(DATE_FORMAT);
doMonthly(from, to, OriginalityType.quarter);
logger.info("季度精选任务完成");
}
//月份处理
logger.info("月精选任务开始");
String from = DateTime.parse(now, ISODateTimeFormat.dateElementParser())
.minusMonths(1).toString(DATE_FORMAT);
String to = DateTime.parse(now, ISODateTimeFormat.dateElementParser())
.minusDays(1).toString(DATE_FORMAT);
doMonthly(from, to, OriginalityType.month);
logger.info("月精选任务完成");
logger.info("IdeaSelectionReminderTask monthlySelectionRemind finish.");
}
public void doMonthly(String from, String to, OriginalityType type) {
DateTime fromDT = DateTime.parse(from, ISODateTimeFormat.dateElementParser());
//修正开始时间为所在周的周一
fromDT = getMondayDateTime(fromDT);
DateTime toDT = DateTime.parse(to, ISODateTimeFormat.dateElementParser());
int month = toDT.getMonthOfYear();
int quarter = (month - 1) / 3 + 1;
int year = toDT.getYear();
long fromTime = fromDT.getMillis();
long toTime = toDT.getMillis();
List<String> userIdList = ideaService.getUserIdList();
//todo 这里不管有没有待选项都生成记录,如果要求没有不能提醒,则需要在筛选接口里面做特殊处理,把没有待选项的提醒标记为完成
for (String userId : userIdList) {
IdeaSelectionReminder reminder =
buildReminder(userId, type, from, to, fromTime, toTime,
year, quarter, month);
isReminderService.save(reminder);
}
}
private IdeaSelectionReminder buildReminder(String userId, OriginalityType type, String from, String to,
long fromTime, long toTime, int year, int quarter, int month) {
IdeaSelectionReminder reminder = new IdeaSelectionReminder();
reminder.setUserId(userId);
reminder.setType(type);
reminder.setFrom(from);
reminder.setFromTime(fromTime);
reminder.setTo(to);
reminder.setToTime(toTime);
reminder.setYear(year);
reminder.setQuarter(quarter);
reminder.setMonth(month);
reminder.setFinished(false);
return reminder;
}
// 获取date所在周的周一的时间
private static DateTime getMondayDateTime(DateTime date) {
int dayOfWeek = date.getDayOfWeek();
return date.minusDays(dayOfWeek-1);
}
public static void main(String[] args) {
System.out.println(DateTime.now().getMonthOfYear());
System.out.println(DateTime.parse("2017-01-01", ISODateTimeFormat.dateElementParser()).minusMonths(3));
DateTime dt = DateTime.parse("2015-11-01", ISODateTimeFormat.dateElementParser());
System.out.println(getMondayDateTime(dt));
}
}
不积跬步无以至千里