zoukankan      html  css  js  c++  java
  • 利用Linux中的crontab实现分布式项目定时任务

    @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定时器执行开始"+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分钟执行一次
    

      

  • 相关阅读:
    BeautifulSoup模块
    爬取校花网视频
    爬虫基本原理
    python学习笔记-50 使用SQLAlchemy
    python学习笔记-49 使用MySQL
    PTA天梯 L3-007 天梯地图
    VS2013 创建ASP.NET MVC 4.0 未指定的错误(异常来自HRESULT: 0x80004005(e_fail))
    动态规划--新手
    文件上传绕过
    C# → 数据库
  • 原文地址:https://www.cnblogs.com/a347911/p/8310307.html
Copyright © 2011-2022 走看看