springboot动态添加定时任务

匿名 (未验证) 提交于 2019-12-02 23:34:01

需要的maven依赖

 <dependency>             <groupId>javax</groupId>             <artifactId>javaee-api</artifactId>             <version>7.0</version>             <scope>provided</scope>         </dependency>          <dependency>             <groupId>com.google.code.gson</groupId>             <artifactId>gson</artifactId>             <version>2.8.2</version>         </dependency>          <dependency>             <groupId>org.quartz-scheduler</groupId>             <artifactId>quartz</artifactId>             <version>2.2.1</version>         </dependency>          <dependency>             <groupId>org.projectlombok</groupId>             <artifactId>lombok</artifactId>             <optional>true</optional>         </dependency>         <dependency>             <groupId>com.baomidou</groupId>             <artifactId>mybatis-plus-boot-starter</artifactId>             <version>3.1.1</version>         </dependency>          <!-- mybatis-plus begin -->         <dependency>             <groupId>mysql</groupId>             <artifactId>mysql-connector-java</artifactId>             <scope>runtime</scope>         </dependency>         <dependency>             <groupId>org.apache.commons</groupId>             <artifactId>commons-lang3</artifactId>             <version>3.8.1</version>         </dependency>         <dependency>             <groupId>com.alibaba</groupId>             <artifactId>fastjson</artifactId>             <version>1.2.54</version>         </dependency>         <dependency>             <groupId>com.alibaba</groupId>             <artifactId>druid</artifactId>             <version>1.1.9</version>         </dependency>         <dependency>             <groupId>org.apache.httpcomponents</groupId>             <artifactId>httpclient</artifactId>         </dependency>         <dependency>             <groupId>org.springframework</groupId>             <artifactId>spring-context-support</artifactId>             <version>5.1.5.RELEASE</version>         </dependency>         <dependency>             <groupId>io.springfox</groupId>             <artifactId>springfox-swagger2</artifactId>             <version>2.2.2</version>         </dependency>         <dependency>             <groupId>io.springfox</groupId>             <artifactId>springfox-swagger-ui</artifactId>             <version>2.2.2</version>         </dependency>         <dependency>             <groupId>io.springfox</groupId>             <artifactId>springfox-swagger2</artifactId>             <version>2.7.0</version>         </dependency>
 package com.example.socket.controller;  import com.example.socket.common.ResultMap; import com.example.socket.common.ValidatorUtils; import com.example.socket.model.PageUtils; import com.example.socket.model.ScheduleJobEntity; import com.example.socket.service.IScheduleJobService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*;  import java.util.Map;  /**  * Created with IDEA  * author:QinWei  * Date:2019/5/20  * Time:14:37  */ @Api(value = "定时任务", tags = {" 定时任务"}) @RestController @RequestMapping("/sys/schedule") public class QuartzApiController {       @Autowired     private IScheduleJobService scheduleJobService;      /**      * 定时任务列表      */     @ApiOperation(value = "定时任务列表", notes = "定时任务列表")     @GetMapping("/list")     public ResultMap list(@RequestParam Map<String, Object> params) {         PageUtils page = scheduleJobService.queryPage(params);          return ResultMap.ok().put("page", page);     }      /**      * 定时任务信息      */     @ApiOperation(value = "定时任务信息", notes = "定时任务信息")     @GetMapping("/info/{jobId}")     public ResultMap info(@PathVariable("jobId") Long jobId) {         ScheduleJobEntity schedule = scheduleJobService.getById(jobId);          return ResultMap.ok().put("schedule", schedule);     }      /**      * 保存定时任务      */     @ApiOperation(value = "保存定时任务", notes = "保存定时任务")     @PostMapping("/save")     public ResultMap save(@RequestBody ScheduleJobEntity scheduleJob) {         ValidatorUtils.validateEntity(scheduleJob);          scheduleJobService.insert(scheduleJob);          return ResultMap.ok();     }      /**      * 修改定时任务      */     @ApiOperation(value = "修改定时任务", notes = "修改定时任务")     @PostMapping("/update")     public ResultMap update(@RequestBody ScheduleJobEntity scheduleJob) {         //ValidatorUtils.validateEntity(scheduleJob);          scheduleJobService.update(scheduleJob);          return ResultMap.ok();     }      /**      * 删除定时任务      */     @ApiOperation(value = "删除定时任务", notes = "删除定时任务")     @PostMapping("/delete")     public ResultMap delete(@RequestBody Long[] jobIds) {         scheduleJobService.deleteBatch(jobIds);          return ResultMap.ok();     }      /**      * 立即执行任务      */     @ApiOperation(value = "立即执行任务", notes = "立即执行任务")     @PostMapping("/run")     public ResultMap run(@RequestBody Long[] jobIds) {         scheduleJobService.run(jobIds);          return ResultMap.ok();     }      /**      * 暂停定时任务      */     @ApiOperation(value = "暂停定时任务", notes = "暂停定时任务")     @PostMapping("/pause")     public ResultMap pause(@RequestBody Long[] jobIds) {         scheduleJobService.pause(jobIds);          return ResultMap.ok();     }      /**      * 恢复定时任务      */     @ApiOperation(value = "恢复定时任务", notes = "恢复定时任务")     @PostMapping("/resume")     public ResultMap resume(@RequestBody Long[] jobIds) {         scheduleJobService.resume(jobIds);          return ResultMap.ok();     } } 

https://gitee.com/qwerdfs/springboot_common

文章来源: https://blog.csdn.net/qq_37996327/article/details/90378137
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!