This commit is contained in:
parent
77a9db8c8f
commit
446eb72943
|
@ -1,8 +1,11 @@
|
||||||
package net.maku.quartz.controller;
|
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.Operation;
|
||||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
|
import net.maku.framework.common.exception.ServerException;
|
||||||
import net.maku.framework.common.page.PageResult;
|
import net.maku.framework.common.page.PageResult;
|
||||||
import net.maku.framework.common.utils.Result;
|
import net.maku.framework.common.utils.Result;
|
||||||
import net.maku.quartz.convert.ScheduleJobConvert;
|
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.utils.CronUtils;
|
||||||
import net.maku.quartz.vo.ScheduleJobVO;
|
import net.maku.quartz.vo.ScheduleJobVO;
|
||||||
import org.springframework.security.access.prepost.PreAuthorize;
|
import org.springframework.security.access.prepost.PreAuthorize;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import javax.validation.Valid;
|
import javax.validation.Valid;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 定时任务
|
* 定时任务
|
||||||
*
|
*
|
||||||
* @author 阿沐 babamu@126.com
|
* @author 阿沐 babamu@126.com
|
||||||
*/
|
*/
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("schedule")
|
@RequestMapping("schedule")
|
||||||
@Tag(name="定时任务")
|
@Tag(name = "定时任务")
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public class ScheduleJobController {
|
public class ScheduleJobController {
|
||||||
private final ScheduleJobService scheduleJobService;
|
private final ScheduleJobService scheduleJobService;
|
||||||
|
@ -32,7 +36,7 @@ public class ScheduleJobController {
|
||||||
@GetMapping("page")
|
@GetMapping("page")
|
||||||
@Operation(summary = "分页")
|
@Operation(summary = "分页")
|
||||||
@PreAuthorize("hasAuthority('schedule:page')")
|
@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);
|
PageResult<ScheduleJobVO> page = scheduleJobService.page(query);
|
||||||
|
|
||||||
return Result.ok(page);
|
return Result.ok(page);
|
||||||
|
@ -41,7 +45,7 @@ public class ScheduleJobController {
|
||||||
@GetMapping("{id}")
|
@GetMapping("{id}")
|
||||||
@Operation(summary = "信息")
|
@Operation(summary = "信息")
|
||||||
@PreAuthorize("hasAuthority('schedule:info')")
|
@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);
|
ScheduleJobEntity entity = scheduleJobService.getById(id);
|
||||||
|
|
||||||
return Result.ok(ScheduleJobConvert.INSTANCE.convert(entity));
|
return Result.ok(ScheduleJobConvert.INSTANCE.convert(entity));
|
||||||
|
@ -50,11 +54,14 @@ public class ScheduleJobController {
|
||||||
@PostMapping
|
@PostMapping
|
||||||
@Operation(summary = "保存")
|
@Operation(summary = "保存")
|
||||||
@PreAuthorize("hasAuthority('schedule:save')")
|
@PreAuthorize("hasAuthority('schedule:save')")
|
||||||
public Result<String> save(@RequestBody ScheduleJobVO vo){
|
public Result<String> save(@RequestBody ScheduleJobVO vo) {
|
||||||
if (!CronUtils.isValid(vo.getCronExpression())) {
|
if (!CronUtils.isValid(vo.getCronExpression())) {
|
||||||
return Result.error("操作失败,Cron表达式不正确");
|
return Result.error("操作失败,Cron表达式不正确");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 检查Bean的合法性
|
||||||
|
checkBean(vo.getBeanName());
|
||||||
|
|
||||||
scheduleJobService.save(vo);
|
scheduleJobService.save(vo);
|
||||||
|
|
||||||
return Result.ok();
|
return Result.ok();
|
||||||
|
@ -68,6 +75,9 @@ public class ScheduleJobController {
|
||||||
return Result.error("操作失败,Cron表达式不正确");
|
return Result.error("操作失败,Cron表达式不正确");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 检查Bean的合法性
|
||||||
|
checkBean(vo.getBeanName());
|
||||||
|
|
||||||
scheduleJobService.update(vo);
|
scheduleJobService.update(vo);
|
||||||
|
|
||||||
return Result.ok();
|
return Result.ok();
|
||||||
|
@ -76,7 +86,7 @@ public class ScheduleJobController {
|
||||||
@DeleteMapping
|
@DeleteMapping
|
||||||
@Operation(summary = "删除")
|
@Operation(summary = "删除")
|
||||||
@PreAuthorize("hasAuthority('schedule:delete')")
|
@PreAuthorize("hasAuthority('schedule:delete')")
|
||||||
public Result<String> delete(@RequestBody List<Long> idList){
|
public Result<String> delete(@RequestBody List<Long> idList) {
|
||||||
scheduleJobService.delete(idList);
|
scheduleJobService.delete(idList);
|
||||||
|
|
||||||
return Result.ok();
|
return Result.ok();
|
||||||
|
@ -85,7 +95,7 @@ public class ScheduleJobController {
|
||||||
@PutMapping("run")
|
@PutMapping("run")
|
||||||
@Operation(summary = "立即执行")
|
@Operation(summary = "立即执行")
|
||||||
@PreAuthorize("hasAuthority('schedule:run')")
|
@PreAuthorize("hasAuthority('schedule:run')")
|
||||||
public Result<String> run(@RequestBody ScheduleJobVO vo){
|
public Result<String> run(@RequestBody ScheduleJobVO vo) {
|
||||||
scheduleJobService.run(vo);
|
scheduleJobService.run(vo);
|
||||||
|
|
||||||
return Result.ok();
|
return Result.ok();
|
||||||
|
@ -94,9 +104,17 @@ public class ScheduleJobController {
|
||||||
@PutMapping("change-status")
|
@PutMapping("change-status")
|
||||||
@Operation(summary = "修改状态")
|
@Operation(summary = "修改状态")
|
||||||
@PreAuthorize("hasAuthority('schedule:update')")
|
@PreAuthorize("hasAuthority('schedule:update')")
|
||||||
public Result<String> changeStatus(@RequestBody ScheduleJobVO vo){
|
public Result<String> changeStatus(@RequestBody ScheduleJobVO vo) {
|
||||||
scheduleJobService.changeStatus(vo);
|
scheduleJobService.changeStatus(vo);
|
||||||
|
|
||||||
return Result.ok();
|
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!");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -1,7 +1,7 @@
|
||||||
package net.maku.quartz.task;
|
package net.maku.quartz.task;
|
||||||
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
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
|
* @author 阿沐 babamu@126.com
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Component
|
@Service
|
||||||
public class TestTask {
|
public class TestTask {
|
||||||
|
|
||||||
public void run(String params) throws InterruptedException {
|
public void run(String params) throws InterruptedException {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user