java 定时器 Quartz Springboot 自启动
时间: 2024-04-04 19:10:39 浏览: 101
spring-boot-starter-quartz:Quartz-Scheduler的Spring-Boot自动配置
在 Spring Boot 中使用 Quartz 定时器并自启动的步骤如下:
1. 添加依赖:在 pom.xml 文件中添加 Quartz 和 Spring Boot Starter 的依赖。
```xml
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.3.2.RELEASE</version>
</dependency>
```
2. 配置定时器:在配置文件中添加 Quartz 定时器的配置。
```properties
# 配置 Quartz SchedulerFactory
org.quartz.scheduler.instanceName=MyQuartzScheduler
org.quartz.scheduler.instanceId=AUTO
# 配置线程池
org.quartz.threadPool.class=org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount=5
org.quartz.threadPool.threadPriority=5
# 配置JobStore
org.quartz.jobStore.class=org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.StdJDBCDelegate
org.quartz.jobStore.tablePrefix=QRTZ_
org.quartz.jobStore.dataSource=myDS
org.quartz.jobStore.isClustered=true
# 配置数据源
org.quartz.dataSource.myDS.driver=com.mysql.jdbc.Driver
org.quartz.dataSource.myDS.URL=jdbc:mysql://localhost:3306/quartz?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
org.quartz.dataSource.myDS.user=root
org.quartz.dataSource.myDS.password=123456
org.quartz.dataSource.myDS.maxConnections=10
# 配置自启动
org.quartz.startupHook.class=org.quartz.plugins.management.ShutdownHookPlugin
org.quartz.plugin.shutdownhook.cleanShutdown=true
```
3. 编写定时器:创建一个 Quartz 定时器 Job 类,实现 execute 方法。
```java
public class MyJob implements Job {
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
// 执行任务的逻辑代码
}
}
```
4. 创建定时器:在 Spring Boot 应用的启动类中创建定时器。
```java
@Configuration
public class QuartzConfig {
@Autowired
private ApplicationContext applicationContext;
@Bean
public JobDetail myJobDetail() {
return JobBuilder.newJob(MyJob.class)
.withIdentity("myJob")
.storeDurably()
.build();
}
@Bean
public Trigger myTrigger() {
SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
.withIntervalInSeconds(10)
.repeatForever();
return TriggerBuilder.newTrigger()
.forJob(myJobDetail())
.withIdentity("myTrigger")
.withSchedule(scheduleBuilder)
.build();
}
@Bean
public SchedulerFactoryBean schedulerFactory() {
AutowiringSpringBeanJobFactory jobFactory = new AutowiringSpringBeanJobFactory();
jobFactory.setApplicationContext(applicationContext);
Properties properties = new Properties();
properties.setProperty("org.quartz.scheduler.instanceName", "MyQuartzScheduler");
properties.setProperty("org.quartz.scheduler.instanceId", "AUTO");
properties.setProperty("org.quartz.threadPool.class", "org.quartz.simpl.SimpleThreadPool");
properties.setProperty("org.quartz.threadPool.threadCount", "5");
properties.setProperty("org.quartz.threadPool.threadPriority", "5");
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.tablePrefix", "QRTZ_");
properties.setProperty("org.quartz.jobStore.dataSource", "myDS");
properties.setProperty("org.quartz.jobStore.isClustered", "true");
properties.setProperty("org.quartz.dataSource.myDS.driver", "com.mysql.jdbc.Driver");
properties.setProperty("org.quartz.dataSource.myDS.URL", "jdbc:mysql://localhost:3306/quartz?useUnicode=true&characterEncoding=UTF-8&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");
properties.setProperty("org.quartz.startupHook.class", "org.quartz.plugins.management.ShutdownHookPlugin");
properties.setProperty("org.quartz.plugin.shutdownhook.cleanShutdown", "true");
SchedulerFactoryBean schedulerFactory = new SchedulerFactoryBean();
schedulerFactory.setQuartzProperties(properties);
schedulerFactory.setAutoStartup(true);
schedulerFactory.setJobFactory(jobFactory);
schedulerFactory.setApplicationContextSchedulerContextKey("applicationContext");
schedulerFactory.setTriggers(myTrigger());
return schedulerFactory;
}
}
```
在上述代码中,创建了一个 JobDetail 对象和一个 Trigger 对象,并将它们绑定在 SchedulerFactoryBean 中。SchedulerFactoryBean 会自动启动 Quartz 定时器,并执行定时任务。
阅读全文