This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git

commit eb9e7247b58cc54b5e943f0c0974c9e8c06476ef
Author: Claus Ibsen <claus.ib...@gmail.com>
AuthorDate: Tue Sep 6 07:15:58 2022 +0200

    CAMEL-18143: camel-quartz - Quartz scheduler should be autowired on 
component.
---
 .../quartz/QuartzComponentConfigurer.java          |  5 ++++
 .../org/apache/camel/component/quartz/quartz.json  |  2 +-
 .../camel/component/quartz/QuartzComponent.java    | 28 ++++++++++++----------
 3 files changed, 21 insertions(+), 14 deletions(-)

diff --git 
a/components/camel-quartz/src/generated/java/org/apache/camel/component/quartz/QuartzComponentConfigurer.java
 
b/components/camel-quartz/src/generated/java/org/apache/camel/component/quartz/QuartzComponentConfigurer.java
index a9027c21b6b..1954f2530e1 100644
--- 
a/components/camel-quartz/src/generated/java/org/apache/camel/component/quartz/QuartzComponentConfigurer.java
+++ 
b/components/camel-quartz/src/generated/java/org/apache/camel/component/quartz/QuartzComponentConfigurer.java
@@ -47,6 +47,11 @@ public class QuartzComponentConfigurer extends 
PropertyConfigurerSupport impleme
         }
     }
 
