zoukankan      html  css  js  c++  java
  • 大规模Schedule任务实现方案

    package com.itlong.bjxizhan.support.web.job.base;
    
    import com.itlong.bjxizhan.common.DbContext;
    import com.itlong.bjxizhan.domain.pojo.Task;
    import com.itlong.bjxizhan.support.web.service.StandardTaskService;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    import java.util.List;
    
    /**
     * Created by shenhongxi on 2016/7/12.
     */
    public class JobRunnable implements Runnable {
    
        private static final Logger log = LoggerFactory.getLogger(JobRunnable.class);
    
        private StandardTaskService standardTaskService;
    
        private List<Task> tasks;
    
        private String dbKey;
    
        private String tableIndex;
    
        @Override
        public void run() {
            if (tasks != null) {
                try {
                    DbContext.setDbKey(dbKey);
                    DbContext.setTableIndex(tableIndex);
                    for (Task task : tasks) {
                        task.setTableIndex(tableIndex);
    
                        // 1. 一个job的多个实例,谁先成功锁定任务,谁先处理任务,若处理失败则解锁任务
                        // 2. 对于1中解锁失败的,要利用另外的job来专门进行解锁
                        // 3. 将任务分成几批,并行处理
                        // 4. 这些任务的子任务分批串行处理,同样有锁定-处理-失败解锁
                        // 5. 对于4中解锁失败的,同样要利用另外的job来专门进行解锁
                        boolean locked = standardTaskService.lock(task);
                        if (!locked) continue;
    
                        boolean result = standardTaskService.process(task);
    
                        standardTaskService.finished(result, task);
                    }
                } catch (Exception e) {
                    log.error("Do task error", e);
                    throw new RuntimeException("Do task error");
                }
            }
        }
    
        public List<Task> getTasks() {
            return tasks;
        }
    
        public void setTasks(List<Task> tasks) {
            this.tasks = tasks;
        }
    
        public StandardTaskService getStandardTaskService() {
            return standardTaskService;
        }
    
        public void setStandardTaskService(StandardTaskService standardTaskService) {
            this.standardTaskService = standardTaskService;
        }
    
        public String getTableIndex() {
            return tableIndex;
        }
    
        public void setTableIndex(String tableIndex) {
            this.tableIndex = tableIndex;
        }
    
        public String getDbKey() {
            return dbKey;
        }
    
        public void setDbKey(String dbKey) {
            this.dbKey = dbKey;
        }
    }
    

     ***:org.quartz.Scheduler提供了start, bystand等方法可以对Scheduler进行管控

    京东技术
  • 相关阅读:
    关于游戏分布式或者多服管理的想法
    surfaceView
    ackerman递归
    netbeans环境的建立
    copy-浅及深的复制操作
    使用VMware安装CentOS6.8详细教程
    Python在线资源优先级排序
    Python导入模块,Python import用法
    编码
    Python清屏命令
  • 原文地址:https://www.cnblogs.com/wely/p/6198732.html
Copyright © 2011-2022 走看看