通用任务定时执行实现

作者: 低至一折起 | 来源:发表于2018-04-11 20:33 被阅读18次

    Quartz触发器配置

    从触发器中配置一个通用触发MainStarter

    <bean id="mainStarter" class="com.zb.timer.MainStarter" >
    </bean>
        
    <bean id="mainStarterDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject" ref="mainStarter" />
        <property name="targetMethod" value="run" />
        <property name="concurrent" value="false" />
    </bean>
    <bean id="mainStarterTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
        <property name="jobDetail" ref="mainStarterDetail" />
        <property name="startDelay" value="10000" />
        <property name="repeatCount" value="0" /> 
    </bean>
        
    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="mainStarterTrigger" />  
            </list>
        </property>
    </bean>
    

    统一任务处理

    MainStarter的实现

    主要是任务中心来执行的

    public class MainStarter{
    
        // 任务中心
        private TaskCenter taskCenter;
        
        public void setTaskCenter(TaskCenter taskCenter) {
            this.taskCenter = taskCenter;
        }
    
        public void run() {
            if(taskCenter==null) {
                taskCenter = new TaskCenterSupport();
            }
            taskCenter.run();
        }
    }
    
    public interface TaskCenter {
        // 加载任务
        public void loadTask(Task taskTracker);
        
        public List<Task> getTasks(); 
        
        public void run();
        
        public void terminate();
        
    }
    

    任务中心存储任务,任务 Task 为代码最终执行

    public abstract class Task extends Thread {
        private boolean isAlive = true;
    
        @Override
        public void run() {
            while (isAlive) {
                try {
                    doRun();
                } catch (Exception e) { 
                    logger.error("run task error.", e);
                }
            }
        }
    
        public void setAlive(boolean isAlive) {
            this.isAlive = isAlive;
        }
    
        // 子类业务需要实现此方法,方法中为具体业务
        protected abstract void doRun() throws Exception;
    }
    

    具体业务实现

    TaskCenter 的实现类 TaskCenterSupport

    public class TaskCenterSupport implements TaskCenter {
        private List<Task> tasks;
        
        public List<Task> getTasks() {
            return tasks;
        }
    
        public void loadTask(Task task) {
            if (tasks == null) {
                tasks = new ArrayList<>();
            }
            
            tasks.add(task);
        }
        
        public void run() {
            if(CollectionUtils.isNotEmpty(tasks)){
                for (Task task : tasks) {
                    if(task != null) {
                        task.setAlive(true);
                        task.start();
                    }
                }
            }
        }
        
        public void terminate() {
            if(CollectionUtils.isNotEmpty(tasks)){
                for (Task task : tasks) {
                    if(task != null) {
                        task.setAlive(false);
                    }
                }
                
                tasks.clear();
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    logger.error(e.getMessage(),e);
                    Thread.currentThread().interrupt();
                }
            }
        }
    }
    

    Task 的子类 ZBTask

    public class ZBTask extends Task{ 
        
        public ZBTask () {
            TaskCenter taskCenter = TaskCenterSingleton.getInstance();
            taskCenter.loadTask(this);
        }
        
        public void doRun() throws Exception {
            /* 具体业务实现,这里就能会定时执行,
             * 若有需要还可以在此使用休眠,
             * 这样只会把此线程挂起,不影响其他 Task 子类的执行
             */
        }
    
    }
    

    相关文章

      网友评论

        本文标题:通用任务定时执行实现

        本文链接:https://www.haomeiwen.com/subject/hcizhftx.html