# springboot-demo-quartz **Repository Path**: ithua-springboot/springboot-demo-quartz ## Basic Information - **Project Name**: springboot-demo-quartz - **Description**: - SpringBoot2.x - Quartz任务调度 - 自定义注解添加及触发任务 - SpringBoot启动自动注册JobDetail/CronTrigger Bean - Mybatis + Mybatis-plus; - 全局异常处理; - 通用服务响应; - **Primary Language**: Java - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 3 - **Forks**: 1 - **Created**: 2019-11-23 - **Last Updated**: 2022-11-30 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # springboot-demo-quartz #### 技术栈 - `springboot:2.1.7` - `quartz:2.3.1` - `mybatis:2.1.1` - `mybatis-plus:3.2.0` - `mysql-connector-java:8.0.17` - `swagger2:2.9.2` #### 整合Quartz流程 1. build.gradle ```groovy implementation 'org.springframework.boot:spring-boot-starter-quartz' implementation 'mysql:mysql-connector-java' // 可选 -- 如果要自定义JobDetailAndTrigger 存到数据库的话 implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.1.1' ``` 2. application.yml ```yaml spring: # 添加数据源配置 datasource: url: jdbc:mysql://localhost:3306/java_test username: root password: 999999999 # 将 Quartz 的配置 application-quartz.yml 包含进来 profiles: include: quartz ``` ```yaml spring: # quartz 配置 quartz: job-store-type: jdbc # 数据库方式 jdbc: initialize-schema: always schema: classpath:sql/quartz.sql # 每次启动应用都执行该文件的SQL语句 properties: org: quartz: scheduler: instanceId: AUTO instanceName: clusteredScheduler jobStore: class: org.quartz.impl.jdbcjobstore.JobStoreTX driverDelegateClass: org.quartz.impl.jdbcjobstore.StdJDBCDelegate tablePrefix: qrtz_ isClustered: true clusterCheckinInterval: 10000 useProperties: true threadPool: class: org.quartz.simpl.SimpleThreadPool threadCount: 5 threadPriority: 5 threadsInheritContextClassLoaderOfInitializingThread: true ``` 3. classpath:sql/quartz.sql:应用启动从该文件中创建数据表 ```sql -- qrtz_fired_triggers; -- qrtz_paused_trigger_grp -- qrtz_scheduler_state; -- qrtz_locks; -- qrtz_simple_triggers; -- qrtz_simprop_triggers; -- qrtz_cron_triggers; -- qrtz_blob_triggers; -- qrtz_triggers; -- qrtz_job_details; -- qrtz_calendars; ``` 4. 创建自定义注解:`@QuartzCronScheduled` 5. 将`@QuartzCronScheduled`注解作用的`Job`对应到`JobDetail`和`CronTrigger`注册到成`Spring Bean`: ```java @EnableQuartzCronScheduled @QuartzCronScheduledRegistrar ``` 6. (可选)使用`Scheduler` `Bean` 来动态操作调度任务。 #### 使用 ##### 说明 1. 继承 `QuartzJobBean`类,并实现 `executeInternal()` 方法来执行定时任务的内容; 2. 使用 `@QuartzCronScheduled`注解作用于该任务类,使其成为 `Springboot` 的 `Bean`, 使该任务在 `Springboot` 启动时能自动添加成任务 ##### 示例 ```java @Slf4j @PersistJobDataAfterExecution @DisallowConcurrentExecution @QuartzCronScheduled( jobName = "MyJob", jobGroup = "com.ih=thua.quartz", cronExpression = "0/3 * * * * ?") public class MyJob extends QuartzJobBean { @Override protected void executeInternal(JobExecutionContext context) throws JobExecutionException { log.info("MyJob ----> {}", LocalTime.now()); } } ``` #### 其它项目参考说明 - 搜索项目的`com.ithua.quartz` 替换成 自己项目的 `组织` - 数据库相关:替换成自己项目的 #### 参与贡献