springboot整合quartz的代码展示
时间: 2023-06-04 18:05:36 浏览: 181
spring整合quartz的代码
好的,下面是 Spring Boot 整合 Quartz 的代码示例:
首先在 pom.xml 中引入 Quartz 和相关依赖:
```xml
<dependencies>
<!-- Quartz -->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.3.2</version>
</dependency>
<!-- Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.5.5</version>
</dependency>
<!-- JDBC -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.200</version>
</dependency>
</dependencies>
```
接下来配置 Quartz 的 JobStore,这里使用的是 JDBCJobStore,将任务信息保存到数据库中:
```java
@Configuration
public class QuartzConfig {
@Bean
public SchedulerFactoryBean schedulerFactoryBean() {
SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
schedulerFactoryBean.setDataSource(dataSource());
schedulerFactoryBean.setJobFactory(jobFactory());
schedulerFactoryBean.setAutoStartup(true);
schedulerFactoryBean.setSchedulerName("MyScheduler");
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", "false");
properties.setProperty("org.quartz.jobStore.acquireTriggersWithinLock", "true");
schedulerFactoryBean.setQuartzProperties(properties);
schedulerFactoryBean.setApplicationContextSchedulerContextKey("applicationContext");
schedulerFactoryBean.setWaitForJobsToCompleteOnShutdown(true);
return schedulerFactoryBean;
}
@Bean
public JobFactory jobFactory() {
return new AutowiringSpringBeanJobFactory();
}
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("org.h2.Driver");
dataSource.setUrl("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1");
dataSource.setUsername("sa");
dataSource.setPassword("");
return dataSource;
}
@Bean
public JobDetail jobDetail() {
return JobBuilder.newJob(MyJob.class)
.withIdentity("myJob")
.storeDurably(true)
.build();
}
@Bean
public Trigger trigger() {
return TriggerBuilder.newTrigger()
.forJob(jobDetail())
.withIdentity("myTrigger")
.withSchedule(CronScheduleBuilder.cronSchedule("0 0/1 * * * ?"))
.build();
}
}
```
注意到这里使用了 AutowiringSpringBeanJobFactory,启用了自动注入功能,这样我们就可以在 Job 中注入 Spring Bean。
接下来定义一个 Job 类,可以注入 Spring Bean 并执行一些任务逻辑:
```java
@Component
public class MyJob implements Job {
@Autowired
private MyService myService;
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
myService.doSomething();
}
}
```
最后,在 Spring Boot 应用中启动 Quartz:
```java
@SpringBootApplication
public class MyApp {
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
}
```
这样就完成了 Spring Boot 整合 Quartz 的基础配置。
阅读全文