64
64
import io .quarkus .quartz .runtime .QuartzRecorder ;
65
65
import io .quarkus .quartz .runtime .QuartzSchedulerImpl ;
66
66
import io .quarkus .quartz .runtime .QuartzSupport ;
67
+ import io .quarkus .quartz .runtime .jdbc .JDBCDataSource ;
67
68
import io .quarkus .quartz .runtime .jdbc .QuarkusDBv8Delegate ;
68
69
import io .quarkus .quartz .runtime .jdbc .QuarkusHSQLDBDelegate ;
69
70
import io .quarkus .quartz .runtime .jdbc .QuarkusMSSQLDelegate ;
@@ -121,8 +122,8 @@ QuartzJDBCDriverDialectBuildItem driver(List<JdbcDataSourceBuildItem> jdbcDataSo
121
122
if (config .clustered ()) {
122
123
throw new ConfigurationException ("Clustered jobs configured with unsupported job store option" );
123
124
}
124
-
125
- return new QuartzJDBCDriverDialectBuildItem (Optional .empty ());
125
+ // No DB storage, the driver can stay empty, and we don't need data sources either
126
+ return new QuartzJDBCDriverDialectBuildItem (Optional .empty (), null );
126
127
}
127
128
128
129
if (capabilities .isMissing (Capability .AGROAL )) {
@@ -161,29 +162,45 @@ QuartzJDBCDriverDialectBuildItem driver(List<JdbcDataSourceBuildItem> jdbcDataSo
161
162
throw new ConfigurationException (message );
162
163
}
163
164
}
165
+ // A custom delegate implementation, we don't need to check datasources
166
+ return new QuartzJDBCDriverDialectBuildItem (driverDelegate , null );
164
167
} else {
165
- Optional <JdbcDataSourceBuildItem > selectedJdbcDataSourceBuildItem = jdbcDataSourceBuildItems .stream ()
166
- .filter (i -> config .dataSourceName ().isPresent () ? config .dataSourceName ().get ().equals (i .getName ())
167
- : i .isDefault ())
168
- .findFirst ();
168
+ if (config .deferDatasourceCheck ()) {
169
+ // if defer is set to true and there is a DS name, throw an exception
170
+ if (config .dataSourceName ().isPresent ()) {
171
+ String message = String .format (
172
+ "Quartz datasource resolution can be either deferred to runtime or specified at build time but not both. Related properties are quarkus.quartz.defer-datasource-check=%s and quarkus.quartz.datasource=%s" ,
173
+ config .deferDatasourceCheck (), config .dataSourceName ());
174
+ throw new ConfigurationException (message );
175
+ }
176
+ // Defer driver resolution to runtime
177
+ List <JDBCDataSource > dataSources = new ArrayList <>();
178
+ for (JdbcDataSourceBuildItem jdbcDataSourceBuildItem : jdbcDataSourceBuildItems ) {
179
+ dataSources .add (new JDBCDataSource (jdbcDataSourceBuildItem .getName (), jdbcDataSourceBuildItem .isDefault (),
180
+ jdbcDataSourceBuildItem .getDbKind ()));
181
+ }
182
+ return new QuartzJDBCDriverDialectBuildItem (Optional .empty (), dataSources );
183
+ } else {
184
+ // Perform driver resolution at build time
185
+ Optional <JdbcDataSourceBuildItem > selectedJdbcDataSourceBuildItem = jdbcDataSourceBuildItems .stream ()
186
+ .filter (i -> config .dataSourceName ().isPresent () ? config .dataSourceName ().get ().equals (i .getName ())
187
+ : i .isDefault ())
188
+ .findFirst ();
169
189
170
- if (!selectedJdbcDataSourceBuildItem .isPresent ()) {
171
- String message = String .format (
172
- "JDBC Store configured but the '%s' datasource is not configured properly. You can configure your datasource by following the guide available at: https://quarkus.io/guides/datasource" ,
173
- config .dataSourceName ().isPresent () ? config .dataSourceName ().get () : "default" );
174
- throw new ConfigurationException (message );
190
+ if (!selectedJdbcDataSourceBuildItem .isPresent ()) {
191
+ String message = String .format (
192
+ "JDBC Store configured but the '%s' datasource is not configured properly. You can configure your datasource by following the guide available at: https://quarkus.io/guides/datasource" ,
193
+ config .dataSourceName ().isPresent () ? config .dataSourceName ().get () : "default" );
194
+ throw new ConfigurationException (message );
195
+ }
196
+ return new QuartzJDBCDriverDialectBuildItem (Optional .of (guessDriver (selectedJdbcDataSourceBuildItem .get ())),
197
+ null );
175
198
}
176
- driverDelegate = Optional .of (guessDriver (selectedJdbcDataSourceBuildItem ));
177
199
}
178
- return new QuartzJDBCDriverDialectBuildItem (driverDelegate );
179
200
}
180
201
181
- private String guessDriver (Optional <JdbcDataSourceBuildItem > jdbcDataSource ) {
182
- if (!jdbcDataSource .isPresent ()) {
183
- return QuarkusStdJDBCDelegate .class .getName ();
184
- }
185
-
186
- String dataSourceKind = jdbcDataSource .get ().getDbKind ();
202
+ private String guessDriver (JdbcDataSourceBuildItem jdbcDataSource ) {
203
+ String dataSourceKind = jdbcDataSource .getDbKind ();
187
204
if (DatabaseKind .isPostgreSQL (dataSourceKind )) {
188
205
return QuarkusPostgreSQLDelegate .class .getName ();
189
206
}
@@ -202,7 +219,7 @@ private String guessDriver(Optional<JdbcDataSourceBuildItem> jdbcDataSource) {
202
219
203
220
@ BuildStep
204
221
List <ReflectiveClassBuildItem > reflectiveClasses (QuartzBuildTimeConfig config ,
205
- QuartzJDBCDriverDialectBuildItem driverDialect ) {
222
+ QuartzJDBCDriverDialectBuildItem driverDialect , List < JdbcDataSourceBuildItem > jdbcDataSourceBuildItems ) {
206
223
List <ReflectiveClassBuildItem > reflectiveClasses = new ArrayList <>();
207
224
208
225
if (config .serializeJobData ()) {
@@ -249,12 +266,23 @@ List<ReflectiveClassBuildItem> reflectiveClasses(QuartzBuildTimeConfig config,
249
266
reflectiveClasses .add (ReflectiveClassBuildItem .builder (Connection .class )
250
267
.reason (getClass ().getName ()).methods ()
251
268
.fields ().build ());
252
- reflectiveClasses .add (ReflectiveClassBuildItem .builder (driverDialect .getDriver ().get ())
253
- .reason (getClass ().getName ())
254
- .methods ().build ());
255
- reflectiveClasses .add (ReflectiveClassBuildItem .builder ("io.quarkus.quartz.runtime.QuartzSchedulerImpl$InvokerJob" )
256
- .reason (getClass ().getName ())
257
- .methods ().fields ().build ());
269
+ if (driverDialect .getDriver ().isPresent ()) {
270
+ // build time datasource resolution
271
+ reflectiveClasses .add (ReflectiveClassBuildItem .builder (driverDialect .getDriver ().get ())
272
+ .reason (getClass ().getName ())
273
+ .methods ().build ());
274
+ } else {
275
+ // deferred datasource resolution, register all DB kinds we can derive from configuration
276
+ for (JdbcDataSourceBuildItem jdbcDataSourceBuildItem : jdbcDataSourceBuildItems ) {
277
+ reflectiveClasses .add (ReflectiveClassBuildItem .builder (guessDriver (jdbcDataSourceBuildItem ))
278
+ .reason (getClass ().getName ())
279
+ .methods ().build ());
280
+ }
281
+ reflectiveClasses
282
+ .add (ReflectiveClassBuildItem .builder ("io.quarkus.quartz.runtime.QuartzSchedulerImpl$InvokerJob" )
283
+ .reason (getClass ().getName ())
284
+ .methods ().fields ().build ());
285
+ }
258
286
}
259
287
260
288
reflectiveClasses
@@ -346,7 +374,8 @@ public void quartzSupportBean(
346
374
syntheticBeanBuildItemBuildProducer .produce (SyntheticBeanBuildItem .configure (QuartzSupport .class )
347
375
.scope (Singleton .class ) // this should be @ApplicationScoped but it fails for some reason
348
376
.setRuntimeInit ()
349
- .supplier (recorder .quartzSupportSupplier (driverDialect .getDriver (), nonconcurrentMethods ))
377
+ .supplier (recorder .quartzSupportSupplier (driverDialect .getDriver (), driverDialect .getDataSources (),
378
+ nonconcurrentMethods ))
350
379
.done ());
351
380
}
352
381
}
0 commit comments