利用Linux中的crontab實(shí)現(xiàn)分布式項(xiàng)目定時(shí)任務(wù)功能
認(rèn)識(shí)crond服務(wù)
1、crond是Linux用來(lái)定期執(zhí)行程序的命令。當(dāng)安裝完成操作系統(tǒng)之后,默認(rèn)便會(huì)啟動(dòng)此任務(wù)調(diào)度命令。crond命令每分鍾會(huì)定期檢查是否有要執(zhí)行的工作,如果有要執(zhí)行的工作便會(huì)自動(dòng)執(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、啟動(dòng)、停止、查看crond服務(wù):
①啟動(dòng):service crond start
②停止:service crond stop
③查看:service crond status
@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í)行開(kāi)始"+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é)束之前的沒(méi)有結(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);
}
}
}
}
像上面這樣寫(xiě)好定時(shí)任務(wù)的邏輯類
創(chuàng)建一個(gè)contab.txt
*/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'
里面這樣調(diào)用方法去執(zhí)行即可實(shí)現(xiàn)分布式項(xiàng)目的定時(shí)任務(wù)
上面即每30分鐘執(zhí)行一次
總結(jié)
以上所述是小編給大家介紹的利用Linux中的crontab實(shí)現(xiàn)分布式項(xiàng)目定時(shí)任務(wù)功能,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- Linux crontab定時(shí)任務(wù)配置方法(詳解)
- linux使用crontab實(shí)現(xiàn)PHP執(zhí)行計(jì)劃定時(shí)任務(wù)
- linux定時(shí)任務(wù)crontab 實(shí)現(xiàn)每秒執(zhí)行一次的方法
- Linux中crontab定時(shí)任務(wù)不執(zhí)行的原因
- Linux定時(shí)任務(wù)Crontab詳解(推薦)
- 詳細(xì)介紹Linux的定時(shí)任務(wù)crontab
- 詳解linux下利用crontab創(chuàng)建定時(shí)任務(wù)
- Linux中使用crontab命令啟用自定義定時(shí)任務(wù)實(shí)例
- Linux定時(shí)任務(wù)的設(shè)置及 crontab 配置指南
- linux如何利用crontab添加定時(shí)任務(wù)詳解
相關(guān)文章
Linux Shell腳本系列教程(三):變量和環(huán)境變量
這篇文章主要介紹了Linux Shell腳本系列教程(三):變量和環(huán)境變量,本文講解了普通變量、獲取字符串的長(zhǎng)度、環(huán)境變量等內(nèi)容,需要的朋友可以參考下2015-06-06
Shell命令批量殺死進(jìn)程的方法實(shí)現(xiàn)
本文主要介紹了Shell命令批量殺死進(jìn)程的方法實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06
Shell腳本實(shí)現(xiàn)監(jiān)控iptables規(guī)則是否被修改
這篇文章主要介紹了Shell腳本實(shí)現(xiàn)監(jiān)控iptables規(guī)則是否被修改,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下2014-12-12
shell腳本實(shí)現(xiàn)實(shí)時(shí)檢測(cè)文件變更
這篇文章主要介紹了shell腳本實(shí)現(xiàn)實(shí)時(shí)檢測(cè)文件變更,本文直接給出實(shí)現(xiàn)代碼和使用方法,以及svn下的實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-05-05
使用curl命令查看服務(wù)器端口開(kāi)放情況的方法
這篇文章主要介紹了如何使用curl命令查看服務(wù)器端口開(kāi)放情況的方法,文中通過(guò)代碼示例和圖文講解的非常詳細(xì),具有一定的參考價(jià)值,需要的朋友可以參考下2024-05-05

