修改 表格 2021年11月10日18:05:04
parent
bbdd1a16cb
commit
09f4d9be0c
|
|
@ -31,12 +31,6 @@
|
|||
<version>1.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.zhangmeng</groupId>
|
||||
<artifactId>mystyle-cloud-model</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package com.zhangmeng.admin.manager.controller;
|
|||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.zhangmeng.admin.manager.feign.ArticleFeign;
|
||||
import com.zhangmeng.admin.manager.feign.CategoryFeign;
|
||||
import com.zhangmeng.admin.manager.feign.QuartzFeign;
|
||||
import com.zhangmeng.admin.manager.service.PermissionService;
|
||||
import com.zhangmeng.admin.manager.service.RoleService;
|
||||
import com.zhangmeng.admin.manager.service.SysLogService;
|
||||
|
|
@ -22,6 +23,8 @@ import org.springframework.web.bind.annotation.ResponseBody;
|
|||
import org.springframework.web.servlet.ModelAndView;
|
||||
import springfox.documentation.annotations.ApiIgnore;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
|
|
@ -53,6 +56,9 @@ public class UrlRequestController extends BaseController {
|
|||
@Autowired
|
||||
private CategoryFeign categoryFeign;
|
||||
|
||||
@Autowired
|
||||
private QuartzFeign quartzFeign;
|
||||
|
||||
//跳转首页
|
||||
@GetMapping({"/login","/"})
|
||||
public ModelAndView login (){
|
||||
|
|
@ -302,4 +308,31 @@ public class UrlRequestController extends BaseController {
|
|||
public ModelAndView file_upload(){
|
||||
return this.jumpPage("admin/file/upload");
|
||||
}
|
||||
|
||||
|
||||
@ApiIgnore
|
||||
@GetMapping("/quartz/index")
|
||||
public ModelAndView quartz_index(){
|
||||
return this.jumpPage("admin/job/job-list");
|
||||
}
|
||||
|
||||
@ApiIgnore
|
||||
@GetMapping("/quartz/add")
|
||||
public ModelAndView quartz_add(Model model){
|
||||
Set<String> names = this.quartzFeign.spring_bean_list();
|
||||
model.addAttribute("spring_bean_list",names);
|
||||
return this.jumpPage("admin/job/job-add");
|
||||
}
|
||||
|
||||
@ApiIgnore
|
||||
@GetMapping("/quartz/edit")
|
||||
public ModelAndView quartz_edit(Model model,String jobId){
|
||||
Set<String> names = this.quartzFeign.spring_bean_list();
|
||||
model.addAttribute("spring_bean_list",names);
|
||||
if (CommonUtil.isNotNull(jobId)){
|
||||
QuartzJob job = this.quartzFeign.findById(Long.parseLong(jobId));
|
||||
model.addAttribute("job",job);
|
||||
}
|
||||
return this.jumpPage("admin/job/job-edit");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,19 @@
|
|||
package com.zhangmeng.admin.manager.feign;
|
||||
|
||||
import com.zhangmeng.model.dto.SysConstant;
|
||||
import com.zhangmeng.model.entity.QuartzJob;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
@FeignClient(SysConstant.mystyle_cloud_quartz)
|
||||
public interface QuartzFeign {
|
||||
|
||||
@GetMapping("/quartz/spring_bean_list")
|
||||
public Set<String> spring_bean_list();
|
||||
|
||||
@GetMapping("/quartz/findById")
|
||||
public QuartzJob findById(@RequestParam("jobId") Long jobId);
|
||||
}
|
||||
|
|
@ -79,6 +79,17 @@
|
|||
{{# } }}
|
||||
</script>
|
||||
|
||||
<script type="text/html" id="videoImages">
|
||||
{{#
|
||||
if(d.videoImages != null){
|
||||
}}
|
||||
<img src="{{d.videoImages}}" width="256px" height="150px">
|
||||
{{#
|
||||
console.log(d.videoImages)
|
||||
}
|
||||
}}
|
||||
</script>
|
||||
|
||||
<script type="text/html" id="uploadType">
|
||||
{{#if (d.uploadType == "images") { }}
|
||||
<span>图片</span>
|
||||
|
|
@ -157,22 +168,24 @@
|
|||
{
|
||||
title: '视频的封面',
|
||||
field: 'videoImages',
|
||||
templet:"#videoImages",
|
||||
align: 'center',
|
||||
width: 156
|
||||
},
|
||||
|
||||
// {
|
||||
// title: '视频封面的宽',
|
||||
// field: 'videoImagesWidth',
|
||||
// align: 'center',
|
||||
// width: 100
|
||||
// },
|
||||
//
|
||||
// {
|
||||
// title: '视频封面的高',
|
||||
// field: 'videoImagesHeight',
|
||||
// align: 'center',
|
||||
// width: 100
|
||||
// },
|
||||
{
|
||||
title: '视频封面的宽',
|
||||
field: 'videoImagesWidth',
|
||||
align: 'center',
|
||||
width: 100
|
||||
},
|
||||
|
||||
{
|
||||
title: '视频封面的高',
|
||||
field: 'videoImagesHeight',
|
||||
align: 'center',
|
||||
width: 100
|
||||
},
|
||||
{
|
||||
title: '操作',
|
||||
toolbar: '#file-bar',
|
||||
|
|
|
|||
|
|
@ -0,0 +1,45 @@
|
|||
package com.zhangmeng.api.service.quartz;
|
||||
|
||||
import com.zhangmeng.model.entity.QuartzJob;
|
||||
import com.zhangmeng.model.vo.Result;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
@Api(tags = "定时任务")
|
||||
public interface QuartzControllerApi {
|
||||
|
||||
@ApiOperation("添加定时任务")
|
||||
public Result save(@RequestParam Map<String, Object> map);
|
||||
|
||||
@ApiOperation("定时任务列表")
|
||||
public Result list(String jobName,String beginTime ,String endTime,Integer pageNum, Integer pageSize);
|
||||
|
||||
@ApiOperation("修改定时任务")
|
||||
public Result update(@RequestBody QuartzJob job);
|
||||
|
||||
@ApiOperation("根据id获取定时任务")
|
||||
public QuartzJob getById(@PathVariable Long id);
|
||||
|
||||
@ApiOperation("删除定时任务")
|
||||
public Result delete( @RequestParam("jobId") Long jobId);
|
||||
|
||||
@ApiOperation("批量删除定时任务")
|
||||
public Result batchRemove(@RequestParam("ids") String ids);
|
||||
|
||||
@ApiOperation(value = "校验cron表达式")
|
||||
public boolean checkCron(String cron);
|
||||
|
||||
@ApiOperation(value = "springBean名字")
|
||||
public List<String> listAllBeanName();
|
||||
|
||||
@ApiOperation(value = "springBean的无参方法")
|
||||
public Set<String> listMethodName(@PathVariable String name);
|
||||
|
||||
@GetMapping("/spring_bean_list")
|
||||
public Set<String> spring_bean_list();
|
||||
}
|
||||
|
|
@ -58,5 +58,11 @@ spring:
|
|||
uri: lb://mystyle-cloud-file
|
||||
predicates:
|
||||
- Path=/mystyle-cloud-file/**
|
||||
filters:
|
||||
- StripPrefix=1
|
||||
- id: mystyle-cloud-quartz
|
||||
uri: lb://mystyle-cloud-quartz
|
||||
predicates:
|
||||
- Path=/mystyle-cloud-quartz/**
|
||||
filters:
|
||||
- StripPrefix=1
|
||||
|
|
@ -11,4 +11,5 @@ public class SysConstant {
|
|||
|
||||
public static final String mystyle_cloud_blog = "mystyle-cloud-blog";
|
||||
|
||||
public static final String mystyle_cloud_quartz = "mystyle-cloud-quartz";
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,60 @@
|
|||
package com.zhangmeng.model.entity;
|
||||
|
||||
import com.zhangmeng.model.base.baseEntity.BaseEntity;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Table;
|
||||
|
||||
/**
|
||||
* 定时任务
|
||||
*
|
||||
* @author zhengmeng
|
||||
* @version 1.0
|
||||
* @date 2021年1月7日17:44:40
|
||||
*/
|
||||
@NoArgsConstructor
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@Entity
|
||||
@EqualsAndHashCode(callSuper = false)
|
||||
@Table(name = "job")
|
||||
public class QuartzJob extends BaseEntity<Long> {
|
||||
|
||||
public enum Status {
|
||||
|
||||
DELETE("已停用"),
|
||||
VALID("正常");
|
||||
|
||||
Status(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
private String description;//描述
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
}
|
||||
|
||||
private String jobName;
|
||||
|
||||
private String description;
|
||||
|
||||
private String cron;
|
||||
|
||||
private String springBeanName;
|
||||
|
||||
private String methodName;
|
||||
|
||||
private Boolean isSysJob;
|
||||
|
||||
private Status status;
|
||||
}
|
||||
|
|
@ -0,0 +1,55 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>mystyle-cloud-parent</artifactId>
|
||||
<groupId>com.zhangmeng</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>mystyle-cloud-quartz</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.zhangmeng</groupId>
|
||||
<artifactId>mystyle-cloud-api</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
<!--定时任务-->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-quartz</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-sleuth</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-sleuth-zipkin</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
package com.zhangmeng.quartz;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.autoconfigure.domain.EntityScan;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableDiscoveryClient
|
||||
@EnableFeignClients
|
||||
@EntityScan("com.zhangmeng.model.entity")
|
||||
@ComponentScan(basePackages = {"com.zhangmeng.model","com.zhangmeng.quartz","com.zhangmeng.api"})
|
||||
public class QuartzApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(QuartzApplication.class);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
package com.zhangmeng.quartz.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
@Configuration
|
||||
public class QuartzJobConfig {
|
||||
|
||||
public static final String KEY = "applicationContextSchedulerContextKey";
|
||||
|
||||
@Bean("adminQuartzScheduler")
|
||||
public SchedulerFactoryBean quartzScheduler(DataSource dataSource) {
|
||||
SchedulerFactoryBean quartzScheduler = new SchedulerFactoryBean();
|
||||
quartzScheduler.setDataSource(dataSource);
|
||||
quartzScheduler.setOverwriteExistingJobs(true);
|
||||
quartzScheduler.setApplicationContextSchedulerContextKey(KEY);
|
||||
quartzScheduler.setStartupDelay(10);
|
||||
return quartzScheduler;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,192 @@
|
|||
package com.zhangmeng.quartz.controller;
|
||||
|
||||
import com.zhangmeng.api.service.quartz.QuartzControllerApi;
|
||||
import com.zhangmeng.model.base.baseController.BaseController;
|
||||
import com.zhangmeng.model.base.baseUtil.CommonUtil;
|
||||
import com.zhangmeng.model.dto.query.QueryParams;
|
||||
import com.zhangmeng.model.entity.QuartzJob;
|
||||
import com.zhangmeng.model.vo.Result;
|
||||
import com.zhangmeng.model.vo.StatusCode;
|
||||
import com.zhangmeng.quartz.service.QuartzJobService;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.quartz.CronExpression;
|
||||
import org.springframework.aop.support.AopUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import tk.mybatis.mapper.entity.Condition;
|
||||
import tk.mybatis.mapper.entity.Example;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.*;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/quartz")
|
||||
public class QuartzController extends BaseController implements QuartzControllerApi {
|
||||
|
||||
@Autowired
|
||||
private QuartzJobService quartzJobService;
|
||||
|
||||
@Autowired
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
@Override
|
||||
@PostMapping("/save")
|
||||
public Result save(@RequestParam Map<String, Object> map) {
|
||||
QuartzJob job = null;
|
||||
boolean flag = false;
|
||||
String jobId = CommonUtil.map_get_value(map, "jobId");
|
||||
String jobName = CommonUtil.map_get_value(map, "jobName");
|
||||
String cron = CommonUtil.map_get_value(map, "cron");
|
||||
String description = CommonUtil.map_get_value(map, "description");
|
||||
String methodName = CommonUtil.map_get_value(map, "methodName");
|
||||
if (jobId == null || jobId.equals("")){
|
||||
Condition condition = new Condition(QuartzJob.class);
|
||||
Example.Criteria criteria = condition.createCriteria();
|
||||
criteria.andEqualTo("jobName", jobName);
|
||||
List<QuartzJob> jobs = this.quartzJobService.findByCondition(condition);
|
||||
if (jobs.size() > 0) {
|
||||
throw new IllegalArgumentException(jobName + "已存在");
|
||||
}
|
||||
job =new QuartzJob();
|
||||
job.setAddTime(new Date());
|
||||
job.setUpdateTime(new Date());
|
||||
job.setDeleteStatus(false);
|
||||
flag = true;
|
||||
}else {
|
||||
job = this.quartzJobService.findById(Long.parseLong(jobId));
|
||||
}
|
||||
job.setDescription(description);
|
||||
job.setCron(cron);
|
||||
job.setJobName(jobName);
|
||||
job.setStatus(QuartzJob.Status.VALID);
|
||||
job.setSpringBeanName("jobManageInterfaceImpl");
|
||||
job.setMethodName(methodName);
|
||||
job.setIsSysJob(false);
|
||||
|
||||
if (flag){
|
||||
return this.quartzJobService.saveJob(job);
|
||||
}else {
|
||||
return this.update(job);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@GetMapping("/list")
|
||||
public Result list(String jobName,String beginTime ,String endTime,Integer pageNum, Integer pageSize) {
|
||||
Condition condition = new Condition(QuartzJob.class);
|
||||
Example.Criteria criteria = condition.createCriteria();
|
||||
if (CommonUtil.isNotNull(jobName)){
|
||||
criteria.andEqualTo("jobName",jobName);
|
||||
}
|
||||
if (CommonUtil.isNotNull(beginTime) && CommonUtil.isNotNull(endTime)){
|
||||
criteria.andBetween("addTime",CommonUtil.data_format(beginTime, CommonUtil.Format.YYYY_MM_DD_HH_MM_SS),CommonUtil.data_format(endTime, CommonUtil.Format.YYYY_MM_DD_HH_MM_SS));
|
||||
}
|
||||
List<QuartzJob> jobList = this.quartzJobService.findByCondition(new QueryParams(pageNum, pageSize, condition));
|
||||
return new Result(true, StatusCode.OK,"查询成功",jobList);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
@PostMapping("/update")
|
||||
public Result update(@RequestBody QuartzJob job) {
|
||||
job.setStatus(QuartzJob.Status.VALID);
|
||||
return this.quartzJobService.saveJob(job);
|
||||
}
|
||||
|
||||
@Override
|
||||
@GetMapping("/{id}")
|
||||
public QuartzJob getById(@PathVariable Long id) {
|
||||
return this.quartzJobService.findById(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
@PostMapping("/delete")
|
||||
public Result delete(@RequestParam("jobId") Long jobId) {
|
||||
return quartzJobService.deleteJob(jobId);
|
||||
}
|
||||
|
||||
@Override
|
||||
@PostMapping("/batchRemove")
|
||||
public Result batchRemove( String ids) {
|
||||
String id_s = CommonUtil.firstLastComma(ids);
|
||||
String[] strings = id_s.split(",");
|
||||
if (strings.length > 0){
|
||||
for (String id : strings) {
|
||||
this.quartzJobService.deleteJob(Long.parseLong(id));
|
||||
}
|
||||
}
|
||||
return new Result(true,StatusCode.OK,"删除成功");
|
||||
}
|
||||
|
||||
@Override
|
||||
@GetMapping("/cron")
|
||||
public boolean checkCron(String cron) {
|
||||
return CronExpression.isValidExpression(cron);
|
||||
}
|
||||
|
||||
@Override
|
||||
@GetMapping("/beans")
|
||||
public List<String> listAllBeanName() {
|
||||
String[] strings = applicationContext.getBeanDefinitionNames();
|
||||
List<String> list = new ArrayList<>();
|
||||
for (String str : strings) {
|
||||
if (str.contains(".")) {
|
||||
continue;
|
||||
}
|
||||
Class<?> clazz = getClass(str);
|
||||
if (clazz.isAnnotationPresent(Service.class) && str.toLowerCase().contains("service")) {
|
||||
list.add(str);
|
||||
}
|
||||
}
|
||||
Collections.sort(list);
|
||||
return list;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
@GetMapping("/beans/{name}")
|
||||
public Set<String> listMethodName(@PathVariable String name) {
|
||||
Class<?> clazz = getClass(name);
|
||||
Method[] methods = clazz.getDeclaredMethods();
|
||||
Set<String> names = new HashSet<>();
|
||||
Arrays.asList(methods).forEach(m -> {
|
||||
int b = m.getModifiers();
|
||||
if (Modifier.isPublic(b)) {
|
||||
Class<?>[] classes = m.getParameterTypes();
|
||||
if (classes.length == 0) {
|
||||
names.add(m.getName());
|
||||
}
|
||||
}
|
||||
});
|
||||
return names;
|
||||
}
|
||||
|
||||
@GetMapping("/spring_bean_list")
|
||||
public Set<String> spring_bean_list(){
|
||||
Class<?> clazz = getClass("jobManageInterfaceImpl");
|
||||
Method[] methods = clazz.getDeclaredMethods();
|
||||
Set<String> names = new HashSet<>();
|
||||
Arrays.asList(methods).forEach(m -> {
|
||||
int b = m.getModifiers();
|
||||
if (Modifier.isPublic(b)) {
|
||||
Class<?>[] classes = m.getParameterTypes();
|
||||
if (classes.length == 0) {
|
||||
names.add(m.getName());
|
||||
}
|
||||
}
|
||||
});
|
||||
return names;
|
||||
}
|
||||
|
||||
private Class<?> getClass(String name) {
|
||||
Object object = applicationContext.getBean(name);
|
||||
Class<?> clazz = object.getClass();
|
||||
if (AopUtils.isAopProxy(object)) {
|
||||
clazz = clazz.getSuperclass();
|
||||
}
|
||||
return clazz;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
package com.zhangmeng.quartz.dao;
|
||||
|
||||
import com.zhangmeng.model.base.baseDao.AbstractBaseMapper;
|
||||
import com.zhangmeng.model.entity.QuartzJob;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
@Mapper
|
||||
public interface QuartzJobDao extends AbstractBaseMapper<QuartzJob> {
|
||||
}
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
package com.zhangmeng.quartz.dto;
|
||||
|
||||
import com.zhangmeng.quartz.config.QuartzJobConfig;
|
||||
import com.zhangmeng.quartz.service.QuartzJobService;
|
||||
import org.quartz.JobExecutionContext;
|
||||
import org.quartz.JobExecutionException;
|
||||
import org.quartz.SchedulerException;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.scheduling.quartz.QuartzJobBean;
|
||||
|
||||
public class SpringBeanJob extends QuartzJobBean {
|
||||
|
||||
@Override
|
||||
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
|
||||
try {
|
||||
ApplicationContext applicationContext = (ApplicationContext) context.getScheduler().getContext().get(QuartzJobConfig.KEY);
|
||||
QuartzJobService jobService = applicationContext.getBean(QuartzJobService.class);
|
||||
jobService.doJob(context.getJobDetail().getJobDataMap());
|
||||
} catch (SchedulerException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
package com.zhangmeng.quartz.job;
|
||||
|
||||
/**
|
||||
* 定时任务接口
|
||||
*/
|
||||
|
||||
public interface JobManageInterface {
|
||||
|
||||
public void mail_execute();
|
||||
|
||||
public void fiction_links();
|
||||
}
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
package com.zhangmeng.quartz.job;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import tk.mybatis.mapper.entity.Condition;
|
||||
import tk.mybatis.mapper.entity.Example;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class JobManageInterfaceImpl implements JobManageInterface {
|
||||
|
||||
private Logger logger = LoggerFactory.getLogger(JobManageInterfaceImpl.class);
|
||||
|
||||
@Override
|
||||
public void mail_execute() {
|
||||
System.out.println("定时任务执行.....");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fiction_links() {
|
||||
logger.info("小说链接定时任务执行结束:用时-------》");
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
package com.zhangmeng.quartz.service;
|
||||
|
||||
import com.zhangmeng.model.base.baseService.BaseService;
|
||||
import com.zhangmeng.model.entity.QuartzJob;
|
||||
import com.zhangmeng.model.vo.Result;
|
||||
import org.quartz.JobDataMap;
|
||||
|
||||
public interface QuartzJobService extends BaseService<QuartzJob> {
|
||||
|
||||
public Result saveJob(QuartzJob job);
|
||||
|
||||
public Result list(Integer pageNum, Integer pageSize);
|
||||
|
||||
public Result deleteJob(Long id);
|
||||
|
||||
public void doJob(JobDataMap jobDataMap);
|
||||
}
|
||||
|
|
@ -0,0 +1,162 @@
|
|||
package com.zhangmeng.quartz.service.impl;
|
||||
|
||||
import com.github.pagehelper.PageHelper;
|
||||
import com.github.pagehelper.PageInfo;
|
||||
import com.zhangmeng.model.base.baseService.impl.AbstractBaseServiceImpl;
|
||||
import com.zhangmeng.model.base.baseUtil.CommonUtil;
|
||||
import com.zhangmeng.model.entity.QuartzJob;
|
||||
import com.zhangmeng.model.vo.Result;
|
||||
import com.zhangmeng.model.vo.StatusCode;
|
||||
import com.zhangmeng.quartz.dto.SpringBeanJob;
|
||||
import com.zhangmeng.quartz.service.QuartzJobService;
|
||||
import org.quartz.*;
|
||||
import org.springframework.aop.support.AopUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.stereotype.Service;
|
||||
import tk.mybatis.mapper.entity.Condition;
|
||||
import tk.mybatis.mapper.entity.Example;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
@Service
|
||||
public class QuartzJobServiceImpl extends AbstractBaseServiceImpl<QuartzJob> implements QuartzJobService {
|
||||
|
||||
|
||||
@Qualifier("adminQuartzScheduler")
|
||||
@Autowired
|
||||
private Scheduler scheduler;
|
||||
|
||||
@Autowired
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
private static final String JOB_DATA_KEY = "JOB_DATA_KEY";
|
||||
|
||||
@Override
|
||||
public Result saveJob(QuartzJob job){
|
||||
|
||||
this.checkJobModel(job);
|
||||
String name = job.getJobName();
|
||||
JobKey jobKey = JobKey.jobKey(name);
|
||||
JobDetail jobDetail = JobBuilder.newJob(SpringBeanJob.class).storeDurably().withDescription(job.getDescription()).withIdentity(jobKey).build();
|
||||
CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(job.getCron());
|
||||
CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity(name).withSchedule(cronScheduleBuilder).forJob(jobKey).build();
|
||||
jobDetail.getJobDataMap().put(JOB_DATA_KEY, job);
|
||||
String message = null;
|
||||
try {
|
||||
boolean exists = scheduler.checkExists(jobKey);
|
||||
if (exists) {
|
||||
scheduler.rescheduleJob(new TriggerKey(name), cronTrigger);
|
||||
scheduler.addJob(jobDetail, true);
|
||||
} else {
|
||||
scheduler.scheduleJob(jobDetail, cronTrigger);
|
||||
}
|
||||
Condition condition = new Condition(Job.class);
|
||||
Example.Criteria criteria = condition.createCriteria();
|
||||
criteria.andEqualTo("jobName",name);
|
||||
List<QuartzJob> jobs = this.findByCondition(condition);
|
||||
if (jobs.size() == 0) {
|
||||
this.save(job);
|
||||
message = "添加成功";
|
||||
} else {
|
||||
this.update(job);
|
||||
message = "修改成功";
|
||||
}
|
||||
} catch (SchedulerException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return new Result(true, StatusCode.OK,message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result list(Integer pageNum, Integer pageSize) {
|
||||
if (CommonUtil.isNull(pageNum) || CommonUtil.isNull(pageSize)){
|
||||
pageNum = 1 ;
|
||||
pageSize = 10 ;
|
||||
}
|
||||
PageHelper.startPage(pageNum,pageSize,"addTime desc");
|
||||
List<QuartzJob> jobList = this.findAll();
|
||||
PageInfo<QuartzJob> pageInfo = new PageInfo<>(jobList);
|
||||
return new Result(true,StatusCode.OK,"查询成功",pageInfo.getTotal(),pageInfo.getList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doJob(JobDataMap jobDataMap) {
|
||||
QuartzJob job = (QuartzJob) jobDataMap.get(JOB_DATA_KEY);
|
||||
String beanName = job.getSpringBeanName();
|
||||
String methodName = job.getMethodName();
|
||||
Object object = applicationContext.getBean(beanName);
|
||||
try {
|
||||
Method method = object.getClass().getDeclaredMethod(methodName);
|
||||
method.invoke(object);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除job
|
||||
* @param id jobId
|
||||
*/
|
||||
@Override
|
||||
public Result deleteJob(Long id) {
|
||||
QuartzJob jobModel = this.findById(id);
|
||||
|
||||
if (jobModel.getIsSysJob() != null && jobModel.getIsSysJob()) {
|
||||
throw new IllegalArgumentException("该job是系统任务,不能删除,因为此job是在代码里初始化的,删除该类job请先确保相关代码已经去除");
|
||||
}
|
||||
|
||||
String jobName = jobModel.getJobName();
|
||||
JobKey jobKey = JobKey.jobKey(jobName);
|
||||
|
||||
try {
|
||||
scheduler.pauseJob(jobKey);
|
||||
scheduler.unscheduleJob(new TriggerKey(jobName));
|
||||
scheduler.deleteJob(jobKey);
|
||||
} catch (SchedulerException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
jobModel.setStatus(QuartzJob.Status.DELETE);
|
||||
this.deleteById(jobModel.getId());
|
||||
return new Result(true, StatusCode.OK,"删除成功");
|
||||
}
|
||||
|
||||
private void checkJobModel(QuartzJob job) {
|
||||
String springBeanName = job.getSpringBeanName();
|
||||
boolean flag = applicationContext.containsBean(springBeanName);
|
||||
if (!flag) {
|
||||
throw new IllegalArgumentException("bean:" + springBeanName + "不存在,bean名如userServiceImpl,首字母小写");
|
||||
}
|
||||
|
||||
Object object = applicationContext.getBean(springBeanName);
|
||||
Class<?> clazz = object.getClass();
|
||||
if (AopUtils.isAopProxy(object)) {
|
||||
clazz = clazz.getSuperclass();
|
||||
}
|
||||
|
||||
String methodName = job.getMethodName();
|
||||
Method[] methods = clazz.getDeclaredMethods();
|
||||
|
||||
Set<String> names = new HashSet<>();
|
||||
Arrays.asList(methods).forEach(m -> {
|
||||
Class<?>[] classes = m.getParameterTypes();
|
||||
if (classes.length == 0) {
|
||||
names.add(m.getName());
|
||||
}
|
||||
});
|
||||
|
||||
if (names.size() == 0) {
|
||||
throw new IllegalArgumentException("该bean没有无参方法");
|
||||
}
|
||||
|
||||
if (!names.contains(methodName)) {
|
||||
throw new IllegalArgumentException("未找到无参方法" + methodName + ",该bean所有方法名为:" + names);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,46 @@
|
|||
server:
|
||||
port: 31008
|
||||
spring:
|
||||
application:
|
||||
name: mystyle-cloud-quartz
|
||||
datasource:
|
||||
username: root
|
||||
password: root
|
||||
url: jdbc:mysql://127.0.0.1:3306/mystyle-blog?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
jpa:
|
||||
database: mysql
|
||||
hibernate:
|
||||
ddl-auto: update
|
||||
naming:
|
||||
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
|
||||
show-sql: true
|
||||
properties:
|
||||
hibernate:
|
||||
dialect: org.hibernate.dialect.MySQL5Dialect
|
||||
zipkin:
|
||||
sender:
|
||||
type: web
|
||||
base-url: http://localhost:9411/
|
||||
service:
|
||||
name: mystyle-cloud-quartz
|
||||
sleuth:
|
||||
sampler:
|
||||
probability: 1
|
||||
cloud:
|
||||
nacos:
|
||||
discovery:
|
||||
server-addr: 127.0.0.1:8848
|
||||
feign:
|
||||
sentinel:
|
||||
enabled: true
|
||||
mybatis:
|
||||
type-aliases-package: com.zhangmeng.model.entity
|
||||
configuration:
|
||||
mapUnderscoreToCamelCase: true
|
||||
default-enum-type-handler: org.apache.ibatis.type.EnumOrdinalTypeHandler
|
||||
mapper:
|
||||
style: normal
|
||||
enum-as-simple-type: true
|
||||
identity: MYSQL
|
||||
check-example-entity-class: true
|
||||
|
|
@ -0,0 +1 @@
|
|||
-----BEGIN PUBLIC KEY-----MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAizuj0fBV2+dj4lM3G6efKYvC2czd07BqmzV++E2yBguVks3XWvsW8qlzmG+t1XBCnRFDI/t1Ddc/Jsnlfy4YzRN8otb/Xn6Yz9ACFvZIPGx/q0cqcrgVaR9rSQiSzsGTgUGHNJk8r3A4w9PSSB552Z9s6p5TsWK5ezlfgg+2ANKn1eJ6R/hzajS/B1bTAqYcl9ddo7prneoeAN5LjlMhc2e0cSVgQt8ALP+4x/bTMnDkMjG6R8lnDAxE27B2ZPaLOIOjkUMK+9mZa4RNBoCDG6J/fwPD1NUoVRCbyr/TVaS4EzyhfNK1QW3BlZ0NLSI/SFD3eryKaFQdacJHS31neQIDAQAB-----END PUBLIC KEY-----
|
||||
Loading…
Reference in New Issue