Files
klp-oa/ruoyi-job/src/main/java/com/ruoyi/job/service/SampleService.java
konbai 3fd2d9681d sync: 同步 RuoYi-Vue-Plus(v4.4.0) 更新
重大更新
    [重大更新] 优化支持 oss 私有库功能(数据库字段改动) #cd9c3c3f
    [重大更新] 连接池由 druid 修改为 hikari 更新相关配置(原因可看文档) #1f42bd3d
    [重大更新] 移除 tlog(不支持UI界面 使用的人太少)建议使用 skywalking
    [重大更新] 增加 skywalking 集成 默认注释不开启(使用看文档)

依赖升级
    update springboot 2.7.5 => 2.7.6
    update springboot-admin 2.7.6 => 2.7.7
    update satoken 1.31.0 => 1.33.0
    update spring-doc 1.6.12 => 1.6.13
    update easyexcel 3.1.1 => 3.1.3
    update hutool 5.8.8 => 5.8.10
    update redisson 3.17.7 => 3.18.0
    update lock4j 2.2.2 => 2.2.3
    update s3-adk 1.12.324 => 1.12.349
    update mysql-docker 8.0.29 => 8.0.31 功能更新
    update 优化 oss 云厂商增加 华为obs关键字
    update 优化 冗余的三元表达式
    update 优化 重置时取消部门选中
    update 优化 新增返回警告消息提示
    update 优化 hikari 参数顺序 最常用的放上面 删除无用 druid 监控页面
    update 优化 p6spy 排除健康检查 sql 执行记录
    update 优化 Dockerfile 创建目录命令
    update 优化 将空‘catch’块形参重命名为‘ignored’
    update 优化 使用本地缓存优化 excel 导出 数据量大字典转换慢问题
    update 优化 字典转换实现去除字符串查找拼接优化效率
    update 优化 减小腾讯短信引入jar包的体积
    update 消除Vue3控制台出现的警告信息
    update 忽略不必要的属性数据返回
    update 替换 mysql-jdbc 最新坐标

新增功能
    add 新增 junit5 单元测试案例 #6e8ef308
    add 增加 sys_oss_config access_policy 桶权限类型字段
    add 增加 4.3-4.4 更新 sql 文件
    add 新增 字典数据映射注解 #da94e898
    add 增加 RedisUtils 获取缓存Map的key列表

问题修复
    fix 修复 上传png透明图片 生成头像透明部分变成黑色
    fix 修复 sqlserver sql文件 重复主键数据问题
    fix 修复 sqlserver 特定情况下报 ssl 证书问题 默认关闭 ssl 认证
    fix 修复 table中更多按钮切换主题色未生效修复问题
    fix 修复 菜单激活无法修改其填充颜色去除某些svg图标的fill="#bfbfbf"属性
    fix 修复 使用缓冲流 导致上传异常问题
    fix 修复 过滤器链使用IoUtil.read方法导致request流关闭
    fix 修复 Log注解GET请求记录不到参数问题
    fix 修复 某些特性的环境生成代码变乱码TXT文件问题
    fix 修复 开启TopNav没有子菜单隐藏侧边栏
    fix 修复 回显数据字典数组异常问题

移除功能
    remove 移除过期 Anonymous 注解与其实现代码
    remove 移除 tlog(不支持UI界面 使用的人太少) 建议使用 skywalking
2023-01-07 21:53:27 +08:00