+    @Override
+    public String[] getAutowiredNames() {
+        return new String[]{"scheduler"};
+    }
+
     @Override
     public Class<?> getOptionType(String name, boolean ignoreCase) {
         switch (ignoreCase ? name.toLowerCase() : name) {
diff --git 
a/components/camel-quartz/src/generated/resources/org/apache/camel/component/quartz/quartz.json
 
b/components/camel-quartz/src/generated/resources/org/apache/camel/component/quartz/quartz.json
index 17aa65e0865..88918eee244 100644
--- 
a/components/camel-quartz/src/generated/resources/org/apache/camel/component/quartz/quartz.json
+++ 
b/components/camel-quartz/src/generated/resources/org/apache/camel/component/quartz/quartz.json
@@ -30,7 +30,7 @@
     "propertiesFile": { "kind": "property", "displayName": "Properties File", 
"group": "consumer", "label": "", "required": false, "type": "string", 
"javaType": "java.lang.String", "deprecated": false, "autowired": false, 
"secret": false, "description": "File name of the properties to load from the 
classpath" },
     "propertiesRef": { "kind": "property", "displayName": "Properties Ref", 
"group": "consumer", "label": "", "required": false, "type": "string", 
"javaType": "java.lang.String", "deprecated": false, "autowired": false, 
"secret": false, "description": "References to an existing Properties or Map to 
lookup in the registry to use for configuring quartz." },
     "autowiredEnabled": { "kind": "property", "displayName": "Autowired 
Enabled", "group": "advanced", "label": "advanced", "required": false, "type": 
"boolean", "javaType": "boolean", "deprecated": false, "autowired": false, 
"secret": false, "defaultValue": true, "description": "Whether autowiring is 
enabled. This is used for automatic autowiring options (the option must be 
marked as autowired) by looking up in the registry to find if there is a single 
instance of matching type, which t [...]
-    "scheduler": { "kind": "property", "displayName": "Scheduler", "group": 
"advanced", "label": "advanced", "required": false, "type": "object", 
"javaType": "org.quartz.Scheduler", "deprecated": false, "autowired": false, 
"secret": false, "description": "To use the custom configured Quartz scheduler, 
instead of creating a new Scheduler." },
+    "scheduler": { "kind": "property", "displayName": "Scheduler", "group": 
"advanced", "label": "advanced", "required": false, "type": "object", 
"javaType": "org.quartz.Scheduler", "deprecated": false, "autowired": true, 
"secret": false, "description": "To use the custom configured Quartz scheduler, 
instead of creating a new Scheduler." },
     "schedulerFactory": { "kind": "property", "displayName": "Scheduler 
Factory", "group": "advanced", "label": "advanced", "required": false, "type": 
"object", "javaType": "org.quartz.SchedulerFactory", "deprecated": false, 
"autowired": false, "secret": false, "description": "To use the custom 
SchedulerFactory which is used to create the Scheduler." },
     "autoStartScheduler": { "kind": "property", "displayName": "Auto Start 
Scheduler", "group": "scheduler", "label": "scheduler", "required": false, 
"type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": 
false, "secret": false, "defaultValue": true, "description": "Whether or not 
the scheduler should be auto started. This options is default true" },
     "interruptJobsOnShutdown": { "kind": "property", "displayName": "Interrupt 
Jobs On Shutdown", "group": "scheduler", "label": "scheduler", "required": 
false, "type": "boolean", "javaType": "boolean", "deprecated": false, 
"autowired": false, "secret": false, "defaultValue": false, "description": 
"Whether to interrupt jobs on shutdown which forces the scheduler to shutdown 
quicker and attempt to interrupt any running jobs. If this is enabled then any 
running jobs can fail due to being i [...]
diff --git 
a/components/camel-quartz/src/main/java/org/apache/camel/component/quartz/QuartzComponent.java
 
b/components/camel-quartz/src/main/java/org/apache/camel/component/quartz/QuartzComponent.java
index a9ae00ca267..b4246b14796 100644
--- 
a/components/camel-quartz/src/main/java/org/apache/camel/component/quartz/QuartzComponent.java
+++ 
b/components/camel-quartz/src/main/java/org/apache/camel/component/quartz/QuartzComponent.java
@@ -50,7 +50,7 @@ import org.slf4j.LoggerFactory;
 
 /**
  * This component will hold a Quartz Scheduler that will provide scheduled 
timer based endpoint that generate a
- * QuartzMessage to a route. Currently it support Cron and Simple trigger 
scheduling type.
+ * QuartzMessage to a route.
  */
 @Component("quartz")
 public class QuartzComponent extends DefaultComponent implements 
ExtendedStartupListener {
@@ -59,8 +59,9 @@ public class QuartzComponent extends DefaultComponent 
implements ExtendedStartup
 
     private final List<SchedulerInitTask> schedulerInitTasks = new 
ArrayList<>();
     private volatile boolean schedulerInitTasksDone;
+    private volatile boolean shouldStopScheduler;
 
-    @Metadata(label = "advanced")
+    @Metadata(label = "advanced", autowired = true)
     private Scheduler scheduler;
     @Metadata(label = "advanced")
     private SchedulerFactory schedulerFactory;
@@ -242,7 +243,7 @@ public class QuartzComponent extends DefaultComponent 
implements ExtendedStartup
             // enable jmx unless configured to not do so
             if (enableJmx && 
!prop.containsKey("org.quartz.scheduler.jmx.export")) {
                 prop.put("org.quartz.scheduler.jmx.export", "true");
-                LOG.info("Setting org.quartz.scheduler.jmx.export=true to 
ensure QuartzScheduler(s) will be enlisted in JMX.");
+                LOG.info("Setting org.quartz.scheduler.jmx.export=true to 
ensure QuartzScheduler(s) will be enlisted in JMX");
             }
 
             answer = new StdSchedulerFactory(prop);
@@ -284,7 +285,7 @@ public class QuartzComponent extends DefaultComponent 
implements ExtendedStartup
             // enable jmx unless configured to not do so
             if (enableJmx && 
!prop.containsKey("org.quartz.scheduler.jmx.export")) {
                 prop.put("org.quartz.scheduler.jmx.export", "true");
-                LOG.info("Setting org.quartz.scheduler.jmx.export=true to 
ensure QuartzScheduler(s) will be enlisted in JMX.");
+                LOG.info("Setting org.quartz.scheduler.jmx.export=true to 
ensure QuartzScheduler(s) will be enlisted in JMX");
             }
 
             answer = new StdSchedulerFactory(prop);
@@ -463,7 +464,8 @@ public class QuartzComponent extends DefaultComponent 
implements ExtendedStartup
     }
 
     private void createAndInitScheduler() throws SchedulerException {
-        LOG.info("Create and initializing scheduler.");
+        LOG.debug("Creating and initializing Quartz scheduler");
+        shouldStopScheduler = true;
         scheduler = createScheduler();
 
         SchedulerContext quartzContext = storeCamelContextInQuartzContext();
@@ -477,7 +479,7 @@ public class QuartzComponent extends DefaultComponent 
implements ExtendedStartup
         // Store CamelContext into QuartzContext space
         SchedulerContext quartzContext = scheduler.getContext();
         String camelContextName = 
QuartzHelper.getQuartzContextName(getCamelContext());
-        LOG.debug("Storing camelContextName={} into Quartz Context space.", 
camelContextName);
+        LOG.debug("Storing camelContextName={} into Quartz Context space", 
camelContextName);
         quartzContext.put(QuartzConstants.QUARTZ_CAMEL_CONTEXT + "-" + 
camelContextName, getCamelContext());
         return quartzContext;
     }
@@ -490,18 +492,18 @@ public class QuartzComponent extends DefaultComponent 
implements ExtendedStartup
     protected void doStop() throws Exception {
         super.doStop();
 
-        if (scheduler != null) {
+        if (scheduler != null && shouldStopScheduler) {
             if (isInterruptJobsOnShutdown()) {
-                LOG.info("Shutting down scheduler. (will interrupts jobs to 
shutdown quicker.)");
+                LOG.info("Shutting down Quartz scheduler (will interrupts jobs 
to shutdown quicker)");
                 scheduler.shutdown(false);
                 scheduler = null;
             } else {
                 AtomicInteger number = (AtomicInteger) 
scheduler.getContext().get(QuartzConstants.QUARTZ_CAMEL_JOBS_COUNT);
                 if (number != null && number.get() > 0) {
-                    LOG.info("Cannot shutdown scheduler: {} as there are still 
{} jobs registered.",
+                    LOG.info("Cannot shutdown Quartz scheduler: {} as there 
are still {} jobs registered",
                             scheduler.getSchedulerName(), number.get());
                 } else {
-                    LOG.info("Shutting down scheduler. (will wait for all jobs 
to complete first.)");
+                    LOG.info("Shutting down Quartz scheduler (will wait for 
all jobs to complete first)");
                     scheduler.shutdown(true);
                     scheduler = null;
                 }
@@ -543,12 +545,12 @@ public class QuartzComponent extends DefaultComponent 
implements ExtendedStartup
 
         // Now scheduler is ready, let see how we should start it.
         if (!autoStartScheduler) {
-            LOG.info("Not starting scheduler because autoStartScheduler is set 
to false");
+            LOG.info("Not starting Quartz scheduler: {} because 
autoStartScheduler is set to false", scheduler);
         } else {
             if (scheduler.isStarted()) {
-                LOG.info("The scheduler has already been started");
+                LOG.info("The Quartz scheduler: {} has already been started", 
scheduler);
             } else {
-                LOG.info("Starting scheduler");
+                LOG.info("Starting Quartz scheduler: {}", scheduler);
                 scheduler.start();
             }
         }

Reply via email to