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进行管控

    京东技术
  • 相关阅读:
    POJ 1401 Factorial
    POJ 2407 Relatives(欧拉函数)
    POJ 1730 Perfect Pth Powers(唯一分解定理)
    POJ 2262 Goldbach's Conjecture(Eratosthenes筛法)
    POJ 2551 Ones
    POJ 1163 The Triangle
    POJ 3356 AGTC
    POJ 2192 Zipper
    POJ 1080 Human Gene Functions
    POJ 1159 Palindrome(最长公共子序列)
  • 原文地址:https://www.cnblogs.com/wely/p/6198732.html
Copyright © 2011-2022 走看看