2019-10-08 11:22:07 5854瀏覽
crond是Linux用來定期執(zhí)行程序的命令,那么如何利用Linux中的crontab實(shí)現(xiàn)分布式項(xiàng)目定時(shí)任務(wù)功能呢?本篇文章扣丁學(xué)堂Linux培訓(xùn)小編給大家主要介紹一下利用Linux中的crontab實(shí)現(xiàn)分布式項(xiàng)目定時(shí)任務(wù),感興趣的小伙伴可以參考下。
認(rèn)識crond服務(wù)
1、crond是Linux用來定期執(zhí)行程序的命令。當(dāng)安裝完成操作系統(tǒng)之后,默認(rèn)便會啟動此任務(wù)調(diào)度命令。crond命令每分鍾會定期檢查是否有要執(zhí)行的工作,如果有要執(zhí)行的工作便會自動執(zhí)行該工作。而Linux任務(wù)調(diào)度的工作主要分為以下兩類:
①系統(tǒng)執(zhí)行的工作:系統(tǒng)周期性所要執(zhí)行的工作,如備份系統(tǒng)數(shù)據(jù)、清理緩存
②個(gè)人執(zhí)行的工作:某個(gè)用戶定期要做的工作,例如每隔10分鐘檢查郵件服務(wù)器是否有新信,這些工作可由每個(gè)用戶自行設(shè)置
2、Crontab是UNIX系統(tǒng)下的定時(shí)任務(wù)觸發(fā)器,其使用者的權(quán)限記載在下列兩個(gè)文件中:
①/etc/cron.deny 該文件中所列的用戶不允許使用Crontab命令
②/etc/cron.allow 該文件中所列的用戶允許使用Crontab命令
3、/var/spool/cron/ 是所有用戶的crontab文件
4、啟動、停止、查看crond服務(wù):
①啟動:service crond start
②停止:service crond stop
③查看:service crond status
像上面這樣寫好定時(shí)任務(wù)的邏輯類
創(chuàng)建一個(gè)contab.txt
里面這樣調(diào)用方法去執(zhí)行即可實(shí)現(xiàn)分布式項(xiàng)目的定時(shí)任務(wù)
上面即每30分鐘執(zhí)行一次
@Controller
@RequestMapping("/task/topic")
public class TopicQuartzController {
protected Logger logger = LoggerFactory.getLogger(TopicQuartzController.class);
@Autowired
private LiveTopicService liveTopicService;
@RequestMapping("execute")
@ResponseBody
public CommonResult execute(HttpServletRequest request,HttpServletResponse response,String type){
long t1 = System.currentTimeMillis();
logger.error("topic定時(shí)器執(zhí)行開始"+type);
CommonResult result = new CommonResult();
if(QlchatUtil.isEmpty(type)){
result.setMsg("參數(shù)為空");
result.setSuccess(false);
return result;
}
try {
switch (type) {
case "autoEndTopic":
this.autoEndTopic();
break;
case "oneWeek":
this.endTopicOneWeek();
break;
default:
break;
}
result.setSuccess(true);
result.setMsg("執(zhí)行完成" + type);
} catch (Exception e) {
logger.error("topic定時(shí)器執(zhí)行異常" + type, e);
result.setMsg("topic定時(shí)器執(zhí)行異常" + type);
result.setSuccess(false);
}
long t2 = System.currentTimeMillis();
logger.error("topic定時(shí)器執(zhí)行結(jié)束"+type+",耗時(shí)="+(t2 - t1) + "ms");
return result;
}
private void autoEndTopic(){
String sql = "SELECT id_ topicId FROM skg_live_topic lt WHERE lt.`status_` = 'beginning' AND lt.end_time_ IS NOT NULL AND lt.`end_time_` < NOW()";
JdbcTemplate jdbcTemplate = SpringHelper.getBean(JdbcTemplate.class);
List<Map<String, Object>> resultMap = jdbcTemplate.queryForList(sql);
for (Map<String, Object> map : resultMap) {
String topicId = String.valueOf(map.get("topicId"));
try {
LiveTopicPo liveTopicPo = liveTopicService.loadCache(topicId);
liveTopicService.endTopic(liveTopicPo, liveTopicPo.getCreateBy());
}catch (Exception e){
logger.error("autoEndTopic異常" + topicId, e);
}
}
}
/**
* 結(jié)束之前的沒有結(jié)束時(shí)間的話題,只跑一周
*/
private void endTopicOneWeek(){
String sql = "SELECT id_ topicId FROM skg_live_topic lt WHERE lt.`status_` = 'beginning' AND lt.end_time_ IS NULL AND lt.start_time_ <= (NOW() - interval 48 hour)";
JdbcTemplate jdbcTemplate = SpringHelper.getBean(JdbcTemplate.class);
List<Map<String, Object>> resultMap = jdbcTemplate.queryForList(sql);
for (Map<String, Object> map : resultMap) {
String topicId = String.valueOf(map.get("topicId"));
try {
LiveTopicPo liveTopicPo = liveTopicService.loadCache(topicId);
liveTopicService.endTopic(liveTopicPo, liveTopicPo.getCreateBy());
}catch (Exception e){
logger.error("autoEndTopic異常" + topicId, e);
}
}
}
}
*/30 * * * * curl 'http://10.47.161.40:8181/task/topic/execute.do?type=oneWeek'
*/30 * * * * curl 'http://10.47.161.40:8181/task/topic/execute.do?type=autoEndTopic'
想要了解更多關(guān)于Linux開發(fā)方面內(nèi)容的小伙伴,請關(guān)注扣丁學(xué)堂Linux培訓(xùn)官網(wǎng)、微信等平臺,扣丁學(xué)堂IT職業(yè)在線學(xué)習(xí)教育有專業(yè)的Linux講師為您指導(dǎo),此外扣丁學(xué)堂老師精心推出的Linux視頻教程定能讓你快速掌握Linux從入門到精通開發(fā)實(shí)戰(zhàn)技能。扣丁學(xué)堂Linux技術(shù)交流群:422345477。
【關(guān)注微信公眾號獲取更多學(xué)習(xí)資料】 【掃碼進(jìn)入Python全棧開發(fā)免費(fèi)公開課】