SpringBoot整合Quartz和Druid
时间: 2023-11-20 18:03:26 浏览: 105
好的,下面是整合步骤:
1. 在 pom.xml 中添加依赖:
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.21</version>
</dependency>
```
2. 在 application.yml 或 application.properties 文件中配置数据源和 Quartz:
```yaml
spring:
datasource:
url: jdbc:mysql://localhost:3306/db_name?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
quartz:
job-store-type: jdbc
jdbc:
initialize-schema: never # 默认值为 always,表示每次启动都会重新初始化表结构,这里设为 never,只在第一次启动时初始化
```
3. 创建一个 Quartz 配置类,用于配置 Quartz 的属性:
```java
@Configuration
public class QuartzConfig {
@Autowired
private DataSource dataSource;
@Bean
public SchedulerFactoryBean schedulerFactoryBean() {
SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
schedulerFactoryBean.setDataSource(dataSource);
schedulerFactoryBean.setQuartzProperties(quartzProperties());
schedulerFactoryBean.setSchedulerName("MyScheduler");
schedulerFactoryBean.setApplicationContextSchedulerContextKey("applicationContext");
schedulerFactoryBean.setAutoStartup(true);
schedulerFactoryBean.setStartupDelay(5);
return schedulerFactoryBean;
}
@Bean
public Properties quartzProperties() {
Properties properties = new Properties();
properties.setProperty("org.quartz.scheduler.instanceName", "MyScheduler");
properties.setProperty("org.quartz.scheduler.instanceId", "AUTO");
properties.setProperty("org.quartz.jobStore.class", "org.quartz.impl.jdbcjobstore.JobStoreTX");
properties.setProperty("org.quartz.jobStore.driverDelegateClass", "org.quartz.impl.jdbcjobstore.StdJDBCDelegate");
properties.setProperty("org.quartz.jobStore.useProperties", "false");
properties.setProperty("org.quartz.jobStore.dataSource", "myDS");
properties.setProperty("org.quartz.jobStore.tablePrefix", "QRTZ_");
properties.setProperty("org.quartz.jobStore.isClustered", "true");
properties.setProperty("org.quartz.threadPool.class", "org.quartz.simpl.SimpleThreadPool");
properties.setProperty("org.quartz.threadPool.threadCount", "10");
properties.setProperty("org.quartz.dataSource.myDS.driver", "com.mysql.jdbc.Driver");
properties.setProperty("org.quartz.dataSource.myDS.URL", "jdbc:mysql://localhost:3306/db_name?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai");
properties.setProperty("org.quartz.dataSource.myDS.user", "root");
properties.setProperty("org.quartz.dataSource.myDS.password", "123456");
properties.setProperty("org.quartz.dataSource.myDS.maxConnections", "10");
return properties;
}
}
```
4. 创建一个 Job 类,用于定义具体的任务:
```java
public class MyJob implements Job {
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
// 任务执行逻辑
}
}
```
5. 创建一个 Trigger 类,用于定义任务执行的时间规则:
```java
public class MyTrigger {
public static Trigger getTrigger() {
SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
.withIntervalInSeconds(10) // 每 10 秒执行一次
.repeatForever();
return TriggerBuilder.newTrigger()
.withIdentity("MyTrigger", "MyTriggerGroup")
.withSchedule(scheduleBuilder)
.startAt(new Date()) // 从当前时间开始执行
.build();
}
}
```
6. 在启动类中添加注解 @EnableScheduling,用于开启定时任务:
```java
@SpringBootApplication
@EnableScheduling
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
```
7. 在需要调度任务的地方,注入 Scheduler 对象,然后调用其 scheduleJob 方法即可:
```java
@Autowired
private Scheduler scheduler;
public void scheduleJob() throws SchedulerException {
JobDetail jobDetail = JobBuilder.newJob(MyJob.class)
.withIdentity("MyJob", "MyJobGroup")
.build();
Trigger trigger = MyTrigger.getTrigger();
scheduler.scheduleJob(jobDetail, trigger);
}
```
以上就是 Spring Boot 整合 Quartz 和 Druid 的步骤。
阅读全文