利用Linux中的crontab实现分布式项目定时任务功能

所属分类: 脚本专栏 / linux shell 阅读数: 390
收藏 0 赞 0 分享

认识crond服务

    1、crond是Linux用来定期执行程序的命令。当安装完成操作系统之后,默认便会启动此任务调度命令。crond命令每分锺会定期检查是否有要执行的工作,如果有要执行的工作便会自动执行该工作。而Linux任务调度的工作主要分为以下两类:

  ①系统执行的工作:系统周期性所要执行的工作,如备份系统数据、清理缓存

  ②个人执行的工作:某个用户定期要做的工作,例如每隔10分钟检查邮件服务器是否有新信,这些工作可由每个用户自行设置

 2、Crontab是UNIX系统下的定时任务触发器,其使用者的权限记载在下列两个文件中:

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

以上所述是小编给大家介绍的利用Linux中的crontab实现分布式项目定时任务功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

更多精彩内容其他人还在看

linux whatis与whatis database 使用及查询方法(man使用实例)

在学习man命令时候,估计很多朋友都发现有man –f ,man –k 参数,可以查出很多数据。这些有的与man手册页相同,有些不是属于手册页的。它们数据怎么收集来的,并且whatis是怎么样工作的
收藏 0 赞 0 分享

linux dev 常见特殊设备介绍与应用(loop,null,zero,full,random)

这篇文章主要介绍了linux dev 常见特殊设备介绍与应用(loop,null,zero,full,random),需要的朋友可以参考下
收藏 0 赞 0 分享

linux shell命令快捷获得系统帮助(一)[man-pages定义规范]

linux命令帮助,一般有2种,命令自身代码里面带有使用帮助说明,这种一般很精简,太长了,程序自身的大小以及日常维护不方便。还有一种,就是带有帮助文件,类似windows的chm格式文件。下面我说下这2种怎么样查阅
收藏 0 赞 0 分享

linux shell实现随机数几种方法分享(date,random,uuid)

这篇文章主要介绍了linux shell实现随机数多种方法(date,random,uuid),需要的朋友可以参考下
收藏 0 赞 0 分享

linux shell 脚本实现tcp/upd协议通讯(重定向应用)

这篇文章主要介绍了linux shell 脚本实现tcp/upd协议通讯(重定向应用),需要的朋友可以参考下
收藏 0 赞 0 分享

linux shell数据重定向(输入重定向与输出重定向)详细分析

这篇文章主要介绍了linux shell数据重定向(输入重定向与输出重定向)详细分析,需要的朋友可以参考下
收藏 0 赞 0 分享

linux shell 管道命令(pipe)使用及与shell重定向区别

这篇文章主要介绍了linux shell 管道命令(pipe)使用及与shell重定向区别,需要的朋友可以参考下
收藏 0 赞 0 分享

Linux下使用tcpdump抓包的实现方法

tcpdump是Linux下面的一个开源的抓包工具,和Windows下面的wireshark抓包工具一样, 支持抓取指定网口、指定目的地址、指定源地址、指定端口、指定协议的数据。这篇文章主要介绍了Linux下使用tcpdump抓包的实现方法,需要的朋友可以参考下
收藏 0 赞 0 分享

零基础入门篇之Linux及Arm-Linux程序开发笔记

这篇文章主要介绍了零基础入门篇之Linux及Arm-Linux程序开发笔记,需要的朋友可以参考下
收藏 0 赞 0 分享

Linux 中 CURL常用命令详解

这篇文章主要介绍了Linux 中 CURL常用命令详解,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多