@@ -486,61 +486,21 @@ public void afterPropertiesSet() throws Exception {
486
486
this .resourceLoader = this .applicationContext ;
487
487
}
488
488
489
- // Initialize the SchedulerFactory instance...
490
- SchedulerFactory schedulerFactory = prepareSchedulerFactory ();
491
-
492
- if (this .resourceLoader != null ) {
493
- // Make given ResourceLoader available for SchedulerFactory configuration.
494
- configTimeResourceLoaderHolder .set (this .resourceLoader );
495
- }
496
- if (this .taskExecutor != null ) {
497
- // Make given TaskExecutor available for SchedulerFactory configuration.
498
- configTimeTaskExecutorHolder .set (this .taskExecutor );
499
- }
500
- if (this .dataSource != null ) {
501
- // Make given DataSource available for SchedulerFactory configuration.
502
- configTimeDataSourceHolder .set (this .dataSource );
503
- }
504
- if (this .nonTransactionalDataSource != null ) {
505
- // Make given non-transactional DataSource available for SchedulerFactory configuration.
506
- configTimeNonTransactionalDataSourceHolder .set (this .nonTransactionalDataSource );
507
- }
508
-
509
- // Get Scheduler instance from SchedulerFactory.
489
+ // Initialize the Scheduler instance...
490
+ this .scheduler = prepareScheduler (prepareSchedulerFactory ());
510
491
try {
511
- this .scheduler = createScheduler (schedulerFactory , this .schedulerName );
512
- populateSchedulerContext ();
513
-
514
- if (!this .jobFactorySet && !(this .scheduler instanceof RemoteScheduler )) {
515
- // Use AdaptableJobFactory as default for a local Scheduler, unless when
516
- // explicitly given a null value through the "jobFactory" bean property.
517
- this .jobFactory = new AdaptableJobFactory ();
518
- }
519
- if (this .jobFactory != null ) {
520
- if (this .jobFactory instanceof SchedulerContextAware ) {
521
- ((SchedulerContextAware ) this .jobFactory ).setSchedulerContext (this .scheduler .getContext ());
522
- }
523
- this .scheduler .setJobFactory (this .jobFactory );
524
- }
492
+ registerListeners ();
493
+ registerJobsAndTriggers ();
525
494
}
526
-
527
- finally {
528
- if (this .resourceLoader != null ) {
529
- configTimeResourceLoaderHolder .remove ();
530
- }
531
- if (this .taskExecutor != null ) {
532
- configTimeTaskExecutorHolder .remove ();
495
+ catch (Exception ex ) {
496
+ try {
497
+ this .scheduler .shutdown (true );
533
498
}
534
- if (this .dataSource != null ) {
535
- configTimeDataSourceHolder .remove ();
536
- }
537
- if (this .nonTransactionalDataSource != null ) {
538
- configTimeNonTransactionalDataSourceHolder .remove ();
499
+ catch (Exception ex2 ) {
500
+ logger .debug ("Scheduler shutdown exception after registration failure" , ex2 );
539
501
}
502
+ throw ex ;
540
503
}
541
-
542
- registerListeners ();
543
- registerJobsAndTriggers ();
544
504
}
545
505
546
506
@@ -607,6 +567,59 @@ private void initSchedulerFactory(StdSchedulerFactory schedulerFactory) throws S
607
567
schedulerFactory .initialize (mergedProps );
608
568
}
609
569
570
+ private Scheduler prepareScheduler (SchedulerFactory schedulerFactory ) throws SchedulerException {
571
+ if (this .resourceLoader != null ) {
572
+ // Make given ResourceLoader available for SchedulerFactory configuration.
573
+ configTimeResourceLoaderHolder .set (this .resourceLoader );
574
+ }
575
+ if (this .taskExecutor != null ) {
576
+ // Make given TaskExecutor available for SchedulerFactory configuration.
577
+ configTimeTaskExecutorHolder .set (this .taskExecutor );
578
+ }
579
+ if (this .dataSource != null ) {
580
+ // Make given DataSource available for SchedulerFactory configuration.
581
+ configTimeDataSourceHolder .set (this .dataSource );
582
+ }
583
+ if (this .nonTransactionalDataSource != null ) {
584
+ // Make given non-transactional DataSource available for SchedulerFactory configuration.
585
+ configTimeNonTransactionalDataSourceHolder .set (this .nonTransactionalDataSource );
586
+ }
587
+
588
+ // Get Scheduler instance from SchedulerFactory.
589
+ try {
590
+ Scheduler scheduler = createScheduler (schedulerFactory , this .schedulerName );
591
+ populateSchedulerContext (scheduler );
592
+
593
+ if (!this .jobFactorySet && !(scheduler instanceof RemoteScheduler )) {
594
+ // Use AdaptableJobFactory as default for a local Scheduler, unless when
595
+ // explicitly given a null value through the "jobFactory" bean property.
596
+ this .jobFactory = new AdaptableJobFactory ();
597
+ }
598
+ if (this .jobFactory != null ) {
599
+ if (this .jobFactory instanceof SchedulerContextAware ) {
600
+ ((SchedulerContextAware ) this .jobFactory ).setSchedulerContext (scheduler .getContext ());
601
+ }
602
+ scheduler .setJobFactory (this .jobFactory );
603
+ }
604
+ return scheduler ;
605
+ }
606
+
607
+ finally {
608
+ if (this .resourceLoader != null ) {
609
+ configTimeResourceLoaderHolder .remove ();
610
+ }
611
+ if (this .taskExecutor != null ) {
612
+ configTimeTaskExecutorHolder .remove ();
613
+ }
614
+ if (this .dataSource != null ) {
615
+ configTimeDataSourceHolder .remove ();
616
+ }
617
+ if (this .nonTransactionalDataSource != null ) {
618
+ configTimeNonTransactionalDataSourceHolder .remove ();
619
+ }
620
+ }
621
+ }
622
+
610
623
/**
611
624
* Create the Scheduler instance for the given factory and scheduler name.
612
625
* Called by {@link #afterPropertiesSet}.
@@ -658,10 +671,10 @@ protected Scheduler createScheduler(SchedulerFactory schedulerFactory, @Nullable
658
671
* Expose the specified context attributes and/or the current
659
672
* ApplicationContext in the Quartz SchedulerContext.
660
673
*/
661
- private void populateSchedulerContext () throws SchedulerException {
674
+ private void populateSchedulerContext (Scheduler scheduler ) throws SchedulerException {
662
675
// Put specified objects into Scheduler context.
663
676
if (this .schedulerContextMap != null ) {
664
- getScheduler () .getContext ().putAll (this .schedulerContextMap );
677
+ scheduler .getContext ().putAll (this .schedulerContextMap );
665
678
}
666
679
667
680
// Register ApplicationContext in Scheduler context.
@@ -671,7 +684,7 @@ private void populateSchedulerContext() throws SchedulerException {
671
684
"SchedulerFactoryBean needs to be set up in an ApplicationContext " +
672
685
"to be able to handle an 'applicationContextSchedulerContextKey'" );
673
686
}
674
- getScheduler () .getContext ().put (this .applicationContextSchedulerContextKey , this .applicationContext );
687
+ scheduler .getContext ().put (this .applicationContextSchedulerContextKey , this .applicationContext );
675
688
}
676
689
}
677
690
0 commit comments