当前位置: 移动技术网 > 网络运营>服务器>Linux > 利用Linux中的crontab实现分布式项目定时任务功能

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

2018年01月25日  | 移动技术网网络运营  | 我要评论

认识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实现分布式项目定时任务功能,希望对大家有所帮助

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网