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

com.github.quartzwebui.service.strategy.SchedulerServiceStrategy Maven / Gradle / Ivy

The newest version!
/**
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.github.quartzwebui.service.strategy;

import com.github.quartzwebui.manager.web.QuartzWebUIManager;
import com.github.quartzwebui.service.JSONResult;
import com.github.quartzwebui.service.QuartzWebUIURL;
import com.github.quartzwebui.utils.Assert;
import com.github.quartzwebui.utils.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * Scheduler 业务处理策略
 * @author quxiucheng [[email protected]]
 */
public class SchedulerServiceStrategy implements ServiceStrategy {


    public JSONResult service(ServiceStrategyURL serviceStrategyURL, SchedulerServiceStrategyParameter parameter) {
        String url = serviceStrategyURL.getURL();
        // 列出信息
        if (QuartzWebUIURL.SchedulerURL.INFO.getURL().equals(url)) {
            return getInfo();
        }
        String schedulerName = parameter.getSchedulerName();
        // 具体操作
        // 启动
        if (QuartzWebUIURL.SchedulerURL.START.getURL().equals(url)) {
            return start(schedulerName);
        }
        // 延时启动
        if (QuartzWebUIURL.SchedulerURL.START_DELAYED.getURL().equals(url)) {
            String delayed = parameter.getDelayed();
            return startDelayed(schedulerName, delayed);
        }
        // 关闭
        if (QuartzWebUIURL.SchedulerURL.SHUTDOWN.getURL().equals(url)) {
            return shutdown(schedulerName);
        }
        // 等待job运行结束后关闭
        if (QuartzWebUIURL.SchedulerURL.SHUTDOWN_WAIT.getURL().equals(url)) {
            return shutdownWait(schedulerName);
        }

        // 404没有找到url
        return JSONResult.build(JSONResult.RESULT_CODE_ERROR, "404 not found");
    }

    /**
     * 实例化接口参数
     * @return 接口参数
     */
    public SchedulerServiceStrategyParameter newServiceStrategyParameterInstance() {
        return new SchedulerServiceStrategyParameter();
    }

    public JSONResult getInfo(){
        try {
            List> resultMapList = QuartzWebUIManager.getAllSchedulerInfo();
            return JSONResult.build(JSONResult.RESULT_CODE_SUCCESS, resultMapList);
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.build(JSONResult.RESULT_CODE_ERROR, e.getMessage());
        }
    }

    public JSONResult start(String schedulerName){
        try {
            Assert.notEmpty(schedulerName,"schedulerName can not be empty");
            QuartzWebUIManager.schedulerStart(schedulerName);
            return JSONResult.build(JSONResult.RESULT_CODE_SUCCESS, "ok");
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.build(JSONResult.RESULT_CODE_ERROR, e.getMessage());
        }
    }

    public JSONResult startDelayed(String schedulerName, String delayed) {
        try {
            Assert.notEmpty(schedulerName,"schedulerName can not be empty");
            if (StringUtils.isEmpty(delayed)) {
                delayed = "0";
            }
            Assert.isInteger(delayed,"delayed must be integer");
            QuartzWebUIManager.schedulerStart(schedulerName, Integer.parseInt(delayed));
            return JSONResult.build(JSONResult.RESULT_CODE_SUCCESS, "ok");
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.build(JSONResult.RESULT_CODE_ERROR, e.getMessage());
        }
    }

    public JSONResult shutdown(String schedulerName){
        try {
            Assert.notEmpty(schedulerName,"schedulerName can not be empty");
            QuartzWebUIManager.schedulerShutdown(schedulerName);
            return JSONResult.build(JSONResult.RESULT_CODE_SUCCESS, "ok");
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.build(JSONResult.RESULT_CODE_ERROR, e.getMessage());
        }
    }

    public JSONResult shutdownWait(String schedulerName){
        try {
            Assert.notEmpty(schedulerName,"schedulerName must not empty");
            QuartzWebUIManager.schedulerShutdown(schedulerName, true);
            return JSONResult.build(JSONResult.RESULT_CODE_SUCCESS, "ok");
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.build(JSONResult.RESULT_CODE_ERROR, e.getMessage());
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy