You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2021/06/24 13:56:13 UTC

[camel] branch main updated: CAMEL-16581: camel-quartz - Should interrupt current inlfight exchanges if configured to interrupt on shutdown. Added in docs the danger of using this.

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


The following commit(s) were added to refs/heads/main by this push:
     new e32cd36  CAMEL-16581: camel-quartz - Should interrupt current inlfight exchanges if configured to interrupt on shutdown. Added in docs the danger of using this.
e32cd36 is described below

commit e32cd364b64ade71aa2efd990ab034b66a1fc412
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Thu Jun 24 15:55:43 2021 +0200

    CAMEL-16581: camel-quartz - Should interrupt current inlfight exchanges if configured to interrupt on shutdown. Added in docs the danger of using this.
---
 .../camel/catalog/docs/quartz-component.adoc       |  2 +-
 .../org/apache/camel/component/quartz/quartz.json  |  2 +-
 .../src/main/docs/quartz-component.adoc            |  2 +-
 .../apache/camel/component/quartz/CamelJob.java    | 22 +++++++++++++++++++++-
 .../camel/component/quartz/QuartzComponent.java    |  5 ++++-
 .../component/quartz/QuartzInterruptTest.java      |  1 +
 .../dsl/QuartzComponentBuilderFactory.java         |  5 +++++
 .../modules/ROOT/pages/quartz-component.adoc       |  2 +-
 8 files changed, 35 insertions(+), 6 deletions(-)

diff --git a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/quartz-component.adoc b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/quartz-component.adoc
index 672c2ad..18bce1a 100644
--- a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/quartz-component.adoc
+++ b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/quartz-component.adoc
@@ -71,7 +71,7 @@ The Quartz component supports 13 options, which are listed below.
 | *scheduler* (advanced) | To use the custom configured Quartz scheduler, instead of creating a new Scheduler. |  | Scheduler
 | *schedulerFactory* (advanced) | To use the custom SchedulerFactory which is used to create the Scheduler. |  | SchedulerFactory
 | *autoStartScheduler* (scheduler) | Whether or not the scheduler should be auto started. This options is default true | true | boolean
-| *interruptJobsOnShutdown* (scheduler) | 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 interrupted. | false | boolean
+| *interruptJobsOnShutdown* (scheduler) | 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 interrupted. When a job is interrupted then Camel will mark the exchange to stop continue routing and set java.util.concurrent.RejectedExecutionException as caused exception. Therefore use this with care, as its often better to allow Camel jobs to complete an [...]
 | *startDelayedSeconds* (scheduler) | Seconds to wait before starting the quartz scheduler. |  | int
 |===
 // component options: END
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 6881af8..ad1b5c2 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
@@ -33,7 +33,7 @@
     "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." },
     "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 [...]
