mirror of
https://gitee.com/dromara/RuoYi-Vue-Plus.git
synced 2026-09-21 02:25:56 +08:00
add ruoyi-job 恢复原本的 job 模块 ;
update LoginHelper ; delete 删除 ruoyi-common-job ;
This commit is contained in:
parent
dc7b0fe2ee
commit
3d0604e480
8
pom.xml
8
pom.xml
@ -256,6 +256,13 @@
|
|||||||
<version>${snakeyaml.version}</version>
|
<version>${snakeyaml.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- 定时任务 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.ruoyi</groupId>
|
||||||
|
<artifactId>ruoyi-job</artifactId>
|
||||||
|
<version>${ruoyi-vue-plus.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!-- 代码生成-->
|
<!-- 代码生成-->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.ruoyi</groupId>
|
<groupId>com.ruoyi</groupId>
|
||||||
@ -292,6 +299,7 @@
|
|||||||
<module>ruoyi-system</module>
|
<module>ruoyi-system</module>
|
||||||
<module>ruoyi-generator</module>
|
<module>ruoyi-generator</module>
|
||||||
<module>ruoyi-common</module>
|
<module>ruoyi-common</module>
|
||||||
|
<module>ruoyi-job</module>
|
||||||
<module>ruoyi-demo</module>
|
<module>ruoyi-demo</module>
|
||||||
<module>ruoyi-extend</module>
|
<module>ruoyi-extend</module>
|
||||||
</modules>
|
</modules>
|
||||||
|
|||||||
@ -62,7 +62,7 @@
|
|||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.ruoyi</groupId>
|
<groupId>com.ruoyi</groupId>
|
||||||
<artifactId>ruoyi-common-job</artifactId>
|
<artifactId>ruoyi-job</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
|
|||||||
@ -6,6 +6,7 @@ import com.ruoyi.common.log.annotation.Log;
|
|||||||
import com.ruoyi.common.core.constant.UserConstants;
|
import com.ruoyi.common.core.constant.UserConstants;
|
||||||
import com.ruoyi.common.core.web.controller.BaseController;
|
import com.ruoyi.common.core.web.controller.BaseController;
|
||||||
import com.ruoyi.common.core.domain.R;
|
import com.ruoyi.common.core.domain.R;
|
||||||
|
import com.ruoyi.common.satoken.utils.LoginHelper;
|
||||||
import com.ruoyi.system.domain.SysMenu;
|
import com.ruoyi.system.domain.SysMenu;
|
||||||
import com.ruoyi.common.log.enums.BusinessType;
|
import com.ruoyi.common.log.enums.BusinessType;
|
||||||
import com.ruoyi.common.core.utils.StringUtils;
|
import com.ruoyi.common.core.utils.StringUtils;
|
||||||
@ -18,8 +19,6 @@ import java.util.HashMap;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import static com.ruoyi.common.satoken.utils.LoginHelper.getUserId;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 菜单信息
|
* 菜单信息
|
||||||
*
|
*
|
||||||
@ -39,7 +38,7 @@ public class SysMenuController extends BaseController {
|
|||||||
@SaCheckPermission("system:menu:list")
|
@SaCheckPermission("system:menu:list")
|
||||||
@GetMapping("/list")
|
@GetMapping("/list")
|
||||||
public R<List<SysMenu>> list(SysMenu menu) {
|
public R<List<SysMenu>> list(SysMenu menu) {
|
||||||
List<SysMenu> menus = menuService.selectMenuList(menu, getUserId());
|
List<SysMenu> menus = menuService.selectMenuList(menu, LoginHelper.getUserId());
|
||||||
return R.ok(menus);
|
return R.ok(menus);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -59,7 +58,7 @@ public class SysMenuController extends BaseController {
|
|||||||
*/
|
*/
|
||||||
@GetMapping("/treeselect")
|
@GetMapping("/treeselect")
|
||||||
public R<List<Tree<Long>>> treeselect(SysMenu menu) {
|
public R<List<Tree<Long>>> treeselect(SysMenu menu) {
|
||||||
List<SysMenu> menus = menuService.selectMenuList(menu, getUserId());
|
List<SysMenu> menus = menuService.selectMenuList(menu, LoginHelper.getUserId());
|
||||||
return R.ok(menuService.buildMenuTreeSelect(menus));
|
return R.ok(menuService.buildMenuTreeSelect(menus));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -70,7 +69,7 @@ public class SysMenuController extends BaseController {
|
|||||||
*/
|
*/
|
||||||
@GetMapping(value = "/roleMenuTreeselect/{roleId}")
|
@GetMapping(value = "/roleMenuTreeselect/{roleId}")
|
||||||
public R<Map<String, Object>> roleMenuTreeselect(@PathVariable("roleId") Long roleId) {
|
public R<Map<String, Object>> roleMenuTreeselect(@PathVariable("roleId") Long roleId) {
|
||||||
List<SysMenu> menus = menuService.selectMenuList(getUserId());
|
List<SysMenu> menus = menuService.selectMenuList(LoginHelper.getUserId());
|
||||||
Map<String, Object> ajax = new HashMap<>();
|
Map<String, Object> ajax = new HashMap<>();
|
||||||
ajax.put("checkedKeys", menuService.selectMenuListByRoleId(roleId));
|
ajax.put("checkedKeys", menuService.selectMenuListByRoleId(roleId));
|
||||||
ajax.put("menus", menuService.buildMenuTreeSelect(menus));
|
ajax.put("menus", menuService.buildMenuTreeSelect(menus));
|
||||||
|
|||||||
@ -11,7 +11,6 @@ import com.ruoyi.system.domain.SysUser;
|
|||||||
import com.ruoyi.common.log.enums.BusinessType;
|
import com.ruoyi.common.log.enums.BusinessType;
|
||||||
import com.ruoyi.common.core.utils.StringUtils;
|
import com.ruoyi.common.core.utils.StringUtils;
|
||||||
import com.ruoyi.common.core.utils.file.MimeTypeUtils;
|
import com.ruoyi.common.core.utils.file.MimeTypeUtils;
|
||||||
import com.ruoyi.system.domain.SysOss;
|
|
||||||
import com.ruoyi.system.domain.vo.SysOssVo;
|
import com.ruoyi.system.domain.vo.SysOssVo;
|
||||||
import com.ruoyi.system.service.ISysOssService;
|
import com.ruoyi.system.service.ISysOssService;
|
||||||
import com.ruoyi.system.service.ISysUserService;
|
import com.ruoyi.system.service.ISysUserService;
|
||||||
@ -25,9 +24,6 @@ import java.util.Arrays;
|
|||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import static com.ruoyi.common.satoken.utils.LoginHelper.getUserId;
|
|
||||||
import static com.ruoyi.common.satoken.utils.LoginHelper.getUsername;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 个人信息 业务处理
|
* 个人信息 业务处理
|
||||||
*
|
*
|
||||||
@ -47,7 +43,7 @@ public class SysProfileController extends BaseController {
|
|||||||
*/
|
*/
|
||||||
@GetMapping
|
@GetMapping
|
||||||
public R<Map<String, Object>> profile() {
|
public R<Map<String, Object>> profile() {
|
||||||
SysUser user = userService.selectUserById(getUserId());
|
SysUser user = userService.selectUserById(LoginHelper.getUserId());
|
||||||
Map<String, Object> ajax = new HashMap<>();
|
Map<String, Object> ajax = new HashMap<>();
|
||||||
ajax.put("user", user);
|
ajax.put("user", user);
|
||||||
ajax.put("roleGroup", userService.selectUserRoleGroup(user.getUserName()));
|
ajax.put("roleGroup", userService.selectUserRoleGroup(user.getUserName()));
|
||||||
@ -69,7 +65,7 @@ public class SysProfileController extends BaseController {
|
|||||||
&& UserConstants.NOT_UNIQUE.equals(userService.checkEmailUnique(user))) {
|
&& UserConstants.NOT_UNIQUE.equals(userService.checkEmailUnique(user))) {
|
||||||
return R.fail("修改用户'" + user.getUserName() + "'失败,邮箱账号已存在");
|
return R.fail("修改用户'" + user.getUserName() + "'失败,邮箱账号已存在");
|
||||||
}
|
}
|
||||||
user.setUserId(getUserId());
|
user.setUserId(LoginHelper.getUserId());
|
||||||
user.setUserName(null);
|
user.setUserName(null);
|
||||||
user.setPassword(null);
|
user.setPassword(null);
|
||||||
user.setAvatar(null);
|
user.setAvatar(null);
|
||||||
@ -121,7 +117,7 @@ public class SysProfileController extends BaseController {
|
|||||||
}
|
}
|
||||||
SysOssVo oss = iSysOssService.upload(avatarfile);
|
SysOssVo oss = iSysOssService.upload(avatarfile);
|
||||||
String avatar = oss.getUrl();
|
String avatar = oss.getUrl();
|
||||||
if (userService.updateUserAvatar(getUsername(), avatar)) {
|
if (userService.updateUserAvatar(LoginHelper.getUsername(), avatar)) {
|
||||||
ajax.put("imgUrl", avatar);
|
ajax.put("imgUrl", avatar);
|
||||||
return R.ok(ajax);
|
return R.ok(ajax);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -29,8 +29,6 @@ import java.util.HashMap;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import static com.ruoyi.common.satoken.utils.LoginHelper.getLoginUser;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 角色信息
|
* 角色信息
|
||||||
*
|
*
|
||||||
@ -112,7 +110,7 @@ public class SysRoleController extends BaseController {
|
|||||||
|
|
||||||
if (roleService.updateRole(role) > 0) {
|
if (roleService.updateRole(role) > 0) {
|
||||||
// 更新缓存用户权限
|
// 更新缓存用户权限
|
||||||
LoginUser loginUser = getLoginUser();
|
LoginUser loginUser = LoginHelper.getLoginUser();
|
||||||
SysUser sysUser = userService.selectUserById(loginUser.getUserId());
|
SysUser sysUser = userService.selectUserById(loginUser.getUserId());
|
||||||
if (ObjectUtil.isNotNull(sysUser) && !sysUser.isAdmin()) {
|
if (ObjectUtil.isNotNull(sysUser) && !sysUser.isAdmin()) {
|
||||||
loginUser.setMenuPermission(permissionService.getMenuPermission(sysUser));
|
loginUser.setMenuPermission(permissionService.getMenuPermission(sysUser));
|
||||||
|
|||||||
@ -40,8 +40,6 @@ import java.util.HashMap;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import static com.ruoyi.common.satoken.utils.LoginHelper.getUserId;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 用户信息
|
* 用户信息
|
||||||
*
|
*
|
||||||
@ -181,7 +179,7 @@ public class SysUserController extends BaseController {
|
|||||||
@Log(title = "用户管理", businessType = BusinessType.DELETE)
|
@Log(title = "用户管理", businessType = BusinessType.DELETE)
|
||||||
@DeleteMapping("/{userIds}")
|
@DeleteMapping("/{userIds}")
|
||||||
public R<Void> remove(@PathVariable Long[] userIds) {
|
public R<Void> remove(@PathVariable Long[] userIds) {
|
||||||
if (ArrayUtil.contains(userIds, getUserId())) {
|
if (ArrayUtil.contains(userIds, LoginHelper.getUserId())) {
|
||||||
return R.fail("当前用户不能删除");
|
return R.fail("当前用户不能删除");
|
||||||
}
|
}
|
||||||
return toAjax(userService.deleteUserByIds(userIds));
|
return toAjax(userService.deleteUserByIds(userIds));
|
||||||
|
|||||||
@ -16,7 +16,6 @@
|
|||||||
<module>ruoyi-common-dict</module>
|
<module>ruoyi-common-dict</module>
|
||||||
<module>ruoyi-common-doc</module>
|
<module>ruoyi-common-doc</module>
|
||||||
<module>ruoyi-common-excel</module>
|
<module>ruoyi-common-excel</module>
|
||||||
<module>ruoyi-common-job</module>
|
|
||||||
<module>ruoyi-common-log</module>
|
<module>ruoyi-common-log</module>
|
||||||
<module>ruoyi-common-mail</module>
|
<module>ruoyi-common-mail</module>
|
||||||
<module>ruoyi-common-oss</module>
|
<module>ruoyi-common-oss</module>
|
||||||
|
|||||||
@ -50,13 +50,6 @@
|
|||||||
<version>${project.version}</version>
|
<version>${project.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- 定时任务 -->
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.ruoyi</groupId>
|
|
||||||
<artifactId>ruoyi-common-job</artifactId>
|
|
||||||
<version>${project.version}</version>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<!-- 日志记录 -->
|
<!-- 日志记录 -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.ruoyi</groupId>
|
<groupId>com.ruoyi</groupId>
|
||||||
|
|||||||
@ -1 +0,0 @@
|
|||||||
com.ruoyi.common.job.config.XxlJobConfig
|
|
||||||
@ -5,14 +5,14 @@
|
|||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>com.ruoyi</groupId>
|
<groupId>com.ruoyi</groupId>
|
||||||
<artifactId>ruoyi-common</artifactId>
|
<artifactId>ruoyi-vue-plus</artifactId>
|
||||||
<version>4.4.0</version>
|
<version>4.4.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<artifactId>ruoyi-common-job</artifactId>
|
<artifactId>ruoyi-job</artifactId>
|
||||||
|
|
||||||
<description>
|
<description>
|
||||||
ruoyi-common-job 定时任务
|
任务调度
|
||||||
</description>
|
</description>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
@ -21,21 +21,11 @@
|
|||||||
<artifactId>ruoyi-common-core</artifactId>
|
<artifactId>ruoyi-common-core</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.boot</groupId>
|
|
||||||
<artifactId>spring-boot-autoconfigure</artifactId>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<!-- xxl-job-core -->
|
<!-- xxl-job-core -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.xuxueli</groupId>
|
<groupId>com.xuxueli</groupId>
|
||||||
<artifactId>xxl-job-core</artifactId>
|
<artifactId>xxl-job-core</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.projectlombok</groupId>
|
|
||||||
<artifactId>lombok</artifactId>
|
|
||||||
</dependency>
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
@ -0,0 +1,42 @@
|
|||||||
|
package com.ruoyi.job.config;
|
||||||
|
|
||||||
|
import com.ruoyi.job.config.properties.XxlJobProperties;
|
||||||
|
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||||
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* xxl-job config
|
||||||
|
*
|
||||||
|
* @author Lion Li
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@Configuration
|
||||||
|
@EnableConfigurationProperties(XxlJobProperties.class)
|
||||||
|
@AllArgsConstructor
|
||||||
|
@ConditionalOnProperty(prefix = "xxl.job", name = "enabled", havingValue = "true")
|
||||||
|
public class XxlJobConfig {
|
||||||
|
|
||||||
|
private final XxlJobProperties xxlJobProperties;
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public XxlJobSpringExecutor xxlJobExecutor() {
|
||||||
|
log.info(">>>>>>>>>>> xxl-job config init.");
|
||||||
|
XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
|
||||||
|
xxlJobSpringExecutor.setAdminAddresses(xxlJobProperties.getAdminAddresses());
|
||||||
|
xxlJobSpringExecutor.setAccessToken(xxlJobProperties.getAccessToken());
|
||||||
|
XxlJobProperties.Executor executor = xxlJobProperties.getExecutor();
|
||||||
|
xxlJobSpringExecutor.setAppname(executor.getAppname());
|
||||||
|
xxlJobSpringExecutor.setAddress(executor.getAddress());
|
||||||
|
xxlJobSpringExecutor.setIp(executor.getIp());
|
||||||
|
xxlJobSpringExecutor.setPort(executor.getPort());
|
||||||
|
xxlJobSpringExecutor.setLogPath(executor.getLogPath());
|
||||||
|
xxlJobSpringExecutor.setLogRetentionDays(executor.getLogRetentionDays());
|
||||||
|
return xxlJobSpringExecutor;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,40 @@
|
|||||||
|
package com.ruoyi.job.config.properties;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* xxljob配置类
|
||||||
|
*
|
||||||
|
* @author Lion Li
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@ConfigurationProperties(prefix = "xxl.job")
|
||||||
|
public class XxlJobProperties {
|
||||||
|
|
||||||
|
private Boolean enabled;
|
||||||
|
|
||||||
|
private String adminAddresses;
|
||||||
|
|
||||||
|
private String accessToken;
|
||||||
|
|
||||||
|
private Executor executor;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@NoArgsConstructor
|
||||||
|
public static class Executor {
|
||||||
|
|
||||||
|
private String appname;
|
||||||
|
|
||||||
|
private String address;
|
||||||
|
|
||||||
|
private String ip;
|
||||||
|
|
||||||
|
private int port;
|
||||||
|
|
||||||
|
private String logPath;
|
||||||
|
|
||||||
|
private int logRetentionDays;
|
||||||
|
}
|
||||||
|
}
|
||||||
252
ruoyi-job/src/main/java/com/ruoyi/job/service/SampleService.java
Normal file
252
ruoyi-job/src/main/java/com/ruoyi/job/service/SampleService.java
Normal file
@ -0,0 +1,252 @@
|
|||||||
|
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");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user