xxl-job定时任务测试

配置执行器

xxl-job定时任务测试_第1张图片

启动相关xxl-job客户端服务后,会自动注册机器地址

xxl-job定时任务测试_第2张图片

简单任务

    /**
     * 简单任务
     *
     * @param params
     * @return
     */
    @XxlJob(value = "demoJob")
    public ReturnT demoJobHandler(String params) {
        log.info("我是 jeecg-system 服务里的定时任务 demoJob,我执行了...............................");
        return ReturnT.SUCCESS;
    }

xxl-job定时任务测试_第3张图片

启动任务

分片任务

    /**
     * 2、分片广播任务
     */
    @XxlJob("shardingJobHandler")
    public ReturnT shardingJobHandler(String param) throws Exception {

        // 分片参数
        ShardingUtil.ShardingVO shardingVO = ShardingUtil.getShardingVo();
        log.info("分片参数:当前分片序号 = {}, 总分片数 = {}", shardingVO.getIndex(), shardingVO.getTotal());

        // 业务逻辑
        for (int i = 0; i < shardingVO.getTotal(); i++) {
            if (i == shardingVO.getIndex()) {
                log.info("第 {} 片, 命中分片开始处理", i);
            } else {
                log.info("第 {} 片, 忽略", i);
            }
        }

        return ReturnT.SUCCESS;
    }

 分片任务需要多个xxl-job客户端服务,这样才能达到并行分片处理定时任务的优势。这里我们利用idea的相关设置去启动同一个demo客户端服务;但记得启动前修改下服务端口

xxl-job定时任务测试_第4张图片

 xxl-job定时任务测试_第5张图片

xxl-job定时任务测试_第6张图片

运行定时任务

xxl-job定时任务测试_第7张图片

xxl-job定时任务测试_第8张图片

第一台机

xxl-job定时任务测试_第9张图片

第二台机

 第三台机

 第四台机

 第五台机

命令任务

    /**
     * 3、命令行任务
     *
     * 输入参数:ipconfig /all
     */
    @XxlJob("commandJobHandler")
    public ReturnT commandJobHandler(String param) throws Exception {
        String command = param;
        int exitValue = -1;

        BufferedReader bufferedReader = null;
        try {
            // command process
            Process process = Runtime.getRuntime().exec(command);
            BufferedInputStream bufferedInputStream = new BufferedInputStream(process.getInputStream());
            bufferedReader = new BufferedReader(new InputStreamReader(bufferedInputStream));

            // command log
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                log.info(line);
            }

            // command exit
            process.waitFor();
            exitValue = process.exitValue();
        } catch (Exception e) {
            log.info(e.getMessage(),e);
        } finally {
            if (bufferedReader != null) {
                bufferedReader.close();
            }
        }

        if (exitValue == 0) {
            return IJobHandler.SUCCESS;
        } else {
            return new ReturnT(IJobHandler.FAIL.getCode(), "command exit value(" + exitValue + ") is failed");
        }
    }

xxl-job定时任务测试_第10张图片

xxl-job定时任务测试_第11张图片

xxl-job定时任务测试_第12张图片 跨平台Http任务

    /**
     * 4、跨平台Http任务
     *
     * 输入参数:
     * url: https://www.baidu.com
     * method: get
     * data: content
     */
    @XxlJob("httpJobHandler")
    public ReturnT httpJobHandler(String param) throws Exception {
        String[] methodArray=new String[]{"GET","POST"};
        int okState=200;
        // param parse
        if (param == null || param.trim().length() == 0) {
            log.info("param[" + param + "] invalid.");
            return ReturnT.FAIL;
        }
        String[] httpParams = param.split("\n");
        String url = null;
        String method = null;
        String data = null;
        for (String httpParam : httpParams) {
            if (httpParam.startsWith("url:")) {
                url = httpParam.substring(httpParam.indexOf("url:") + 4).trim();
            }
            if (httpParam.startsWith("method:")) {
                method = httpParam.substring(httpParam.indexOf("method:") + 7).trim().toUpperCase();
            }
            if (httpParam.startsWith("data:")) {
                data = httpParam.substring(httpParam.indexOf("data:") + 5).trim();
            }
        }

        // param valid
        if (url == null || url.trim().length() == 0) {
            log.info("url[" + url + "] invalid.");
            return ReturnT.FAIL;
        }
        if (method == null || !Arrays.asList(methodArray).contains(method)) {
            log.info("method[" + method + "] invalid.");
            return ReturnT.FAIL;
        }

        // request
        HttpURLConnection connection = null;
        BufferedReader bufferedReader = null;
        try {
            // connection
            URL realUrl = new URL(url);
            connection = (HttpURLConnection) realUrl.openConnection();

            // connection setting
            connection.setRequestMethod(method);
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setUseCaches(false);
            connection.setReadTimeout(5 * 1000);
            connection.setConnectTimeout(3 * 1000);
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
            connection.setRequestProperty("Accept-Charset", "application/json;charset=UTF-8");

            // do connection
            connection.connect();

            // data
            if (data != null && data.trim().length() > 0) {
                DataOutputStream dataOutputStream = new DataOutputStream(connection.getOutputStream());
                dataOutputStream.write(data.getBytes("UTF-8"));
                dataOutputStream.flush();
                dataOutputStream.close();
            }

            // valid StatusCode
            int statusCode = connection.getResponseCode();
            if (statusCode != okState) {
                throw new RuntimeException("Http Request StatusCode(" + statusCode + ") Invalid.");
            }

            // result
            bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
            StringBuilder result = new StringBuilder();
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                result.append(line);
            }
            String responseMsg = result.toString();

            log.info(responseMsg);
            return ReturnT.SUCCESS;
        } catch (Exception e) {
            log.info(e.getMessage(),e);
            return ReturnT.FAIL;
        } finally {
            try {
                if (bufferedReader != null) {
                    bufferedReader.close();
                }
                if (connection != null) {
                    connection.disconnect();
                }
            } catch (Exception e2) {
                log.info(e2.getMessage(),e2);
            }
        }

    }

xxl-job定时任务测试_第13张图片

 xxl-job定时任务测试_第14张图片

 由于我这个环境是内网,没有外网,报如下错误,暂时无法验证,可自行参考流程验证

xxl-job定时任务测试_第15张图片

 生命周期任务

    /**
     * 5、生命周期任务示例:任务初始化与销毁时,支持自定义相关逻辑;
     */
    @XxlJob(value = "demoJobHandler2", init = "init", destroy = "destroy")
    public ReturnT demoJobHandler2(String param) throws Exception {
        log.info("XXL-JOB, Hello World.");
        return ReturnT.SUCCESS;
    }

    public void init() {
        log.info("init");
    }

    public void destroy() {
        log.info("destory");
    }

xxl-job定时任务测试_第16张图片

xxl-job定时任务测试_第17张图片 

 

你可能感兴趣的:(java,xxl-job)