SpringBoot Quartz查询数据库动态创建定时任务
时间: 2023-11-02 10:17:02 浏览: 224
Spring Boot 创建定时任务(配合数据库动态执行).docx
在Spring Boot中,可以通过集成Quartz框架来实现动态创建定时任务。下面是一个简单的示例,演示如何查询数据库来创建定时任务。
首先,需要在pom.xml文件中添加Quartz和MySQL的依赖:
```xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
```
然后,需要在application.properties文件中配置数据库连接信息:
```properties
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
```
接下来,可以定义一个ScheduledJob实体类,用于映射数据库中的定时任务数据:
```java
@Entity
@Table(name = "scheduled_job")
public class ScheduledJob {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String group;
private String cronExpression;
private String description;
// getters and setters
}
```
然后,创建一个JobFactory类,继承SpringBeanJobFactory,并重写createJobInstance方法,用于将Job实例交给Spring容器进行管理:
```java
public class JobFactory extends SpringBeanJobFactory implements ApplicationContextAware {
private transient AutowireCapableBeanFactory beanFactory;
@Override
public void setApplicationContext(final ApplicationContext context) {
beanFactory = context.getAutowireCapableBeanFactory();
}
@Override
protected Object createJobInstance(final TriggerFiredBundle bundle) throws Exception {
final Object job = super.createJobInstance(bundle);
beanFactory.autowireBean(job);
return job;
}
}
```
最后,创建一个JobScheduler类,用于从数据库中读取定时任务数据,并创建对应的定时任务:
```java
@Component
public class JobScheduler {
@Autowired
private SchedulerFactory schedulerFactory;
@Autowired
private JobFactory jobFactory;
@Autowired
private DataSource dataSource;
@PostConstruct
public void init() throws Exception {
final Scheduler scheduler = schedulerFactory.getScheduler();
scheduler.setJobFactory(jobFactory);
final String sql = "select * from scheduled_job";
try (final Connection connection = dataSource.getConnection();
final PreparedStatement statement = connection.prepareStatement(sql);
final ResultSet resultSet = statement.executeQuery()) {
while (resultSet.next()) {
final JobDetail jobDetail = JobBuilder.newJob(QuartzJob.class)
.withIdentity(resultSet.getString("name"), resultSet.getString("group"))
.withDescription(resultSet.getString("description"))
.build();
final Trigger trigger = TriggerBuilder.newTrigger()
.withIdentity(resultSet.getString("name"), resultSet.getString("group"))
.withSchedule(CronScheduleBuilder.cronSchedule(resultSet.getString("cron_expression")))
.build();
scheduler.scheduleJob(jobDetail, trigger);
}
scheduler.start();
}
}
}
```
在上面的代码中,首先通过SchedulerFactory获取Scheduler实例,并设置JobFactory。然后,通过数据库查询语句从数据库中获取定时任务数据,并创建对应的JobDetail和Trigger实例。最后,将JobDetail和Trigger实例添加到Scheduler中,并启动Scheduler。
需要注意的是,QuartzJob类需要继承QuartzJobBean,并实现executeInternal方法,用于执行定时任务的具体逻辑:
```java
public class QuartzJob extends QuartzJobBean {
@Override
protected void executeInternal(final JobExecutionContext context) throws JobExecutionException {
// 定时任务逻辑
}
}
```
至此,一个动态创建定时任务的示例就完成了。需要注意的是,该示例仅供参考,实际应用中还需要考虑很多细节问题,如异常处理、任务状态管理等。
阅读全文