+    "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 [...]
     "startDelayedSeconds": { "kind": "property", "displayName": "Start Delayed Seconds", "group": "scheduler", "label": "scheduler", "required": false, "type": "integer", "javaType": "int", "deprecated": false, "autowired": false, "secret": false, "description": "Seconds to wait before starting the quartz scheduler." }
   },
   "properties": {
diff --git a/components/camel-quartz/src/main/docs/quartz-component.adoc b/components/camel-quartz/src/main/docs/quartz-component.adoc
index 672c2ad..18bce1a 100644
--- a/components/camel-quartz/src/main/docs/quartz-component.adoc
+++ b/components/camel-quartz/src/main/docs/quartz-component.adoc
@@ -71,7 +71,7 @@ The Quartz component supports 13 options, which are listed below.
 | *scheduler* (advanced) | To use the custom configured Quartz scheduler, instead of creating a new Scheduler. |  | Scheduler
 | *schedulerFactory* (advanced) | To use the custom SchedulerFactory which is used to create the Scheduler. |  | SchedulerFactory
 | *autoStartScheduler* (scheduler) | Whether or not the scheduler should be auto started. This options is default true | true | boolean
-| *interruptJobsOnShutdown* (scheduler) | 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 interrupted. | false | boolean
+| *interruptJobsOnShutdown* (scheduler) | 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 interrupted. When a job is interrupted then Camel will mark the exchange to stop continue routing and set java.util.concurrent.RejectedExecutionException as caused exception. Therefore use this with care, as its often better to allow Camel jobs to complete an [...]
 | *startDelayedSeconds* (scheduler) | Seconds to wait before starting the quartz scheduler. |  | int
 |===
 // component options: END
diff --git a/components/camel-quartz/src/main/java/org/apache/camel/component/quartz/CamelJob.java b/components/camel-quartz/src/main/java/org/apache/camel/component/quartz/CamelJob.java
index 1041c96..13e3181 100644
--- a/components/camel-quartz/src/main/java/org/apache/camel/component/quartz/CamelJob.java
+++ b/components/camel-quartz/src/main/java/org/apache/camel/component/quartz/CamelJob.java
@@ -17,6 +17,8 @@
 package org.apache.camel.component.quartz;
 
 import java.util.Collection;
+import java.util.concurrent.RejectedExecutionException;
+import java.util.concurrent.atomic.AtomicReference;
 
 import org.apache.camel.AsyncProcessor;
 import org.apache.camel.CamelContext;
@@ -25,6 +27,7 @@ import org.apache.camel.DelegateEndpoint;
 import org.apache.camel.Endpoint;
 import org.apache.camel.Exchange;
 import org.apache.camel.Route;
+import org.quartz.InterruptableJob;
 import org.quartz.Job;
 import org.quartz.JobDetail;
 import org.quartz.JobExecutionContext;
@@ -33,6 +36,7 @@ import org.quartz.JobKey;
 import org.quartz.SchedulerContext;
 import org.quartz.SchedulerException;
 import org.quartz.TriggerKey;
+import org.quartz.UnableToInterruptJobException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -40,9 +44,11 @@ import org.slf4j.LoggerFactory;
  * This is a Quartz Job that is scheduled by QuartzEndpoint's Consumer and will call it to produce a QuartzMessage
  * sending to a route.
  */
-public class CamelJob implements Job {
+public class CamelJob implements Job, InterruptableJob {
     private static final Logger LOG = LoggerFactory.getLogger(CamelJob.class);
 
+    private final AtomicReference<Exchange> current = new AtomicReference<>();
+
     @Override
     public void execute(JobExecutionContext context) throws JobExecutionException {
         Exchange exchange = null;
@@ -55,6 +61,7 @@ public class CamelJob implements Job {
             QuartzEndpoint endpoint = lookupQuartzEndpoint(camelContext, context);
             exchange = endpoint.createExchange();
             exchange.setIn(new QuartzMessage(exchange, context));
+            current.set(exchange);
 
             AsyncProcessor processor = endpoint.getProcessor();
             try {
@@ -65,6 +72,8 @@ public class CamelJob implements Job {
                 }
             } catch (Exception e) {
                 exchange.setException(e);
+            } finally {
+                current.set(null);
             }
 
             if (exchange.getException() != null) {
@@ -169,4 +178,15 @@ public class CamelJob implements Job {
         }
         return null;
     }
+
+    @Override
+    public void interrupt() throws UnableToInterruptJobException {
+        Exchange exchange = current.get();
+        if (exchange != null) {
+            // mark the exchange to stop continue routing as we want to interrupt
+            LOG.debug("Quartz interrupted job during shutdown on exchange: {}", exchange);
+            exchange.setRouteStop(true);
+            exchange.setException(new RejectedExecutionException("Quartz interrupted job during shutdown"));
+        }
+    }
 }
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 42a4c7a..9c43e28 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
@@ -193,7 +193,10 @@ public class QuartzComponent extends DefaultComponent implements ExtendedStartup
 
     /**
      * 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 interrupted.
+     * running jobs. If this is enabled then any running jobs can fail due to being interrupted. When a job is
+     * interrupted then Camel will mark the exchange to stop continue routing and set
+     * {@link java.util.concurrent.RejectedExecutionException} as caused exception. Therefore use this with care, as its
+     * often better to allow Camel jobs to complete and shutdown gracefully.
      */
     public void setInterruptJobsOnShutdown(boolean interruptJobsOnShutdown) {
         this.interruptJobsOnShutdown = interruptJobsOnShutdown;
diff --git a/components/camel-quartz/src/test/java/org/apache/camel/component/quartz/QuartzInterruptTest.java b/components/camel-quartz/src/test/java/org/apache/camel/component/quartz/QuartzInterruptTest.java
index b4f2150..583ea00 100644
--- a/components/camel-quartz/src/test/java/org/apache/camel/component/quartz/QuartzInterruptTest.java
+++ b/components/camel-quartz/src/test/java/org/apache/camel/component/quartz/QuartzInterruptTest.java
@@ -43,6 +43,7 @@ public class QuartzInterruptTest extends BaseQuartzTest {
                 quartz.setInterruptJobsOnShutdown(true);
 
                 from("quartz://myGroup/myTimerName?trigger.repeatInterval=2&trigger.repeatCount=100").routeId("myRoute")
+                        .delay(constant(1000))
                         .to("mock:result");
             }
         };
diff --git a/core/camel-componentdsl/src/generated/java/org/apache/camel/builder/component/dsl/QuartzComponentBuilderFactory.java b/core/camel-componentdsl/src/generated/java/org/apache/camel/builder/component/dsl/QuartzComponentBuilderFactory.java
index 54b783a..2322568 100644
--- a/core/camel-componentdsl/src/generated/java/org/apache/camel/builder/component/dsl/QuartzComponentBuilderFactory.java
+++ b/core/camel-componentdsl/src/generated/java/org/apache/camel/builder/component/dsl/QuartzComponentBuilderFactory.java
@@ -243,6 +243,11 @@ public interface QuartzComponentBuilderFactory {
          * 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 interrupted.
+         * When a job is interrupted then Camel will mark the exchange to stop
+         * continue routing and set
+         * java.util.concurrent.RejectedExecutionException as caused exception.
+         * Therefore use this with care, as its often better to allow Camel jobs
+         * to complete and shutdown gracefully.
          * 
          * The option is a: &lt;code&gt;boolean&lt;/code&gt; type.
          * 
diff --git a/docs/components/modules/ROOT/pages/quartz-component.adoc b/docs/components/modules/ROOT/pages/quartz-component.adoc
index c3eed9d..02d53bb 100644
--- a/docs/components/modules/ROOT/pages/quartz-component.adoc
+++ b/docs/components/modules/ROOT/pages/quartz-component.adoc
@@ -73,7 +73,7 @@ The Quartz component supports 13 options, which are listed below.
 | *scheduler* (advanced) | To use the custom configured Quartz scheduler, instead of creating a new Scheduler. |  | Scheduler
 | *schedulerFactory* (advanced) | To use the custom SchedulerFactory which is used to create the Scheduler. |  | SchedulerFactory
 | *autoStartScheduler* (scheduler) | Whether or not the scheduler should be auto started. This options is default true | true | boolean
-| *interruptJobsOnShutdown* (scheduler) | 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 interrupted. | false | boolean
+| *interruptJobsOnShutdown* (scheduler) | 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 interrupted. When a job is interrupted then Camel will mark the exchange to stop continue routing and set java.util.concurrent.RejectedExecutionException as caused exception. Therefore use this with care, as its often better to allow Camel jobs to complete an [...]
 | *startDelayedSeconds* (scheduler) | Seconds to wait before starting the quartz scheduler. |  | int
 |===
 // component options: END