0
# Auto-configuration
1
2
Core auto-configuration classes that automatically set up Quartz Scheduler when the starter is present on the classpath. Handles scheduler factory bean creation, job store configuration, and seamless Spring integration.
3
4
## Capabilities
5
6
### QuartzAutoConfiguration
7
8
Main auto-configuration class that sets up the Quartz Scheduler with Spring Boot's opinionated defaults.
9
10
```java { .api }
11
/**
12
* Auto-configuration for Quartz Scheduler integration with Spring Boot
13
* Activates when Scheduler, SchedulerFactoryBean, and PlatformTransactionManager are on classpath
14
* Runs after DataSource, JPA, and database migration auto-configurations
15
*/
16
@AutoConfiguration(after = {
17
DataSourceAutoConfiguration.class,
18
HibernateJpaAutoConfiguration.class,
19
LiquibaseAutoConfiguration.class,
20
FlywayAutoConfiguration.class
21
})
22
@ConditionalOnClass({ Scheduler.class, SchedulerFactoryBean.class, PlatformTransactionManager.class })
23
@EnableConfigurationProperties(QuartzProperties.class)
24
public class QuartzAutoConfiguration {
25
26
/**
27
* Creates and configures the main Quartz SchedulerFactoryBean
28
* @param properties QuartzProperties configuration
29
* @param customizers Optional customizers for advanced configuration
30
* @param jobDetails JobDetail beans to register with scheduler
31
* @param calendars Calendar beans to register with scheduler
32
* @param triggers Trigger beans to register with scheduler
33
* @param applicationContext Spring application context for job factory
34
* @return Configured SchedulerFactoryBean
35
*/
36
@Bean
37
@ConditionalOnMissingBean
38
public SchedulerFactoryBean quartzScheduler(
39
QuartzProperties properties,
40
ObjectProvider<SchedulerFactoryBeanCustomizer> customizers,
41
ObjectProvider<JobDetail> jobDetails,
42
Map<String, Calendar> calendars,
43
ObjectProvider<Trigger> triggers,
44
ApplicationContext applicationContext
45
);
46
}
47
```
48
49
**Usage Examples:**
50
51
```java
52
// Basic configuration - no additional setup needed
53
@SpringBootApplication
54
public class Application {
55
public static void main(String[] args) {
56
SpringApplication.run(Application.class, args);
57
// Quartz scheduler automatically configured and started
58
}
59
}
60
61
// Custom job and trigger beans are automatically registered
62
@Configuration
63
public class JobConfiguration {
64
65
@Bean
66
public JobDetail myJobDetail() {
67
return JobBuilder.newJob(MyJob.class)
68
.withIdentity("myJob", "myGroup")
69
.storeDurably()
70
.build();
71
}
72
73
@Bean
74
public Trigger myTrigger() {
75
return TriggerBuilder.newTrigger()
76
.forJob(myJobDetail())
77
.withIdentity("myTrigger", "myGroup")
78
.withSchedule(CronScheduleBuilder.cronSchedule("0 0 12 * * ?"))
79
.build();
80
}
81
}
82
```
83
84
### JDBC Store Configuration
85
86
Nested configuration class that handles JDBC job store setup when database persistence is required.
87
88
```java { .api }
89
/**
90
* Configuration for JDBC-based job store
91
* Activates when DataSource is available and spring.quartz.job-store-type=jdbc
92
*/
93
@Configuration(proxyBeanMethods = false)
94
@ConditionalOnSingleCandidate(DataSource.class)
95
@ConditionalOnProperty(name = "spring.quartz.job-store-type", havingValue = "jdbc")
96
@Import(DatabaseInitializationDependencyConfigurer.class)
97
protected static class JdbcStoreTypeConfiguration {
98
99
/**
100
* Customizer that configures DataSource and TransactionManager for Quartz
101
* @param properties Quartz configuration properties
102
* @param dataSource Primary DataSource (or @QuartzDataSource if available)
103
* @param quartzDataSource Optional dedicated DataSource for Quartz
104
* @param transactionManager Primary transaction manager
105
* @param quartzTransactionManager Optional dedicated transaction manager
106
* @return SchedulerFactoryBeanCustomizer for JDBC configuration
107
*/
108
@Bean
109
@Order(0)
110
public SchedulerFactoryBeanCustomizer dataSourceCustomizer(
111
QuartzProperties properties,
112
DataSource dataSource,
113
@QuartzDataSource ObjectProvider<DataSource> quartzDataSource,
114
ObjectProvider<PlatformTransactionManager> transactionManager,
115
@QuartzTransactionManager ObjectProvider<PlatformTransactionManager> quartzTransactionManager
116
);
117
118
/**
119
* Database initializer for Quartz schema setup
120
* @param dataSource DataSource for schema initialization
121
* @param quartzDataSource Optional dedicated Quartz DataSource
122
* @param properties Quartz configuration properties
123
* @return QuartzDataSourceScriptDatabaseInitializer
124
*/
125
@Bean
126
@ConditionalOnMissingBean
127
@Conditional(OnQuartzDatasourceInitializationCondition.class)
128
public QuartzDataSourceScriptDatabaseInitializer quartzDataSourceScriptDatabaseInitializer(
129
DataSource dataSource,
130
@QuartzDataSource ObjectProvider<DataSource> quartzDataSource,
131
QuartzProperties properties
132
);
133
}
134
```
135
136
**Usage Examples:**
137
138
```java
139
// JDBC job store with dedicated DataSource
140
@Configuration
141
public class QuartzDataSourceConfiguration {
142
143
@Bean
144
@QuartzDataSource
145
@ConfigurationProperties("spring.datasource.quartz")
146
public DataSourceProperties quartzDataSourceProperties() {
147
return new DataSourceProperties();
148
}
149
150
@Bean
151
@QuartzDataSource
152
public DataSource quartzDataSource() {
153
return quartzDataSourceProperties()
154
.initializeDataSourceBuilder()
155
.build();
156
}
157
158
@Bean
159
@QuartzTransactionManager
160
public PlatformTransactionManager quartzTransactionManager(
161
@QuartzDataSource DataSource dataSource) {
162
return new DataSourceTransactionManager(dataSource);
163
}
164
}
165
```
166
167
### Database Initialization
168
169
Classes responsible for Quartz database schema initialization and dependency management.
170
171
```java { .api }
172
/**
173
* Handles database schema initialization for Quartz JDBC job store
174
* Extends DataSourceScriptDatabaseInitializer for Quartz-specific schema setup
175
*/
176
public class QuartzDataSourceScriptDatabaseInitializer
177
extends DataSourceScriptDatabaseInitializer {
178
179
/**
180
* Creates database initializer with DataSource and configuration
181
* @param dataSource DataSource for initialization
182
* @param properties Quartz properties containing schema settings
183
*/
184
public QuartzDataSourceScriptDatabaseInitializer(
185
DataSource dataSource,
186
QuartzProperties properties
187
);
188
189
/**
190
* Gets database initialization settings from Quartz properties
191
* @return DatabaseInitializationSettings configured for Quartz
192
*/
193
protected DatabaseInitializationSettings getDatabaseInitializationSettings();
194
}
195
196
/**
197
* Detects if Scheduler depends on database initialization for proper ordering
198
* Implements DependsOnDatabaseInitializationDetector interface
199
*/
200
public class SchedulerDependsOnDatabaseInitializationDetector
201
implements DependsOnDatabaseInitializationDetector {
202
203
/**
204
* Detects scheduler factory bean dependency on database initialization
205
* @param beanFactory Bean factory to check for dependencies
206
* @param configurer Database initialization dependency configurer
207
*/
208
void detect(ConfigurableListableBeanFactory beanFactory,
209
DatabaseInitializationDependencyConfigurer configurer);
210
}
211
212
/**
213
* Conditional class for checking Quartz DataSource initialization requirements
214
*/
215
public class OnQuartzDatasourceInitializationCondition implements Condition {
216
/**
217
* Evaluates if Quartz database initialization should occur
218
* @param context Condition context with environment and bean factory
219
* @param metadata Annotation metadata from conditional annotation
220
* @return true if initialization should occur
221
*/
222
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata);
223
}
224
```
225
226
## Configuration Integration
227
228
The auto-configuration integrates with Spring Boot's configuration system:
229
230
```properties
231
# Enable auto-configuration (default when starter is present)
232
spring.quartz.auto-startup=true
233
234
# Configure job store type to activate JDBC configuration
235
spring.quartz.job-store-type=jdbc
236
237
# Database initialization
238
spring.quartz.jdbc.initialize-schema=embedded
239
spring.quartz.jdbc.schema=classpath:org/quartz/impl/jdbcjobstore/tables_@@platform@@.sql
240
241
# Advanced Quartz properties
242
spring.quartz.properties.org.quartz.scheduler.instanceName=MyScheduler
243
spring.quartz.properties.org.quartz.scheduler.instanceId=AUTO
244
spring.quartz.properties.org.quartz.jobStore.class=org.springframework.scheduling.quartz.LocalDataSourceJobStore
245
```
246
247
## Error Handling
248
249
The auto-configuration handles common error scenarios:
250
251
- **Missing DataSource**: Falls back to in-memory job store
252
- **Database initialization failures**: Provides clear error messages about schema setup
253
- **Conflicting configurations**: Uses Spring Boot's conditional annotations to prevent conflicts
254
- **Startup timing**: Ensures proper ordering with database migrations and JPA initialization