feat: 定时任务调用修改

This commit is contained in:
张松 2024-12-30 18:23:21 +08:00
parent 5e06231aaa
commit 91a1154383
5 changed files with 86 additions and 36 deletions

View File

@ -96,28 +96,9 @@ public class ScheduleJobController {
@SysLog("立即执行任务")
@RequestMapping("/run")
public Result run(@RequestBody @Validated RunJobDTO jobDTO) throws Exception {
// 从Spring容器获取类的实例
Object classInstance = applicationContext.getBean(jobDTO.getClassName());
public Result run(@RequestBody Long[] jobIds) throws Exception {
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);
// 调用方法并返回结果
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);
return Result.success().put("data", scheduleJobService.run(jobIds));
}
// @SysLog("立即执行任务")

View File

@ -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;
}

View File

@ -2,6 +2,7 @@ package com.sqx.modules.job.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.sqx.common.utils.PageUtils;
import com.sqx.modules.job.dto.RunJobDTO;
import com.sqx.modules.job.entity.ScheduleJobEntity;
import java.util.Map;
@ -18,22 +19,24 @@ public interface ScheduleJobService extends IService<ScheduleJobEntity> {
* 保存定时任务
*/
void saveJob(ScheduleJobEntity scheduleJob);
/**
* 更新定时任务
*/
void update(ScheduleJobEntity scheduleJob);
/**
* 批量删除定时任务
*/
void deleteBatch(Long[] jobIds);
/**
* 批量更新定时任务状态
*/
int updateBatch(Long[] jobIds, int status);
Object run(Long[] jobIds) throws Exception;
// /**
// * 立即执行
// */

View File

@ -1,5 +1,6 @@
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.metadata.IPage;
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.Query;
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.service.ScheduleJobService;
import org.apache.commons.lang.StringUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.lang.reflect.Method;
import java.util.*;
@Service("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());
this.save(scheduleJob);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void update(ScheduleJobEntity scheduleJob) {
@ -68,8 +75,48 @@ public class ScheduleJobServiceImpl extends ServiceImpl<ScheduleJobDao, Schedule
map.put("status", status);
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)
// public void run(Long[] jobIds) {
// for(Long jobId : jobIds){
@ -96,5 +143,5 @@ public class ScheduleJobServiceImpl extends ServiceImpl<ScheduleJobDao, Schedule
//
// updateBatch(jobIds, Constant.ScheduleStatus.NORMAL.getValue());
// }
}

View File

@ -32,7 +32,7 @@ public class CashOutTask{
private Logger logger = LoggerFactory.getLogger(getClass());
public void run(String params) {
public void run() {
logger.info("提现开始");
List<CashOut> cashOuts = cashOutDao.selectYesterday();
for (CashOut cashOut : cashOuts) {
@ -67,4 +67,9 @@ public class CashOutTask{
}
logger.info("提现结束");
}
public void test(String name, String age) {
logger.info("name: {}, age: {}", name, age);
}
}