springboot整合quartz框架的代码示例
时间: 2023-06-18 22:03:14 浏览: 125
好的,下面给出一个简单的 Spring Boot 整合 Quartz 框架的代码示例。
首先,我们需要在 `pom.xml` 文件中添加以下依赖:
```xml
<!-- Quartz -->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.3.2</version>
</dependency>
```
接下来,创建一个 Quartz 的配置类,用于配置 Quartz 相关的参数和调度器。
```java
@Configuration
public class QuartzConfig {
@Autowired
private ApplicationContext applicationContext;
@Bean
public SchedulerFactoryBean schedulerFactoryBean() {
SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
schedulerFactoryBean.setApplicationContext(applicationContext);
schedulerFactoryBean.setTaskExecutor(schedulerThreadPool());
schedulerFactoryBean.setSchedulerName("quartz-scheduler");
schedulerFactoryBean.setStartupDelay(5);
schedulerFactoryBean.setAutoStartup(true);
schedulerFactoryBean.setOverwriteExistingJobs(true);
schedulerFactoryBean.setQuartzProperties(quartzProperties());
return schedulerFactoryBean;
}
@Bean
public ThreadPoolTaskExecutor schedulerThreadPool() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setThreadNamePrefix("quartz-threadpool-");
executor.setCorePoolSize(10);
executor.setMaxPoolSize(20);
executor.setWaitForTasksToCompleteOnShutdown(true);
executor.setAwaitTerminationSeconds(60);
return executor;
}
@Bean
public Properties quartzProperties() {
Properties properties = new Properties();
properties.setProperty("org.quartz.scheduler.instanceName", "quartz-scheduler");
properties.setProperty("org.quartz.scheduler.instanceId", "AUTO");
properties.setProperty("org.quartz.scheduler.skipUpdateCheck", "true");
properties.setProperty("org.quartz.scheduler.jobFactory.class", "org.quartz.simpl.SimpleJobFactory");
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", "quartzDataSource");
properties.setProperty("org.quartz.jobStore.tablePrefix", "QRTZ_");
properties.setProperty("org.quartz.jobStore.isClustered", "false");
properties.setProperty("org.quartz.threadPool.class", "org.quartz.simpl.SimpleThreadPool");
properties.setProperty("org.quartz.threadPool.threadCount", "10");
properties.setProperty("org.quartz.dataSource.quartzDataSource.driver", "${jdbc.driverClassName}");
properties.setProperty("org.quartz.dataSource.quartzDataSource.URL", "${jdbc.url}");
properties.setProperty("org.quartz.dataSource.quartzDataSource.user", "${jdbc.username}");
properties.setProperty("org.quartz.dataSource.quartzDataSource.password", "${jdbc.password}");
return properties;
}
}
```
上述代码中,我们使用了 `SchedulerFactoryBean` 来创建调度器,并且设置了调度器相关的属性,比如调度器名称、线程池、延迟启动时间等等。同时,我们也配置了 Quartz 的属性,比如数据源、表前缀等等。
接下来,我们创建一个定时任务类。
```java
@Component
public class ScheduledTask {
private static final Logger LOGGER = LoggerFactory.getLogger(ScheduledTask.class);
@Scheduled(cron = "0/5 * * * * ?")
public void run() {
LOGGER.info("定时任务执行了!");
}
}
```
上述代码中,我们使用了 `@Scheduled` 注解来指定定时任务的执行时间。在本例中,我们将任务设置为每隔 5 秒执行一次。
最后,我们启动 Spring Boot 应用,就可以看到定时任务开始执行了。
阅读全文