253 lines
8.0 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.ruoyi.job.service;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Arrays;
/**
* XxlJob开发示例Bean模式
* <p>
* 开发步骤:
* 1、任务开发在Spring Bean实例中开发Job方法
* 2、注解配置为Job方法添加注解 "@XxlJob(value="自定义jobhandler名称", init = "JobHandler初始化方法", destroy = "JobHandler销毁方法")"注解value值对应的是调度中心新建任务的JobHandler属性的值。
* 3、执行日志需要通过 "XxlJobHelper.log" 打印执行日志;
* 4、任务结果默认任务结果为 "成功" 状态,不需要主动设置;如有诉求,比如设置任务结果为失败,可以通过 "XxlJobHelper.handleFail/handleSuccess" 自主设置任务结果;
*
* @author xuxueli 2019-12-11 21:52:51
*/
@Slf4j
@Service
public class SampleService {
/**
* 1、简单任务示例Bean模式
*/
@XxlJob("demoJobHandler")
public void demoJobHandler() throws Exception {
XxlJobHelper.log("XXL-JOB, Hello World.");
for (int i = 0; i < 5; i++) {
XxlJobHelper.log("beat at:" + i);
}
// default success
}
/**
* 2、分片广播任务
*/
@XxlJob("shardingJobHandler")
public void shardingJobHandler() throws Exception {
// 分片参数
int shardIndex = XxlJobHelper.getShardIndex();
int shardTotal = XxlJobHelper.getShardTotal();
XxlJobHelper.log("分片参数:当前分片序号 = {}, 总分片数 = {}", shardIndex, shardTotal);
// 业务逻辑
for (int i = 0; i < shardTotal; i++) {
if (i == shardIndex) {
XxlJobHelper.log("第 {} 片, 命中分片开始处理", i);
} else {
XxlJobHelper.log("第 {} 片, 忽略", i);
}
}
}
/**
* 3、命令行任务
*/
@XxlJob("commandJobHandler")
public void commandJobHandler() throws Exception {
String command = XxlJobHelper.getJobParam();
int exitValue = -1;
BufferedReader bufferedReader = null;
try {
// command process
ProcessBuilder processBuilder = new ProcessBuilder();
processBuilder.command(command);
processBuilder.redirectErrorStream(true);
Process process = processBuilder.start();
//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) {
XxlJobHelper.log(line);
}
// command exit
process.waitFor();
exitValue = process.exitValue();
} catch (Exception e) {
XxlJobHelper.log(e);
} finally {
if (bufferedReader != null) {
bufferedReader.close();
}
}
if (exitValue == 0) {
// default success
} else {
XxlJobHelper.handleFail("command exit value(" + exitValue + ") is failed");
}
}
/**
* 4、跨平台Http任务
* 参数示例:
* "url: http://www.baidu.com\n" +
* "method: get\n" +
* "data: content\n";
*/
@XxlJob("httpJobHandler")
public void httpJobHandler() throws Exception {
// param parse
String param = XxlJobHelper.getJobParam();
if (param == null || param.trim().length() == 0) {
XxlJobHelper.log("param[" + param + "] invalid.");
XxlJobHelper.handleFail();
return;
}
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) {
XxlJobHelper.log("url[" + url + "] invalid.");
XxlJobHelper.handleFail();
return;
}
if (method == null || !Arrays.asList("GET", "POST").contains(method)) {
XxlJobHelper.log("method[" + method + "] invalid.");
XxlJobHelper.handleFail();
return;
}
boolean isPostMethod = method.equals("POST");
// 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(isPostMethod);
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 (isPostMethod && 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 != 200) {
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();
XxlJobHelper.log(responseMsg);
return;
} catch (Exception e) {
XxlJobHelper.log(e);
XxlJobHelper.handleFail();
return;
} finally {
try {
if (bufferedReader != null) {
bufferedReader.close();
}
if (connection != null) {
connection.disconnect();
}
} catch (Exception e2) {
XxlJobHelper.log(e2);
}
}
}
/**
* 5、生命周期任务示例任务初始化与销毁时支持自定义相关逻辑
*/
@XxlJob(value = "demoJobHandler2", init = "init", destroy = "destroy")
public void demoJobHandler2() throws Exception {
XxlJobHelper.log("XXL-JOB, Hello World.");
}
public void init() {
log.info("init");
}
public void destroy() {
log.info("destory");
}
}