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

cn.dceast.platform.task.TaskContainer Maven / Gradle / Ivy

The newest version!
package cn.dceast.platform.task;

import java.lang.reflect.Method;
import java.util.List;
import java.util.Set;

import cn.dceast.platform.task.annotation.Init;
import cn.dceast.platform.task.config.TaskConfig;
import cn.dceast.platform.task.util.ClassScanner;
import cn.dceast.platform.task.util.StringUtil;
import org.quartz.Trigger;

import cn.dceast.platform.task.db.DBUtil;
import cn.dceast.platform.task.quartz.JobEntity;
import cn.dceast.platform.task.quartz.SchedulerUtil;
import cn.dceast.platform.task.quartz.data.DataFactory;
import cn.dceast.platform.task.quartz.listener.PlatformSchedulerListener;
import cn.dceast.platform.task.quartz.listener.platformJobListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class TaskContainer {
	private static Logger logger= LoggerFactory.getLogger(TaskContainer.class);


    /**
     * 启动容器
     */
    public static void start() {
        /**
         * 初始化任务调度器
         */
        SchedulerUtil.init();

        /**
         * 初始化配置
         */
        TaskConfig.init();

        //扫描@init注解 ,路径从环境变量中获得。
        String scanPackage = System.getenv("scanPackage");
        if (!StringUtil.isEmpty(scanPackage)) {
            String[] scanPackageArr = scanPackage.split(",");

            for (int i = 0; i < scanPackageArr.length; i++) {
                if (!StringUtil.isEmpty(scanPackageArr[i]))
                    initClass(scanPackageArr[i]);
            }
        } else {
//            initClass("");
        }

        /**
         * mongodb初始化
         */
        DBUtil.init();

        /**
         * 初始化数据工厂
         */
        DataFactory.init();

        /**
         * 添加任务调度监听器
         */
        SchedulerUtil.addSchedulerListener(new PlatformSchedulerListener());
        SchedulerUtil.addJobListener(new platformJobListener());

        /**
         * 添加job
         */

        List jobList = DataFactory.getInputJobInfoExecutor().getJobs();

        if (jobList != null && jobList.size() > 0) {
            for (JobEntity job : jobList) {
                job.setAppName(TaskConfig.appName);

                //status:null,normal,pause 需要加入task,其他只加入清单中
                //暂停的任务,仍然暂停

                if (job.getStatus() == null
                        || "".equals(job.getStatus())
                        || Trigger.TriggerState.NORMAL.name().equals(job.getStatus())
                        || Trigger.TriggerState.BLOCKED.name().equals(job.getStatus())
                        || Trigger.TriggerState.PAUSED.name().equals(job.getStatus())
                        ) {
                    SchedulerUtil.addJob(job);

                    if (Trigger.TriggerState.PAUSED.name().equals(job.getStatus())) {
                        SchedulerUtil.pauseJob(job.getJobId());
                    }
                }

            }
        }

        /**
         * 一切就绪后启动scheduler
         */
        SchedulerUtil.start();


    }

    /**
     * 停止容器
     */
    public static void shutdown() {
        SchedulerUtil.shutdown();
    }

    private static void initClass(String scannPackage) {
        ClassScanner classScanner = new ClassScanner();
        Set> set = classScanner.getClassOfPackage(scannPackage, true);
        logger.debug(scannPackage + "=============");
        for (Class cla : set) {
            Init init = cla.getAnnotation(Init.class);
            if (init != null) {
                try {
                    Method[] methods = cla.getMethods();
                    boolean isInit = false;
                    for (Method method : methods) {
                        Init initM = method.getAnnotation(Init.class);

                        if (initM != null) {
                            method.invoke(cla);
                            isInit = true;
                            break;
                        }
                    }

                    if (!isInit) {
                        cla.getMethod("init").invoke(cla);
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy