feat: 定时任务调用修改
This commit is contained in:
parent
5e06231aaa
commit
91a1154383
|
|
@ -96,28 +96,9 @@ public class ScheduleJobController {
|
||||||
|
|
||||||
@SysLog("立即执行任务")
|
@SysLog("立即执行任务")
|
||||||
@RequestMapping("/run")
|
@RequestMapping("/run")
|
||||||
public Result run(@RequestBody @Validated RunJobDTO jobDTO) throws Exception {
|
public Result run(@RequestBody Long[] jobIds) throws Exception {
|
||||||
// 从Spring容器获取类的实例
|
|
||||||
Object classInstance = applicationContext.getBean(jobDTO.getClassName());
|
|
||||||
|
|
||||||
if (jobDTO.getArgs() != null && !jobDTO.getArgs().isEmpty()) {
|
return Result.success().put("data", scheduleJobService.run(jobIds));
|
||||||
Class<?>[] argTypes = new Class<?>[jobDTO.getArgs().size()];
|
|
||||||
for (int i = 0; i < jobDTO.getArgs().size(); i++) {
|
|
||||||
argTypes[i] = jobDTO.getArgs().get(i).getClass();
|
|
||||||
}
|
|
||||||
// 获取指定的Method对象
|
|
||||||
Method method = classInstance.getClass().getDeclaredMethod(jobDTO.getMethodName(), argTypes);
|
|
||||||
|
|
||||||
// 调用方法并返回结果
|
|
||||||
Object invoke = method.invoke(classInstance, jobDTO.getArgs().toArray());
|
|
||||||
return Result.success().put("data", invoke == null ? "ok" : invoke);
|
|
||||||
}
|
|
||||||
// 获取指定的Method对象
|
|
||||||
Method method = classInstance.getClass().getDeclaredMethod(jobDTO.getMethodName());
|
|
||||||
|
|
||||||
// 调用方法并返回结果
|
|
||||||
Object invoke = method.invoke(classInstance);
|
|
||||||
return Result.success().put("data", invoke == null ? "ok" : invoke);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// @SysLog("立即执行任务")
|
// @SysLog("立即执行任务")
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,14 @@
|
||||||
|
package com.sqx.modules.job.dto;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import javax.validation.constraints.NotBlank;
|
||||||
|
import javax.validation.constraints.NotNull;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class RunJobDTO {
|
||||||
|
@NotNull
|
||||||
|
private Integer jobId;
|
||||||
|
private List<?> args;
|
||||||
|
}
|
||||||
|
|
@ -2,6 +2,7 @@ package com.sqx.modules.job.service;
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.extension.service.IService;
|
import com.baomidou.mybatisplus.extension.service.IService;
|
||||||
import com.sqx.common.utils.PageUtils;
|
import com.sqx.common.utils.PageUtils;
|
||||||
|
import com.sqx.modules.job.dto.RunJobDTO;
|
||||||
import com.sqx.modules.job.entity.ScheduleJobEntity;
|
import com.sqx.modules.job.entity.ScheduleJobEntity;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
@ -18,22 +19,24 @@ public interface ScheduleJobService extends IService<ScheduleJobEntity> {
|
||||||
* 保存定时任务
|
* 保存定时任务
|
||||||
*/
|
*/
|
||||||
void saveJob(ScheduleJobEntity scheduleJob);
|
void saveJob(ScheduleJobEntity scheduleJob);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 更新定时任务
|
* 更新定时任务
|
||||||
*/
|
*/
|
||||||
void update(ScheduleJobEntity scheduleJob);
|
void update(ScheduleJobEntity scheduleJob);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 批量删除定时任务
|
* 批量删除定时任务
|
||||||
*/
|
*/
|
||||||
void deleteBatch(Long[] jobIds);
|
void deleteBatch(Long[] jobIds);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 批量更新定时任务状态
|
* 批量更新定时任务状态
|
||||||
*/
|
*/
|
||||||
int updateBatch(Long[] jobIds, int status);
|
int updateBatch(Long[] jobIds, int status);
|
||||||
|
|
||||||
|
Object run(Long[] jobIds) throws Exception;
|
||||||
|
|
||||||
// /**
|
// /**
|
||||||
// * 立即执行
|
// * 立即执行
|
||||||
// */
|
// */
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
package com.sqx.modules.job.service.impl;
|
package com.sqx.modules.job.service.impl;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
|
|
@ -7,21 +8,27 @@ import com.sqx.common.utils.Constant;
|
||||||
import com.sqx.common.utils.PageUtils;
|
import com.sqx.common.utils.PageUtils;
|
||||||
import com.sqx.common.utils.Query;
|
import com.sqx.common.utils.Query;
|
||||||
import com.sqx.modules.job.dao.ScheduleJobDao;
|
import com.sqx.modules.job.dao.ScheduleJobDao;
|
||||||
|
import com.sqx.modules.job.dto.RunJobDTO;
|
||||||
import com.sqx.modules.job.entity.ScheduleJobEntity;
|
import com.sqx.modules.job.entity.ScheduleJobEntity;
|
||||||
import com.sqx.modules.job.service.ScheduleJobService;
|
import com.sqx.modules.job.service.ScheduleJobService;
|
||||||
import org.apache.commons.lang.StringUtils;
|
import org.apache.commons.lang.StringUtils;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.lang.reflect.Method;
|
||||||
import java.util.Date;
|
import java.util.*;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
@Service("scheduleJobService")
|
@Service("scheduleJobService")
|
||||||
public class ScheduleJobServiceImpl extends ServiceImpl<ScheduleJobDao, ScheduleJobEntity> implements ScheduleJobService {
|
public class ScheduleJobServiceImpl extends ServiceImpl<ScheduleJobDao, ScheduleJobEntity> implements ScheduleJobService {
|
||||||
|
|
||||||
/**
|
private final ApplicationContext applicationContext;
|
||||||
|
|
||||||
|
public ScheduleJobServiceImpl(ApplicationContext applicationContext) {
|
||||||
|
this.applicationContext = applicationContext;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
* 项目启动时,初始化定时器
|
* 项目启动时,初始化定时器
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
@ -46,7 +53,7 @@ public class ScheduleJobServiceImpl extends ServiceImpl<ScheduleJobDao, Schedule
|
||||||
scheduleJob.setStatus(Constant.ScheduleStatus.NORMAL.getValue());
|
scheduleJob.setStatus(Constant.ScheduleStatus.NORMAL.getValue());
|
||||||
this.save(scheduleJob);
|
this.save(scheduleJob);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public void update(ScheduleJobEntity scheduleJob) {
|
public void update(ScheduleJobEntity scheduleJob) {
|
||||||
|
|
@ -68,8 +75,48 @@ public class ScheduleJobServiceImpl extends ServiceImpl<ScheduleJobDao, Schedule
|
||||||
map.put("status", status);
|
map.put("status", status);
|
||||||
return baseMapper.updateBatch(map);
|
return baseMapper.updateBatch(map);
|
||||||
}
|
}
|
||||||
|
|
||||||
// @Override
|
|
||||||
|
@Override
|
||||||
|
public Object run(Long[] jobIds) throws Exception {
|
||||||
|
ArrayList<Object> result = new ArrayList<>();
|
||||||
|
for(Long jobId : jobIds){
|
||||||
|
ScheduleJobEntity jobEntity = baseMapper.selectById(jobId);
|
||||||
|
if (jobEntity == null) {
|
||||||
|
throw new RuntimeException("任务不存在");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 从Spring容器获取类的实例
|
||||||
|
Object classInstance = applicationContext.getBean(StrUtil.lowerFirst(jobEntity.getBeanName()));
|
||||||
|
if (!"com.sqx.modules.job.task".equals(classInstance.getClass().getPackage().getName())) {
|
||||||
|
throw new RuntimeException("非法调用");
|
||||||
|
}
|
||||||
|
|
||||||
|
// if (jobDTO.getArgs() != null && !jobDTO.getArgs().isEmpty()) {
|
||||||
|
// Class<?>[] argTypes = new Class<?>[jobDTO.getArgs().size()];
|
||||||
|
// for (int i = 0; i < jobDTO.getArgs().size(); i++) {
|
||||||
|
// argTypes[i] = jobDTO.getArgs().get(i).getClass();
|
||||||
|
// }
|
||||||
|
// // 获取指定的Method对象
|
||||||
|
// Method method = classInstance.getClass().getDeclaredMethod(jobDTO.getMethodName(), argTypes);
|
||||||
|
//
|
||||||
|
// // 调用方法并返回结果
|
||||||
|
// return method.invoke(classInstance, jobDTO.getArgs().toArray());
|
||||||
|
// }
|
||||||
|
// 获取指定的Method对象
|
||||||
|
Method method = classInstance.getClass().getDeclaredMethod("run");
|
||||||
|
|
||||||
|
// 调用方法并返回结果
|
||||||
|
Object invoke = method.invoke(classInstance);
|
||||||
|
if (invoke != null) {
|
||||||
|
result.add(invoke);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
// @Override
|
||||||
// @Transactional(rollbackFor = Exception.class)
|
// @Transactional(rollbackFor = Exception.class)
|
||||||
// public void run(Long[] jobIds) {
|
// public void run(Long[] jobIds) {
|
||||||
// for(Long jobId : jobIds){
|
// for(Long jobId : jobIds){
|
||||||
|
|
@ -96,5 +143,5 @@ public class ScheduleJobServiceImpl extends ServiceImpl<ScheduleJobDao, Schedule
|
||||||
//
|
//
|
||||||
// updateBatch(jobIds, Constant.ScheduleStatus.NORMAL.getValue());
|
// updateBatch(jobIds, Constant.ScheduleStatus.NORMAL.getValue());
|
||||||
// }
|
// }
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -32,7 +32,7 @@ public class CashOutTask{
|
||||||
|
|
||||||
private Logger logger = LoggerFactory.getLogger(getClass());
|
private Logger logger = LoggerFactory.getLogger(getClass());
|
||||||
|
|
||||||
public void run(String params) {
|
public void run() {
|
||||||
logger.info("提现开始");
|
logger.info("提现开始");
|
||||||
List<CashOut> cashOuts = cashOutDao.selectYesterday();
|
List<CashOut> cashOuts = cashOutDao.selectYesterday();
|
||||||
for (CashOut cashOut : cashOuts) {
|
for (CashOut cashOut : cashOuts) {
|
||||||
|
|
@ -67,4 +67,9 @@ public class CashOutTask{
|
||||||
}
|
}
|
||||||
logger.info("提现结束");
|
logger.info("提现结束");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void test(String name, String age) {
|
||||||
|
logger.info("name: {}, age: {}", name, age);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue