欧美成人午夜免费全部完,亚洲午夜福利精品久久,а√最新版在线天堂,另类亚洲综合区图片小说区,亚洲欧美日韩精品色xxx

扣丁學堂Linux培訓簡述如何利用Linux中的crontab實現(xiàn)分布式項目定時任務功能

2019-10-08 11:22:07 5834瀏覽

crond是Linux用來定期執(zhí)行程序的命令,那么如何利用Linux中的crontab實現(xiàn)分布式項目定時任務功能呢?本篇文章扣丁學堂Linux培訓小編給大家主要介紹一下利用Linux中的crontab實現(xiàn)分布式項目定時任務,感興趣的小伙伴可以參考下。


扣丁學堂Linux培訓簡述如何利用Linux中的crontab實現(xiàn)分布式項目定時任務功能


認識crond服務


1、crond是Linux用來定期執(zhí)行程序的命令。當安裝完成操作系統(tǒng)之后,默認便會啟動此任務調(diào)度命令。crond命令每分鍾會定期檢查是否有要執(zhí)行的工作,如果有要執(zhí)行的工作便會自動執(zhí)行該工作。而Linux任務調(diào)度的工作主要分為以下兩類:


①系統(tǒng)執(zhí)行的工作:系統(tǒng)周期性所要執(zhí)行的工作,如備份系統(tǒng)數(shù)據(jù)、清理緩存

②個人執(zhí)行的工作:某個用戶定期要做的工作,例如每隔10分鐘檢查郵件服務器是否有新信,這些工作可由每個用戶自行設置



2、Crontab是UNIX系統(tǒng)下的定時任務觸發(fā)器,其使用者的權(quán)限記載在下列兩個文件中:


①/etc/cron.deny 該文件中所列的用戶不允許使用Crontab命令

②/etc/cron.allow 該文件中所列的用戶允許使用Crontab命令



3、/var/spool/cron/ 是所有用戶的crontab文件



4、啟動、停止、查看crond服務:


    ①啟動: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定時器執(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定時器執(zhí)行異常" + type, e);
      result.setMsg("topic定時器執(zhí)行異常" + type);
      result.setSuccess(false);
    }
    long t2 = System.currentTimeMillis();
    logger.error("topic定時器執(zhí)行結(jié)束"+type+",耗時="+(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é)束時間的話題,只跑一周
   */
  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);
      }
    }
  }
}


像上面這樣寫好定時任務的邏輯類 

創(chuàng)建一個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í)行即可實現(xiàn)分布式項目的定時任務  

上面即每30分鐘執(zhí)行一次


想要了解更多關(guān)于Linux開發(fā)方面內(nèi)容的小伙伴,請關(guān)注扣丁學堂Linux培訓官網(wǎng)、微信等平臺,扣丁學堂IT職業(yè)在線學習教育有專業(yè)的Linux講師為您指導,此外扣丁學堂老師精心推出的Linux視頻教程定能讓你快速掌握Linux從入門到精通開發(fā)實戰(zhàn)技能??鄱W堂Linux技術(shù)交流群:422345477。


扣丁學堂微信公眾號                          Python全棧開發(fā)爬蟲人工智能機器學習數(shù)據(jù)分析免費公開課直播間


      【關(guān)注微信公眾號獲取更多學習資料】         【掃碼進入Python全棧開發(fā)免費公開課】



查看更多關(guān)于“Linux培訓資訊”的相關(guān)文章>>

標簽: Linux培訓 Linux視頻教程 紅帽Linux視頻 Linux學習視頻 Linux入門視頻 紅帽RHCE/RHCSA考試

熱門專區(qū)

暫無熱門資訊

課程推薦

微信
微博
15311698296

全國免費咨詢熱線

郵箱:codingke@1000phone.com

官方群:148715490

北京千鋒互聯(lián)科技有限公司版權(quán)所有   北京市海淀區(qū)寶盛北里西區(qū)28號中關(guān)村智誠科創(chuàng)大廈4層
京ICP備2021002079號-2   Copyright ? 2017 - 2022
返回頂部 返回頂部