1. pom文件添加依赖
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-quartz</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency> <!--<!– druid数据库连接池 –>--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> </dependency> </dependencies>
2. yml配置
spring: datasource: url: jdbc:mysql://localhost:3306/aipyun?serverTimezone=GMT&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true username: root password: root123 driver-class-name: com.mysql.jdbc.Driver type: com.alibaba.druid.pool.DruidDataSource druid: initialSize: 2 minIdle: 2 maxActive: 30 #StatViewServlet: #loginUsername: admin #loginPassword: admin quartz: #相关属性配置 properties: org: quartz: scheduler: instanceName: DefaultQuartzScheduler instanceId: AUTO jobStore: class: org.quartz.impl.jdbcjobstore.JobStoreTX driverDelegateClass: org.quartz.impl.jdbcjobstore.StdJDBCDelegate tablePrefix: QRTZ_ isClustered: false clusterCheckinInterval: 10000 useProperties: true threadPool: class: org.quartz.simpl.SimpleThreadPool threadCount: 10 threadPriority: 5 threadsInheritContextClassLoaderOfInitializingThread: true dataSource: default: URL: jdbc:mysql://localhost:3306/aipyun?characterEncoding=utf-8 user: root password: root123 driver: com.mysql.jdbc.Driver #数据库方式 job-store-type: jdbc #初始化表结构 #jdbc: #initialize-schema: never
我们可以对比下这个配置文件和之前的quartz.properties的区别,基本一模一样,只不过写的格式不一样而已。
3. 业务逻辑代码
/** * 删除job * * @param triggerName 触发器名称 * @param triggerGroup 触发器分组 * @param jobName 任务名称 * @param jobGroup 任务分组 * @throws SchedulerException */ public void deleteJob(String triggerName, String triggerGroup, String jobName, String jobGroup) throws SchedulerException { TriggerKey triggerKey = TriggerKey.triggerKey(triggerName, triggerGroup); scheduler.pauseTrigger(triggerKey); scheduler.unscheduleJob(triggerKey); JobKey jobKey = JobKey.jobKey(jobName, jobGroup); scheduler.deleteJob(jobKey); } /** * 修改定时任务 * * @param oldTriggerKey 需要修改的TriggerKey 也就是唯一标识 * @param cron 新的cron表达式 */ public void updateJob(TriggerKey oldTriggerKey, String cron) { CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cron); CronTrigger cronTrigger = TriggerBuilder.newTrigger() .withIdentity(oldTriggerKey).withSchedule(scheduleBuilder).build(); try { scheduler.rescheduleJob(oldTriggerKey, cronTrigger); } catch (SchedulerException e) { e.printStackTrace(); } } /** * 新增job任务 * * @param jobName job名称 * @param jobGroupName job分组名称 * @param triggerName 触发器名称 * @param triggerGroupName 触发器分组名称 * @param jobClass 需要执行的job.class * @param cron cron 表达式 * @throws SchedulerException */ public void addJob(String jobName, String jobGroupName, String triggerName, String triggerGroupName, Class jobClass, String cron) throws SchedulerException { CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(cron); JobDetail jobDetail = JobBuilder.newJob(jobClass).withIdentity(jobName, jobGroupName).build(); Trigger trigger = TriggerBuilder.newTrigger().withIdentity(triggerName, triggerGroupName) .withSchedule(cronScheduleBuilder).build(); scheduler.scheduleJob(jobDetail, trigger); }
关于spring-boot-starter-quartz
鄙人用的开发工具是idea,可以直接查看源码,至于用eclispe的怎么查看源码我就不知道了。
我们找到Idea的External Libraries并且展开spring-boot-autoconfigure-2.0.0.RELEASE.jar,找到org.springframework.boot.autoconfigure.quartz,该目录就是SpringBoot为我们提供的Quartz自动化配置源码实现,在该目录下有如下所示几个类:
QuartzAutoConfiguration该类是自动配置的主类,内部配置了SchedulerFactoryBean,相当于我们之前自己配的SchedulerFactoryBean类。
JobStoreType是一个枚举:表示quartz的存储方式:RAM 或者JDBC。
QuartzProperties配置类:从yml或者properties中读取配置信息。