濮阳杆衣贸易有限公司

主頁 > 知識(shí)庫 > 利用Linux中的crontab實(shí)現(xiàn)分布式項(xiàng)目定時(shí)任務(wù)功能

利用Linux中的crontab實(shí)現(xiàn)分布式項(xiàng)目定時(shí)任務(wù)功能

熱門標(biāo)簽:電銷機(jī)器人加盟多少錢 貴陽400電話到哪里去辦理 天津智能外呼系統(tǒng)排名 400電話申請(qǐng)找 宿松高德地圖標(biāo)注 4層電梯外呼控制系統(tǒng)設(shè)計(jì) 網(wǎng)絡(luò)電話外呼系統(tǒng)撥號(hào)軟件 汨羅代理外呼系統(tǒng) 申請(qǐng)400電話有什么用

認(rèn)識(shí)crond服務(wù)

    1、crond是Linux用來定期執(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)度的工作主要分為以下兩類:

 ?、傧到y(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í)行開始"+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);
    ListMapString, Object>> resultMap = jdbcTemplate.queryForList(sql);
    for (MapString, 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);
    ListMapString, Object>> resultMap = jdbcTemplate.queryForList(sql);
    for (MapString, 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);
      }
    }
  }
}

像上面這樣寫好定時(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ì)大家有所幫助,如果大家有任何疑問請(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ù)詳解

標(biāo)簽:海北 臨沂 撫州 連云港 廣東 烏蘭察布 贛州 昌都

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《利用Linux中的crontab實(shí)現(xiàn)分布式項(xiàng)目定時(shí)任務(wù)功能》,本文關(guān)鍵詞  利用,Linux,中的,crontab,實(shí)現(xiàn),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《利用Linux中的crontab實(shí)現(xiàn)分布式項(xiàng)目定時(shí)任務(wù)功能》相關(guān)的同類信息!
  • 本頁收集關(guān)于利用Linux中的crontab實(shí)現(xiàn)分布式項(xiàng)目定時(shí)任務(wù)功能的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    奎屯市| 舞钢市| 安多县| 班玛县| 敦化市| 温泉县| 襄城县| 深圳市| 郧西县| 云林县| 大冶市| 那坡县| 确山县| 凤山县| 铜川市| 汉寿县| 乌鲁木齐市| 胶南市| 昌吉市| 桃园市| 保山市| 鱼台县| 丘北县| 乾安县| 万荣县| 湘潭县| 马龙县| 海宁市| 甘孜县| 调兵山市| 招远市| 卓资县| 云和县| 乡城县| 乡宁县| 沾化县| 潞城市| 德州市| 凌海市| 山丹县| 华池县|