All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.cudoy.framework.schedule.manager.SchedulerManagerImpl Maven / Gradle / Ivy

package com.cudoy.framework.schedule.manager;

import com.cudoy.framework.core.annotation.RsSchedule;
import com.cudoy.framework.core.manager.SchedulerManager;
import com.cudoy.framework.core.support.RunMode;
import com.cudoy.framework.core.utils.StringUtils;
import com.cudoy.framework.schedule.trigger.NormalTrigger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledFuture;

public class SchedulerManagerImpl implements SchedulerManager {

    private static Logger logger = LoggerFactory.getLogger(SchedulerManagerImpl.class);

    @Autowired
    private DefaultListableBeanFactory beanFactory;

    private ScheduledTaskRegistrar taskRegistrar;

    public ScheduledTaskRegistrar getTaskRegistrar() {
        return taskRegistrar;
    }

    public void setTaskRegistrar(Object taskRegistrar) {
        this.taskRegistrar = (ScheduledTaskRegistrar)taskRegistrar;
    }

    public SchedulerManagerImpl(){}

    public SchedulerManagerImpl(ScheduledTaskRegistrar taskRegistrar){
        this.taskRegistrar = taskRegistrar;
    }

    private Map> taskFutures = new ConcurrentHashMap>();

    //初始化
    public void init(){
        //获取本地代码任务
        Map temp = beanFactory.getBeansWithAnnotation(RsSchedule.class);
        Set keys = temp.keySet();
        for(String k : keys) {
            Object object = temp.get(k);
            if(object instanceof Runnable) {
                RsSchedule rsSchedule = object.getClass().getAnnotation(RsSchedule.class);
                String key = rsSchedule.value();
                if(StringUtils.isEmpty(key)){
                    key = k;// RsSchedule 没有标记id,则使用spring默认的bean name
                }
                start(key, rsSchedule);
            }else{
                logger.warn("RsSchedule[" + k + "] " + object.getClass() + " has not implemented Runnable.");
            }
        }
    }

    public boolean isRunning(String taskId){
        return taskFutures.get(taskId) != null;
    }

    public boolean cancel(String taskId){
        ScheduledFuture future = taskFutures.get(taskId);
        boolean canceled = false;
        if (future != null){
            canceled = future.cancel(true);
            if(canceled) {
                taskFutures.remove(taskId);
            }
        }
        return canceled;
    }

    public boolean start(String taskId, String cron, Runnable runnable){
        if (taskFutures.containsKey(taskId)){
            return true;
        }
        TaskScheduler scheduler = taskRegistrar.getScheduler();
        ScheduledFuture future = scheduler.schedule(runnable, new CronTrigger(cron));
        if(future != null) {
            taskFutures.put(taskId, future);
            return true;
        }
        return false;
    }

    public boolean start(String taskId, String mode, long period, Runnable runnable){
        if (taskFutures.containsKey(taskId)){
            return true;
        }
        TaskScheduler scheduler = taskRegistrar.getScheduler();
        RunMode m = RunMode.getMode(mode);
        ScheduledFuture future = null;
        if(m == RunMode.CUSTOM) {
        }else if(m == RunMode.NONE){
        }else{
            future = scheduler.schedule(runnable, new NormalTrigger(m, period));
        }
        if(future != null) {
            taskFutures.put(taskId, future);
            return true;
        }
        return false;
    }

    //启动任务
    public void start(String taskId, RsSchedule schedule){
        if (taskFutures.containsKey(taskId)){
            return;
        }
        TaskScheduler scheduler = taskRegistrar.getScheduler();
        Object object = beanFactory.getBean(taskId);
        if(!(object instanceof Runnable)) {
            return;
        }
        Runnable runnable = (Runnable) object;
        ScheduledFuture future = null;
        if(schedule.mode() == RunMode.CUSTOM) {
            future = scheduler.schedule(runnable, new CronTrigger(schedule.cron()));
        }else if(schedule.mode() == RunMode.NONE){
        }else{//
            future = scheduler.schedule(runnable, new NormalTrigger(schedule));
        }
        if(future != null) {
            taskFutures.put(taskId, future);
        }
    }


    //是否已初始化
    public boolean inited()
    {
        return this.taskRegistrar != null && this.taskRegistrar.getScheduler() != null;
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy