Use crontab in Linux to implement scheduled tasks for distributed projects.

Source: Internet
Author: User

Use crontab in Linux to implement scheduled tasks for distributed projects.

@ 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 set The timer execution starts "+ type); CommonResult result = new CommonResult (); if (QlchatUtil. isEmpty (type) {result. setMsg ("parameter is blank"); 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 ("execution completed" + type);} catch (Exception e) {logger. error ("topic timer execution exception" + type, e); result. setMsg ("Topic timer execution exception" + type); result. setSuccess (false);} long t2 = System. currentTimeMillis (); logger. error ("topic timer execution ended" + type + ", time consumed =" + (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 exception" + topicId, e) ;}}/ *** no end time before the end of the topic, only one week */priva Te 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. value Of (map. get ("topicId"); try {LiveTopicPo liveTopicPo = liveTopicService. loadCache (topicId); liveTopicService. endTopic (liveTopicPo, liveTopicPo. getCreateBy ();} catch (Exception e) {logger. error ("autoEndTopic exception" + topicId, e);}}creates A 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 '. In this way, you can call the method to execute the scheduled task of the Distributed Project. The task is executed every 30 minutes.

  

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.