From 615c4b659a6ab0406750f2b97e2cc89a6c5ef151 Mon Sep 17 00:00:00 2001 From: SimonSun Date: Fri, 25 Jan 2019 14:28:49 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=AE=A1=E7=90=86?= =?UTF-8?q?=E9=A1=B5=E9=9D=A2quartz=E5=90=AF=E5=8A=A8=E6=9A=82=E5=81=9C?= =?UTF-8?q?=E5=A4=B1=E8=B4=A5=E9=97=AE=E9=A2=98=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/src/main/resources/logback.xml | 2 +- .../common/plugins/quartz/QuartzManage.java | 43 ++++- .../simon/controller/QuartzJobController.java | 91 +++++++--- web/src/main/resources/logback.xml | 2 +- .../templates/easyui/quartzJob/add.html | 69 ++++++++ .../templates/easyui/quartzJob/edit.html | 73 ++++++++ .../templates/easyui/quartzJob/list.html | 163 ++++++++++++++++++ 7 files changed, 414 insertions(+), 29 deletions(-) create mode 100644 web/src/main/resources/templates/easyui/quartzJob/add.html create mode 100644 web/src/main/resources/templates/easyui/quartzJob/edit.html create mode 100644 web/src/main/resources/templates/easyui/quartzJob/list.html diff --git a/api/src/main/resources/logback.xml b/api/src/main/resources/logback.xml index 62b8f64..17e0c46 100644 --- a/api/src/main/resources/logback.xml +++ b/api/src/main/resources/logback.xml @@ -73,7 +73,7 @@ - + diff --git a/web/src/main/java/com/simon/common/plugins/quartz/QuartzManage.java b/web/src/main/java/com/simon/common/plugins/quartz/QuartzManage.java index 8b8c01c..29986a5 100644 --- a/web/src/main/java/com/simon/common/plugins/quartz/QuartzManage.java +++ b/web/src/main/java/com/simon/common/plugins/quartz/QuartzManage.java @@ -2,6 +2,7 @@ package com.simon.common.plugins.quartz; import com.simon.model.QuartzJob; import org.quartz.*; +import org.quartz.impl.matchers.GroupMatcher; import org.springframework.stereotype.Component; import javax.annotation.Resource; @@ -23,7 +24,7 @@ public class QuartzManage { public void addJob(QuartzJob job) throws SchedulerException, ClassNotFoundException, IllegalAccessException, InstantiationException { //通过类名获取实体类,即要执行的定时任务的类 Class clazz = Class.forName(job.getBeanName()); - Job jobEntity = (Job)clazz.newInstance(); + Job jobEntity = (Job) clazz.newInstance(); //通过实体类和任务名创建 JobDetail JobDetail jobDetail = JobBuilder.newJob(jobEntity.getClass()) .withDescription(job.getDescription()) @@ -38,11 +39,12 @@ public class QuartzManage { .withSchedule(CronScheduleBuilder.cronSchedule(job.getCronExpression())) .build(); //执行定时任务 - scheduler.scheduleJob(jobDetail,cronTrigger); + scheduler.scheduleJob(jobDetail, cronTrigger); } /** * 更新job cron表达式 + * * @param quartzJob * @throws SchedulerException */ @@ -54,8 +56,10 @@ public class QuartzManage { trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build(); scheduler.rescheduleJob(triggerKey, trigger); } + /** * 删除一个job + * * @param quartzJob * @throws SchedulerException */ @@ -63,8 +67,10 @@ public class QuartzManage { JobKey jobKey = JobKey.jobKey(quartzJob.getJobName()); scheduler.deleteJob(jobKey); } + /** * 恢复一个job + * * @param quartzJob * @throws SchedulerException */ @@ -72,8 +78,10 @@ public class QuartzManage { JobKey jobKey = JobKey.jobKey(quartzJob.getJobName()); scheduler.resumeJob(jobKey); } + /** * 立即执行job + * * @param quartzJob * @throws SchedulerException */ @@ -81,8 +89,10 @@ public class QuartzManage { JobKey jobKey = JobKey.jobKey(quartzJob.getJobName()); scheduler.triggerJob(jobKey); } + /** * 暂停一个job + * * @param quartzJob * @throws SchedulerException */ @@ -90,4 +100,33 @@ public class QuartzManage { JobKey jobKey = JobKey.jobKey(quartzJob.getJobName()); scheduler.pauseJob(jobKey); } + + /** + * 判断任务是否正在运行 + * @param name 任务名称 + * @return 是否正在运行 + */ + public boolean isRun(String name) { + try { + for(String groupName : scheduler.getJobGroupNames()){ + for(JobKey jobKey : scheduler.getJobKeys(GroupMatcher.jobGroupEquals(groupName))){ + if (name.equals(jobKey.getName())){ + return true; + } + } + } + } catch (SchedulerException e) { + e.printStackTrace(); + } + return false; + } + + /** + * 判断任务是否正在运行 + * @param quartzJob 任务 + * @return 是否正在运行 + */ + public boolean isRun(QuartzJob quartzJob){ + return isRun(quartzJob.getJobName()); + } } diff --git a/web/src/main/java/com/simon/controller/QuartzJobController.java b/web/src/main/java/com/simon/controller/QuartzJobController.java index e684cc7..fa0bff4 100644 --- a/web/src/main/java/com/simon/controller/QuartzJobController.java +++ b/web/src/main/java/com/simon/controller/QuartzJobController.java @@ -6,6 +6,7 @@ import com.simon.common.domain.ResultMsg; import com.simon.common.domain.UserEntity; import com.simon.common.plugins.quartz.QuartzManage; import com.simon.model.QuartzJob; +import com.simon.service.DictTypeService; import com.simon.service.QuartzJobService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; @@ -16,6 +17,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.core.Authentication; import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; import org.springframework.web.bind.annotation.*; import springfox.documentation.annotations.ApiIgnore; @@ -24,40 +26,59 @@ import java.util.LinkedHashMap; import java.util.Map; /** -* quartz任务 -* -* @author SimonSun -* @date 2018-12-22 -**/ + * quartz任务 + * + * @author SimonSun + * @date 2018-12-22 + **/ @Slf4j @Api(description = "quartz任务") @Controller @RequestMapping("/api/quartzJobs") -public class QuartzJobController extends BaseController{ +public class QuartzJobController extends BaseController { @Autowired private QuartzJobService quartzJobService; + @Autowired + private DictTypeService dictTypeService; + @Autowired private QuartzManage quartzManage; @ApiIgnore @ApiOperation(value = "列表页面") - @GetMapping(params = "easyui-list") - public String getEasyUIList(){ - return "easyui/quartz_job"; + @GetMapping("list") + public String list(Model model) { + model.addAttribute("jobStatusList", dictTypeService.getTypeByGroupCode("job_status")); + return "easyui/quartzJob/list"; + } + + @ApiIgnore + @ApiOperation(value = "新增页面") + @GetMapping("add") + public String add() { + return "easyui/quartzJob/add"; + } + + @ApiIgnore + @ApiOperation(value = "编辑页面") + @GetMapping("edit") + public String edit(@RequestParam Long id, Model model) { + model.addAttribute("quartzJob", quartzJobService.findById(id)); + return "easyui/quartzJob/edit"; } @ApiIgnore @ApiOperation(value = "列表数据") - @GetMapping("easyui/list") + @GetMapping("data") @ResponseBody public EasyUIDataGridResult getEasyUIList( @ApiParam(value = "类名") @RequestParam(required = false) String beanName, @ApiParam(value = "job状态") @RequestParam(required = false) Integer jobStatus, @ApiParam(value = "页码", defaultValue = "1", required = true) @RequestParam Integer pageNo, - @ApiParam(value = "每页条数", defaultValue = "10", required = true)@RequestParam Integer pageSize, - @ApiParam(value = "排序")@RequestParam(required = false, defaultValue = "") String orderBy){ + @ApiParam(value = "每页条数", defaultValue = "10", required = true) @RequestParam Integer pageSize, + @ApiParam(value = "排序") @RequestParam(required = false, defaultValue = "") String orderBy) { Map params = new LinkedHashMap<>(); params.put("beanName", beanName); params.put("jobStatus", jobStatus); @@ -65,25 +86,39 @@ public class QuartzJobController extends BaseController{ } @ApiOperation(value = "新增") - @PostMapping + @PostMapping("add") @ResponseBody - public ResultMsg add(@RequestBody QuartzJob body){ + public ResultMsg add(@RequestBody QuartzJob body, Authentication authentication) { + Object principal = authentication.getPrincipal(); + UserEntity userEntity = null; + if (principal instanceof UserEntity) { + userEntity = (UserEntity) principal; + } + body.setCreateDate(new Date()); + body.setCreateBy(userEntity.getId()); quartzJobService.insertSelective(body); return ResultMsg.success(); } @ApiOperation(value = "修改") - @PatchMapping + @PatchMapping("edit") @ResponseBody - public ResultMsg update(@RequestBody QuartzJob body){ + public ResultMsg update(@RequestBody QuartzJob body, Authentication authentication) { + Object principal = authentication.getPrincipal(); + UserEntity userEntity = null; + if (principal instanceof UserEntity) { + userEntity = (UserEntity) principal; + } + body.setUpdateDate(new Date()); + body.setUpdateBy(userEntity.getId()); quartzJobService.updateByPrimaryKeySelective(body); return ResultMsg.success(); } @ApiOperation(value = "删除") - @DeleteMapping("/id/{ids}") + @DeleteMapping("/ids/{ids}") @ResponseBody - public ResultMsg delete(@PathVariable String ids){ + public ResultMsg delete(@PathVariable String ids) { quartzJobService.deleteByIds(ids); return ResultMsg.success(); } @@ -96,19 +131,25 @@ public class QuartzJobController extends BaseController{ public ResultMsg operation( Authentication authentication, @PathVariable Long id, - @ApiParam(value = "job状态[0:off, 1:on]", required = true)@PathVariable int jobStatus) throws ClassNotFoundException, InstantiationException, SchedulerException, IllegalAccessException { + @ApiParam(value = "job状态[0:off, 1:on]", required = true) @PathVariable int jobStatus) throws ClassNotFoundException, InstantiationException, SchedulerException, IllegalAccessException { Object principal = authentication.getPrincipal(); UserEntity userEntity = null; - if(principal instanceof UserEntity){ - userEntity = (UserEntity)principal; + if (principal instanceof UserEntity) { + userEntity = (UserEntity) principal; } QuartzJob quartzJob = quartzJobService.findById(id); - if(0 == jobStatus){ - quartzManage.pauseJob(quartzJob); - }else{ - quartzManage.resumeJob(quartzJob); + if (quartzManage.isRun(quartzJob.getJobName())) { + if (0 == jobStatus) { + quartzManage.pauseJob(quartzJob); + } else { + quartzManage.resumeJob(quartzJob); + } + } else { + if (1 == jobStatus) { + quartzManage.addJob(quartzJob); + } } quartzJob.setJobStatus(jobStatus); diff --git a/web/src/main/resources/logback.xml b/web/src/main/resources/logback.xml index a66c4b7..68378ba 100644 --- a/web/src/main/resources/logback.xml +++ b/web/src/main/resources/logback.xml @@ -73,7 +73,7 @@ - + diff --git a/web/src/main/resources/templates/easyui/quartzJob/add.html b/web/src/main/resources/templates/easyui/quartzJob/add.html new file mode 100644 index 0000000..d2069a5 --- /dev/null +++ b/web/src/main/resources/templates/easyui/quartzJob/add.html @@ -0,0 +1,69 @@ + + + + +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ + +
+
+
+
+
+ + + \ No newline at end of file diff --git a/web/src/main/resources/templates/easyui/quartzJob/edit.html b/web/src/main/resources/templates/easyui/quartzJob/edit.html new file mode 100644 index 0000000..5de43c7 --- /dev/null +++ b/web/src/main/resources/templates/easyui/quartzJob/edit.html @@ -0,0 +1,73 @@ + + + + +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ + +
+
+
+
+
+ + + \ No newline at end of file diff --git a/web/src/main/resources/templates/easyui/quartzJob/list.html b/web/src/main/resources/templates/easyui/quartzJob/list.html new file mode 100644 index 0000000..61f2398 --- /dev/null +++ b/web/src/main/resources/templates/easyui/quartzJob/list.html @@ -0,0 +1,163 @@ + + + + +
+
+ 类名: + 任务状态: + + +
+
+ + + +
+
+ + + + + + + + + + + + + + + + + + + + + +
id创建人id创建时间更新人id更新时间cron表达式任务调用的方法名任务是否有状态描述任务执行时调用哪个类的方法 包名+类名,完全限定名触发器名称任务状态spring_bean任务名操作
+
+
+
+ + + \ No newline at end of file -- Gitee From faf43a51c95f6cc1d0dd618476c353a54eafcfbb Mon Sep 17 00:00:00 2001 From: SimonSun Date: Fri, 25 Jan 2019 16:52:22 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E8=B0=83=E6=95=B4=E9=A1=B5=E9=9D=A2?= =?UTF-8?q?=E7=BB=86=E8=8A=82=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/resources/templates/code/list.ftl | 8 ++--- .../templates/easyui/quartzJob/add.html | 2 +- .../templates/easyui/quartzJob/edit.html | 2 +- .../easyui/roleAuthority/auth_config.html | 2 +- .../templates/easyui/roleAuthority/list.html | 33 +++++++++---------- .../main/resources/templates/index_v1.html | 12 +++---- 6 files changed, 28 insertions(+), 31 deletions(-) diff --git a/web/src/main/resources/templates/code/list.ftl b/web/src/main/resources/templates/code/list.ftl index 0bfbfc5..f94cf14 100644 --- a/web/src/main/resources/templates/code/list.ftl +++ b/web/src/main/resources/templates/code/list.ftl @@ -50,16 +50,16 @@ <#list columns as column> <#switch column.easyuiType> <#case "rich_text"> - ${(column.comment)} + ${(column.comment)} <#break> <#case "image"> - ${(column.comment)} + ${(column.comment)} <#break> <#case "t:dict"> - ${(column.comment)} + ${(column.comment)} <#break> <#default> - ${(column.comment)} + ${(column.comment)} diff --git a/web/src/main/resources/templates/easyui/quartzJob/add.html b/web/src/main/resources/templates/easyui/quartzJob/add.html index d2069a5..6fb37ea 100644 --- a/web/src/main/resources/templates/easyui/quartzJob/add.html +++ b/web/src/main/resources/templates/easyui/quartzJob/add.html @@ -16,7 +16,7 @@
- +
diff --git a/web/src/main/resources/templates/easyui/quartzJob/edit.html b/web/src/main/resources/templates/easyui/quartzJob/edit.html index 5de43c7..375f977 100644 --- a/web/src/main/resources/templates/easyui/quartzJob/edit.html +++ b/web/src/main/resources/templates/easyui/quartzJob/edit.html @@ -19,7 +19,7 @@
- +
diff --git a/web/src/main/resources/templates/easyui/roleAuthority/auth_config.html b/web/src/main/resources/templates/easyui/roleAuthority/auth_config.html index 0a479dc..fc14af0 100644 --- a/web/src/main/resources/templates/easyui/roleAuthority/auth_config.html +++ b/web/src/main/resources/templates/easyui/roleAuthority/auth_config.html @@ -19,7 +19,7 @@

Tree nodes with check boxes.

--> - - +
-
+
- - - - - - - - - - - + + + + + + + + + + +
id创建人id创建时间更新人id更新时间字典编码字典编码名称字典组id字典组编码排序操作id创建人id创建时间更新人id更新时间字典编码字典编码名称字典组id字典组编码排序操作
diff --git a/web/src/main/resources/templates/index_v1.html b/web/src/main/resources/templates/index_v1.html index 35ca734..c511a41 100644 --- a/web/src/main/resources/templates/index_v1.html +++ b/web/src/main/resources/templates/index_v1.html @@ -66,7 +66,7 @@ - - + -->