# 功能描述

实现定时任务功能。
配置文件为application-job.yml

提示

(1) 需在application.yml中增加spring.profiles.active中增加job才能激活配置文件
(2) 需设置application-job.yml中的spring.quartz.enabled为true才能开启job服务

# 配置一览表

spring:
  quartz:
    enabled: true #设置为true才开启定时任务服务
    #相关属性配置
    properties:
      org:
        quartz:
          scheduler:
            instanceName: clusteredScheduler
            instanceId: AUTO
          jobStore:
            class: org.quartz.impl.jdbcjobstore.JobStoreTX
            driverDelegateClass: org.quartz.impl.jdbcjobstore.StdJDBCDelegate
            tablePrefix: QRTZ_
            isClustered: false
            clusterCheckinInterval: 10000
            useProperties: false
          threadPool:
            class: org.quartz.simpl.SimpleThreadPool
            threadCount: 10
            threadPriority: 5
            threadsInheritContextClassLoaderOfInitializingThread: true
    job-store-type: jdbc  #数据库方式
    jdbc:
      initialize-schema: always  #初始化表结构: NEVER,ALWAYS,EMBEDDED
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

# Service接口

# 第一步 引入包

<dependency>
    <groupId>sei-cloud</groupId>
    <artifactId>job</artifactId>
</dependency>
1
2
3
4

# 第二步 引入接口

@Resource
    JobService jobService;
1
2

# 第三步 使用接口

/**
 *
 * @author xiong
 */
public interface JobService {
    /**
     * 添加任务
     * @param data: 内容
     * @throws SchedulerException 异常
     * @throws ClassNotFoundException 异常
     * @throws InstantiationException 异常
     * @throws IllegalAccessException 异常
     */
    void add(@NonNull JSONObject data) throws SchedulerException, ClassNotFoundException, InstantiationException, IllegalAccessException;

    /**
     * 修改任务
     * @param data: 内容
     * @throws SchedulerException 异常
     */
    void edit(@NonNull JSONObject data) throws  SchedulerException;

    /**
     * 停止任务
     * @param data: 内容
     * @throws SchedulerException 异常
     */
    void stop(@NonNull JSONObject data) throws SchedulerException;

    /**
     * 重启任务
     * @param data: 内容
     * @throws SchedulerException 异常
     */
    void resume(@NonNull JSONObject data) throws SchedulerException;

    /**
     * 移除任务
     * @param data: 内容
     * @throws SchedulerException 异常
     */
    void remove(@NonNull JSONObject data) throws SchedulerException;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

# Controller接口

@ConditionalOnProperty(name = "spring.quartz.enabled", havingValue = "true")
@Api(description = "定时任务服务")
@RequestMapping(value = "/api/job/")
public class JobController {

    @ApiOperation(value = "添加任务",response = ResMsg.class)
    @RequestMapping(value = "/add",method = {RequestMethod.POST}, produces = { MediaType.APPLICATION_JSON_VALUE })
    public @ResponseBody ResMsg add(@RequestBody JSONArray data) throws Exception;

    @ApiOperation(value = "修改任务",response = ResMsg.class)
    @RequestMapping(value = "/edit",method = {RequestMethod.POST}, produces = { MediaType.APPLICATION_JSON_VALUE })
    public @ResponseBody ResMsg edit(@RequestBody JSONArray data) throws Exception;

    @ApiOperation(value = "暂停任务",response = ResMsg.class)
    @RequestMapping(value = "/pause",method = {RequestMethod.POST}, produces = { MediaType.APPLICATION_JSON_VALUE })
    public @ResponseBody ResMsg pause(@RequestBody JSONObject data) throws Exception;

    @ApiOperation(value = "恢复任务",response = ResMsg.class)
    @RequestMapping(value = "/resume",method = {RequestMethod.POST}, produces = { MediaType.APPLICATION_JSON_VALUE })
    public @ResponseBody ResMsg resume(@RequestBody JSONObject data) throws Exception;

    @ApiOperation(value = "删除任务",response = ResMsg.class)
    @RequestMapping(value = "/remove",method = {RequestMethod.POST}, produces = { MediaType.APPLICATION_JSON_VALUE })
    public @ResponseBody ResMsg remove(@RequestBody JSONArray data) throws Exception;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25