不想 CRUD 干到老,就来看看这篇 OOM 排查的实战案例!
公众号程序猿DD
共 1478字,需浏览 3分钟
·
2020-09-16 23:27
点击上方蓝色“程序猿DD”,选择“设为星标”
回复“资源”获取独家整理的学习资料!
作者:三国梦回
来源:cnblogs.com/grey-wolf/p/9179113.html
二、程序大体逻辑
1、job接口定义:
/**
* desc:
* 造数据的job,可按表来划分。一个表一个job
* @author :
* creat_date: 2018/6/11 0011
* creat_time: 14:46
**/
public interface DataProduceJob {
/**
* job的初始化
* @param date
*/
void jobInit(Date date);
/**
* 具体的job运行细节
*/
void jobDetail(Integer recordNum);
}
@Component
public class TopicWebsiteJob extends BaseJob implements DataProduceJob {
@Autowired
private TopicWebsiteMapper topicWebsiteMapper;
private Date date;
Random random = new Random();
private ListtopicWebsites;
/**
* 当前job执行时的时间,会作为创建时间写入数据库表
*
* @param date
*/
@Override
public void jobInit(Date date) {
this.date = date;
topicWebsites = topicWebsiteMapper.selectAll();
}
@Override
public void jobDetail() {
for (TopicWebsite website : topicWebsites) {
for (int i = 0; i < 5; i++) {
TopicWebsite topicWebsite = new TopicWebsite();
topicWebsite.setWebsiteName(website.getWebsiteName());
topicWebsite.setIconUrl(website.getIconUrl());
topicWebsite.setHotValue((long) random.nextInt(6354147));
//设置时间
topicWebsite.setCreateTime(date);
topicWebsiteMapper.insert(topicWebsite);
}
}
}
}
2、job的历史数据初始化器
package com..datavisual.quartz.init;
/**
* desc:
* 用于造初始化数据
* @author :
* creat_date: 2018/6/11 0011
* creat_time: 14:29
**/
public interface Initer {
/**
* 具体的初始化逻辑,可参考
* @return 成功或失败
*/
Boolean init();
}
/**
* desc:
*
* @author:
* creat_date: 2018/6/11 0011
* creat_time: 14:28
**/
@Component
public class TopicWebsiteIniter implements Initer {
@Autowired
private TopicWebsiteJob job;
@Override
public Boolean init() {
DateTime now = DateTime.now();
//日期循环,30天
for (int a = -29; a < 1; a++) {
for (int b = 0; b < 24; b++) {
int minutes = (int) (Math.random() * 60);
Date date = com.ceiec.datavisual.quartz.DateUtils.getNeedTime(b, minutes, 0, a);
if (a == 0 && date.after(now.toDate())) {
} else {
job.jobInit(date);
job.jobDetail(360);
}
}
}
return true;
}
}
3、目前为止,运行正常?
4、加上定时触发机制
@Component
public class TopicWebsiteScheduler implements DataProduceScheduler {
private static final Logger logger = LoggerFactory.getLogger(TopicWebsiteScheduler.class);
@Autowired
private TopicWebsiteJob job;
@Override
@Scheduled(cron = "0/10 * * * * ?}")
public Boolean schedule() {
logger.info("start...");
job.jobInit(new Date());
job.jobDetail(1);
return true;
}
}
5、问题出来了
三、总结
往期推荐
最后,推荐一个专注分享后端面试要点的公众号「后端面试那些事儿」,置顶标星。每日一篇常问的面试问题,秀的一批~扫描下方二维码关注!
评论