阿沐 2022-11-08 10:54:00 +08:00
parent 77a9db8c8f
commit 446eb72943
2 changed files with 31 additions and 13 deletions

View File

@ -1,8 +1,11 @@
package net.maku.quartz.controller;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.extra.spring.SpringUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import net.maku.framework.common.exception.ServerException;
import net.maku.framework.common.page.PageResult;
import net.maku.framework.common.utils.Result;
import net.maku.quartz.convert.ScheduleJobConvert;
@ -12,19 +15,20 @@ import net.maku.quartz.service.ScheduleJobService;
import net.maku.quartz.utils.CronUtils;
import net.maku.quartz.vo.ScheduleJobVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;
/**
* 定时任务
*
* @author 阿沐 babamu@126.com
*/
* 定时任务
*
* @author 阿沐 babamu@126.com
*/
@RestController
@RequestMapping("schedule")
@Tag(name="定时任务")
@Tag(name = "定时任务")
@AllArgsConstructor
public class ScheduleJobController {
private final ScheduleJobService scheduleJobService;
@ -32,7 +36,7 @@ public class ScheduleJobController {
@GetMapping("page")
@Operation(summary = "分页")
@PreAuthorize("hasAuthority('schedule:page')")
public Result<PageResult<ScheduleJobVO>> page(@Valid ScheduleJobQuery query){
public Result<PageResult<ScheduleJobVO>> page(@Valid ScheduleJobQuery query) {
PageResult<ScheduleJobVO> page = scheduleJobService.page(query);
return Result.ok(page);
@ -41,7 +45,7 @@ public class ScheduleJobController {
@GetMapping("{id}")
@Operation(summary = "信息")
@PreAuthorize("hasAuthority('schedule:info')")
public Result<ScheduleJobVO> get(@PathVariable("id") Long id){
public Result<ScheduleJobVO> get(@PathVariable("id") Long id) {
ScheduleJobEntity entity = scheduleJobService.getById(id);
return Result.ok(ScheduleJobConvert.INSTANCE.convert(entity));
@ -50,11 +54,14 @@ public class ScheduleJobController {
@PostMapping
@Operation(summary = "保存")
@PreAuthorize("hasAuthority('schedule:save')")
public Result<String> save(@RequestBody ScheduleJobVO vo){
public Result<String> save(@RequestBody ScheduleJobVO vo) {
if (!CronUtils.isValid(vo.getCronExpression())) {
return Result.error("操作失败Cron表达式不正确");
}
// 检查Bean的合法性
checkBean(vo.getBeanName());
scheduleJobService.save(vo);
return Result.ok();
@ -68,6 +75,9 @@ public class ScheduleJobController {
return Result.error("操作失败Cron表达式不正确");
}
// 检查Bean的合法性
checkBean(vo.getBeanName());
scheduleJobService.update(vo);
return Result.ok();
@ -76,7 +86,7 @@ public class ScheduleJobController {
@DeleteMapping
@Operation(summary = "删除")
@PreAuthorize("hasAuthority('schedule:delete')")
public Result<String> delete(@RequestBody List<Long> idList){
public Result<String> delete(@RequestBody List<Long> idList) {
scheduleJobService.delete(idList);
return Result.ok();
@ -85,7 +95,7 @@ public class ScheduleJobController {
@PutMapping("run")
@Operation(summary = "立即执行")
@PreAuthorize("hasAuthority('schedule:run')")
public Result<String> run(@RequestBody ScheduleJobVO vo){
public Result<String> run(@RequestBody ScheduleJobVO vo) {
scheduleJobService.run(vo);
return Result.ok();
@ -94,9 +104,17 @@ public class ScheduleJobController {
@PutMapping("change-status")
@Operation(summary = "修改状态")
@PreAuthorize("hasAuthority('schedule:update')")
public Result<String> changeStatus(@RequestBody ScheduleJobVO vo){
public Result<String> changeStatus(@RequestBody ScheduleJobVO vo) {
scheduleJobService.changeStatus(vo);
return Result.ok();
}
private void checkBean(String beanName) {
// 为避免执行jdbcTemplate等类只允许添加有@Service注解的Bean
String[] serviceBeans = SpringUtil.getApplicationContext().getBeanNamesForAnnotation(Service.class);
if (!ArrayUtil.contains(serviceBeans, beanName)) {
throw new ServerException("只允许添加有@Service注解的Bean");
}
}
}

View File

@ -1,7 +1,7 @@
package net.maku.quartz.task;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
/**
* 测试定时任务
@ -9,7 +9,7 @@ import org.springframework.stereotype.Component;
* @author 阿沐 babamu@126.com
*/
@Slf4j
@Component
@Service
public class TestTask {
public void run(String params) throws InterruptedException {