P3 定时器实战-计算优先级
程序员文章站
2022-06-09 10:53:39
...
package life.majiang.community.cache;
import lombok.Data;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
@Component
@Data
public class HotTagCache {
private Map<String,Integer> tag = new HashMap<>();
}
package life.majiang.community.schedule;
import life.majiang.community.cache.HotTagCache;
import life.majiang.community.dto.QuestionDTO;
import life.majiang.community.service.QuestionService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
/**
* 热门标签定时器,此处是测试定时任务(每隔5秒打印日志)
* 注意:需在main方法的那个类上添加@EnableScheduling注解
*/
@Component
@Slf4j
public class HotTagTasks {
private List<QuestionDTO> list = null;
@Autowired
private QuestionService questionService;
@Autowired
private HotTagCache hotTagCache;
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
@Scheduled(fixedRate = 5000)
//@Scheduled(cron = "0 0 1 * * *")//每天凌晨1点执行
public void hotTagSchedule() {
int offset = 0;
int limit = 5;
String search=null;
log.info("hotTagSchedule start {}", dateFormat.format(new Date()));
HashMap<String, Integer> priorities = new HashMap<>();
while (offset ==0 || list.size()==limit){
list = questionService.list(search, offset, limit);
for (QuestionDTO questionDTO : list) {
String tag1 = questionDTO.getTag();
String[] tags = StringUtils.split(tag1, ",");
for (String tag : tags) {
Integer priority = priorities.get(tag);
if (priority != null) {
priorities.put(tag,priority+5+questionDTO.getCommentCount());
}else{
priorities.put(tag,5+questionDTO.getCommentCount());
}
}
log.info("list question :{}",questionDTO.getId());
}
offset +=limit;
}
hotTagCache.setTag(priorities);
hotTagCache.getTag().forEach(
(k,v) -> {
System.out.print(k);
System.out.print(":");
System.out.print(v);
System.out.println();
}
);
log.info("hotTagSchedule stop {}", dateFormat.format(new Date()));
}
}
运行结果:
因为StringUtils的包导错,导致以下错误,应该导入(import org.apache.commons.lang3.StringUtils;)下的包
2020-03-22 11:02:13.593 ERROR 7796 --- [ scheduling-1] o.s.s.s.TaskUtils$LoggingErrorHandler : Unexpected error occurred in scheduled task
java.lang.NullPointerException: null
at life.majiang.community.schedule.HotTagTasks.hotTagSchedule(HotTagTasks.java:51) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_161]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_161]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_161]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_161]
at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:84) ~[spring-context-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
下一篇: Oracle delete语句调优一例
推荐阅读