利用Linux中的crontab實現分布式項目定時任務,linuxcrontab項目

來源:互聯網
上載者:User

利用Linux中的crontab實現分布式項目定時任務,linuxcrontab項目

@Controller@RequestMapping("/task/topic")public class TopicQuartzController {protected Logger logger = LoggerFactory.getLogger(TopicQuartzController.class);@Autowiredprivate LiveTopicService liveTopicService;@RequestMapping("execute")@ResponseBodypublic CommonResult execute(HttpServletRequest request,HttpServletResponse response,String type){long t1 = System.currentTimeMillis();logger.error("topic定時器執行開始"+type);CommonResult result = new CommonResult();if(QlchatUtil.isEmpty(type)){result.setMsg("參數為空白");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("執行完成" + type);} catch (Exception e) {logger.error("topic定時器執行異常" + type, e);result.setMsg("topic定時器執行異常" + type);result.setSuccess(false);}long t2 = System.currentTimeMillis();logger.error("topic定時器執行結束"+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);}}}/** * 結束之前的沒有結束時間的話題,只跑一周 */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);}}}}像上面這樣寫好定時任務的邏輯類建立一個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'裡面這樣調用方法去執行即可實現分布式項目的定時任務 上面即每30分鐘執行一次

  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.