This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch CAMEL-27378-eventbridge-queue-reuse in repository https://gitbox.apache.org/repos/asf/camel.git
commit 588c9a614dee8438044e080d3682355f70114dbd Author: Claus Ibsen <[email protected]> AuthorDate: Sun Nov 22 09:49:57 2026 +0100 CAMEL-27378: camel-aws2-eventbridge - with deleteQueueOnShutdown=false the consumer reuses its auto-created queue With autoCreateQueue and deleteQueueOnShutdown=false, the queue name had a random part and the target was removed on shutdown, so every restart created another queue and left the previous one behind, without a target. A queue that is kept on shutdown now has a name derived from the event bus and the rule name (camel-eventbridge-<rule>-<hash>), so CreateQueue returns the existing queue on the next start. Like a queue given with queueUrl, it gets a target id derived from the queue ARN and keeps its target on shutdown (as the option description says), so it collects the events while the consumer is stopped. The queue policy (aws:SourceArn of the rule) is set again on every start. With the default deleteQueueOnShutdown=true, every consumer still gets its own queue with a random part, deleted on shutdown. The rule name in the queue name now has dots replaced and is cut to 45 characters, so a long rule name or one with dots no longer gives an invalid queue name. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Claus Ibsen <[email protected]> --- .../camel/catalog/components/aws2-eventbridge.json | 4 +- .../catalog/docs/aws2-eventbridge-component.adoc | 11 +- .../aws2/eventbridge/aws2-eventbridge.json | 4 +- .../src/main/docs/aws2-eventbridge-component.adoc | 11 +- .../aws2/eventbridge/EventbridgeConfiguration.java | 9 +- .../aws2/eventbridge/EventbridgeConsumer.java | 73 +++++++++---- .../aws2/eventbridge/EventbridgeConsumerTest.java | 74 ++++++++++++-- .../localstack/EventbridgeConsumerKeepQueueIT.java | 113 +++++++++++++++++++++ .../ROOT/pages/camel-4x-upgrade-guide-4_24.adoc | 9 ++ .../Aws2EventbridgeComponentBuilderFactory.java | 5 +- .../dsl/EventbridgeEndpointBuilderFactory.java | 10 +- 11 files changed, 288 insertions(+), 35 deletions(-) diff --git a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/aws2-eventbridge.json b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/aws2-eventbridge.json index 50ea1bdd7b6a..01486115e1aa 100644 --- a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/aws2-eventbridge.json +++ b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/aws2-eventbridge.json @@ -33,7 +33,7 @@ "uriEndpointOverride": { "index": 6, "kind": "property", "displayName": "Uri Endpoint Override", "group": "common", "label": "", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "configurationClass": "org.apache.camel.component.aws2.eventbridge.EventbridgeConfiguration", "configurationField": "configuration", "description": "Set the overriding uri endpoint. This option needs to be used in combination with o [...] "autoCreateQueue": { "index": 7, "kind": "property", "displayName": "Auto Create Queue", "group": "consumer", "label": "consumer", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": true, "configurationClass": "org.apache.camel.component.aws2.eventbridge.EventbridgeConfiguration", "configurationField": "configuration", "description": "Whether to auto-create an SQS queue and wire it as an EventBridge r [...] "bridgeErrorHandler": { "index": 8, "kind": "property", "displayName": "Bridge Error Handler", "group": "consumer", "label": "consumer", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": false, "description": "Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions (if possible) occurred while the Camel consumer is trying to pickup incoming messages, or the like [...] - "deleteQueueOnShutdown": { "index": 9, "kind": "property", "displayName": "Delete Queue On Shutdown", "group": "consumer", "label": "consumer", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": true, "configurationClass": "org.apache.camel.component.aws2.eventbridge.EventbridgeConfiguration", "configurationField": "configuration", "description": "Whether to delete the auto-created SQS queue and remov [...] + "deleteQueueOnShutdown": { "index": 9, "kind": "property", "displayName": "Delete Queue On Shutdown", "group": "consumer", "label": "consumer", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": true, "configurationClass": "org.apache.camel.component.aws2.eventbridge.EventbridgeConfiguration", "configurationField": "configuration", "description": "Whether to delete the auto-created SQS queue and remov [...] "maxMessagesPerPoll": { "index": 10, "kind": "property", "displayName": "Max Messages Per Poll", "group": "consumer", "label": "consumer", "required": false, "type": "integer", "javaType": "int", "deprecated": false, "autowired": false, "secret": false, "defaultValue": 10, "configurationClass": "org.apache.camel.component.aws2.eventbridge.EventbridgeConfiguration", "configurationField": "configuration", "description": "The maximum number of messages to receive per poll from SQS." }, "queueUrl": { "index": 11, "kind": "property", "displayName": "Queue Url", "group": "consumer", "label": "consumer", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "configurationClass": "org.apache.camel.component.aws2.eventbridge.EventbridgeConfiguration", "configurationField": "configuration", "description": "The URL of an existing SQS queue to use as EventBridge target. If not specified, a queue is aut [...] "ruleName": { "index": 12, "kind": "property", "displayName": "Rule Name", "group": "consumer", "label": "consumer", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "configurationClass": "org.apache.camel.component.aws2.eventbridge.EventbridgeConfiguration", "configurationField": "configuration", "description": "The EventBridge rule name to consume events from. Required for consumer." }, @@ -89,7 +89,7 @@ "region": { "index": 5, "kind": "parameter", "displayName": "Region", "group": "common", "label": "", "required": false, "type": "enum", "javaType": "java.lang.String", "enum": [ "ap-south-2", "ap-south-1", "eu-south-1", "eu-south-2", "us-gov-east-1", "me-central-1", "il-central-1", "ca-central-1", "eu-central-1", "us-iso-west-1", "eu-central-2", "eu-isoe-west-1", "us-west-1", "us-west-2", "af-south-1", "eu-north-1", "eu-west-3", "eu-west-2", "eu-west-1", "ap-northeast-3", "ap-northe [...] "uriEndpointOverride": { "index": 6, "kind": "parameter", "displayName": "Uri Endpoint Override", "group": "common", "label": "", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "configurationClass": "org.apache.camel.component.aws2.eventbridge.EventbridgeConfiguration", "configurationField": "configuration", "description": "Set the overriding uri endpoint. This option needs to be used in combination with [...] "autoCreateQueue": { "index": 7, "kind": "parameter", "displayName": "Auto Create Queue", "group": "consumer", "label": "consumer", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": true, "configurationClass": "org.apache.camel.component.aws2.eventbridge.EventbridgeConfiguration", "configurationField": "configuration", "description": "Whether to auto-create an SQS queue and wire it as an EventBridge [...] - "deleteQueueOnShutdown": { "index": 8, "kind": "parameter", "displayName": "Delete Queue On Shutdown", "group": "consumer", "label": "consumer", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": true, "configurationClass": "org.apache.camel.component.aws2.eventbridge.EventbridgeConfiguration", "configurationField": "configuration", "description": "Whether to delete the auto-created SQS queue and remo [...] + "deleteQueueOnShutdown": { "index": 8, "kind": "parameter", "displayName": "Delete Queue On Shutdown", "group": "consumer", "label": "consumer", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": true, "configurationClass": "org.apache.camel.component.aws2.eventbridge.EventbridgeConfiguration", "configurationField": "configuration", "description": "Whether to delete the auto-created SQS queue and remo [...] "maxMessagesPerPoll": { "index": 9, "kind": "parameter", "displayName": "Max Messages Per Poll", "group": "consumer", "label": "consumer", "required": false, "type": "integer", "javaType": "int", "deprecated": false, "autowired": false, "secret": false, "defaultValue": 10, "configurationClass": "org.apache.camel.component.aws2.eventbridge.EventbridgeConfiguration", "configurationField": "configuration", "description": "The maximum number of messages to receive per poll from SQS." }, "queueUrl": { "index": 10, "kind": "parameter", "displayName": "Queue Url", "group": "consumer", "label": "consumer", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "configurationClass": "org.apache.camel.component.aws2.eventbridge.EventbridgeConfiguration", "configurationField": "configuration", "description": "The URL of an existing SQS queue to use as EventBridge target. If not specified, a queue is au [...] "ruleName": { "index": 11, "kind": "parameter", "displayName": "Rule Name", "group": "consumer", "label": "consumer", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "configurationClass": "org.apache.camel.component.aws2.eventbridge.EventbridgeConfiguration", "configurationField": "configuration", "description": "The EventBridge rule name to consume events from. Required for consumer." }, diff --git a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/aws2-eventbridge-component.adoc b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/aws2-eventbridge-component.adoc index 658633df7c2d..28d6c8bd31b4 100644 --- a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/aws2-eventbridge-component.adoc +++ b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/aws2-eventbridge-component.adoc @@ -625,6 +625,14 @@ This is the standard AWS pattern for programmatically consuming EventBridge even A queue given with `queueUrl` keeps its target, so it collects the events while no consumer runs, and all the consumers that share it use the same target (its id is derived from the queue ARN). +An auto-created queue has a random part in its name, so every consumer has its own queue, which is deleted on +shutdown. With `deleteQueueOnShutdown=false`, the consumer creates a durable queue instead: its name is +`camel-eventbridge-<rule name>-<hash of the event bus and the rule name>`, so the next start reuses the queue (SQS +`CreateQueue` returns the existing queue), and the queue keeps its target on shutdown, like a queue given with +`queueUrl`. The queue collects the events while the consumer is stopped, and the consumers of the rule that use +`deleteQueueOnShutdown=false` share one queue (they compete for the events instead of each getting a copy). Delete the +queue yourself when the route is no longer used. + === Consumer Configuration Options [width="100%",cols="2,1,1,4",options="header"] @@ -649,7 +657,8 @@ consumers that share it use the same target (its id is derived from the queue AR |`deleteQueueOnShutdown` |`true` |boolean -|Whether to delete the auto-created queue and remove the EventBridge target on shutdown. +|Whether to delete the auto-created queue and remove the EventBridge target on shutdown. When `false`, the queue has a +name derived from the event bus and the rule name, the next start reuses it, and its target stays on the rule. |`maxMessagesPerPoll` |`10` diff --git a/components/camel-aws/camel-aws2-eventbridge/src/generated/resources/META-INF/org/apache/camel/component/aws2/eventbridge/aws2-eventbridge.json b/components/camel-aws/camel-aws2-eventbridge/src/generated/resources/META-INF/org/apache/camel/component/aws2/eventbridge/aws2-eventbridge.json index 50ea1bdd7b6a..01486115e1aa 100644 --- a/components/camel-aws/camel-aws2-eventbridge/src/generated/resources/META-INF/org/apache/camel/component/aws2/eventbridge/aws2-eventbridge.json +++ b/components/camel-aws/camel-aws2-eventbridge/src/generated/resources/META-INF/org/apache/camel/component/aws2/eventbridge/aws2-eventbridge.json @@ -33,7 +33,7 @@ "uriEndpointOverride": { "index": 6, "kind": "property", "displayName": "Uri Endpoint Override", "group": "common", "label": "", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "configurationClass": "org.apache.camel.component.aws2.eventbridge.EventbridgeConfiguration", "configurationField": "configuration", "description": "Set the overriding uri endpoint. This option needs to be used in combination with o [...] "autoCreateQueue": { "index": 7, "kind": "property", "displayName": "Auto Create Queue", "group": "consumer", "label": "consumer", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": true, "configurationClass": "org.apache.camel.component.aws2.eventbridge.EventbridgeConfiguration", "configurationField": "configuration", "description": "Whether to auto-create an SQS queue and wire it as an EventBridge r [...] "bridgeErrorHandler": { "index": 8, "kind": "property", "displayName": "Bridge Error Handler", "group": "consumer", "label": "consumer", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": false, "description": "Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions (if possible) occurred while the Camel consumer is trying to pickup incoming messages, or the like [...] - "deleteQueueOnShutdown": { "index": 9, "kind": "property", "displayName": "Delete Queue On Shutdown", "group": "consumer", "label": "consumer", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": true, "configurationClass": "org.apache.camel.component.aws2.eventbridge.EventbridgeConfiguration", "configurationField": "configuration", "description": "Whether to delete the auto-created SQS queue and remov [...] + "deleteQueueOnShutdown": { "index": 9, "kind": "property", "displayName": "Delete Queue On Shutdown", "group": "consumer", "label": "consumer", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": true, "configurationClass": "org.apache.camel.component.aws2.eventbridge.EventbridgeConfiguration", "configurationField": "configuration", "description": "Whether to delete the auto-created SQS queue and remov [...] "maxMessagesPerPoll": { "index": 10, "kind": "property", "displayName": "Max Messages Per Poll", "group": "consumer", "label": "consumer", "required": false, "type": "integer", "javaType": "int", "deprecated": false, "autowired": false, "secret": false, "defaultValue": 10, "configurationClass": "org.apache.camel.component.aws2.eventbridge.EventbridgeConfiguration", "configurationField": "configuration", "description": "The maximum number of messages to receive per poll from SQS." }, "queueUrl": { "index": 11, "kind": "property", "displayName": "Queue Url", "group": "consumer", "label": "consumer", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "configurationClass": "org.apache.camel.component.aws2.eventbridge.EventbridgeConfiguration", "configurationField": "configuration", "description": "The URL of an existing SQS queue to use as EventBridge target. If not specified, a queue is aut [...] "ruleName": { "index": 12, "kind": "property", "displayName": "Rule Name", "group": "consumer", "label": "consumer", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "configurationClass": "org.apache.camel.component.aws2.eventbridge.EventbridgeConfiguration", "configurationField": "configuration", "description": "The EventBridge rule name to consume events from. Required for consumer." }, @@ -89,7 +89,7 @@ "region": { "index": 5, "kind": "parameter", "displayName": "Region", "group": "common", "label": "", "required": false, "type": "enum", "javaType": "java.lang.String", "enum": [ "ap-south-2", "ap-south-1", "eu-south-1", "eu-south-2", "us-gov-east-1", "me-central-1", "il-central-1", "ca-central-1", "eu-central-1", "us-iso-west-1", "eu-central-2", "eu-isoe-west-1", "us-west-1", "us-west-2", "af-south-1", "eu-north-1", "eu-west-3", "eu-west-2", "eu-west-1", "ap-northeast-3", "ap-northe [...] "uriEndpointOverride": { "index": 6, "kind": "parameter", "displayName": "Uri Endpoint Override", "group": "common", "label": "", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "configurationClass": "org.apache.camel.component.aws2.eventbridge.EventbridgeConfiguration", "configurationField": "configuration", "description": "Set the overriding uri endpoint. This option needs to be used in combination with [...] "autoCreateQueue": { "index": 7, "kind": "parameter", "displayName": "Auto Create Queue", "group": "consumer", "label": "consumer", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": true, "configurationClass": "org.apache.camel.component.aws2.eventbridge.EventbridgeConfiguration", "configurationField": "configuration", "description": "Whether to auto-create an SQS queue and wire it as an EventBridge [...] - "deleteQueueOnShutdown": { "index": 8, "kind": "parameter", "displayName": "Delete Queue On Shutdown", "group": "consumer", "label": "consumer", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": true, "configurationClass": "org.apache.camel.component.aws2.eventbridge.EventbridgeConfiguration", "configurationField": "configuration", "description": "Whether to delete the auto-created SQS queue and remo [...] + "deleteQueueOnShutdown": { "index": 8, "kind": "parameter", "displayName": "Delete Queue On Shutdown", "group": "consumer", "label": "consumer", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": true, "configurationClass": "org.apache.camel.component.aws2.eventbridge.EventbridgeConfiguration", "configurationField": "configuration", "description": "Whether to delete the auto-created SQS queue and remo [...] "maxMessagesPerPoll": { "index": 9, "kind": "parameter", "displayName": "Max Messages Per Poll", "group": "consumer", "label": "consumer", "required": false, "type": "integer", "javaType": "int", "deprecated": false, "autowired": false, "secret": false, "defaultValue": 10, "configurationClass": "org.apache.camel.component.aws2.eventbridge.EventbridgeConfiguration", "configurationField": "configuration", "description": "The maximum number of messages to receive per poll from SQS." }, "queueUrl": { "index": 10, "kind": "parameter", "displayName": "Queue Url", "group": "consumer", "label": "consumer", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "configurationClass": "org.apache.camel.component.aws2.eventbridge.EventbridgeConfiguration", "configurationField": "configuration", "description": "The URL of an existing SQS queue to use as EventBridge target. If not specified, a queue is au [...] "ruleName": { "index": 11, "kind": "parameter", "displayName": "Rule Name", "group": "consumer", "label": "consumer", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "configurationClass": "org.apache.camel.component.aws2.eventbridge.EventbridgeConfiguration", "configurationField": "configuration", "description": "The EventBridge rule name to consume events from. Required for consumer." }, diff --git a/components/camel-aws/camel-aws2-eventbridge/src/main/docs/aws2-eventbridge-component.adoc b/components/camel-aws/camel-aws2-eventbridge/src/main/docs/aws2-eventbridge-component.adoc index 658633df7c2d..28d6c8bd31b4 100644 --- a/components/camel-aws/camel-aws2-eventbridge/src/main/docs/aws2-eventbridge-component.adoc +++ b/components/camel-aws/camel-aws2-eventbridge/src/main/docs/aws2-eventbridge-component.adoc @@ -625,6 +625,14 @@ This is the standard AWS pattern for programmatically consuming EventBridge even A queue given with `queueUrl` keeps its target, so it collects the events while no consumer runs, and all the consumers that share it use the same target (its id is derived from the queue ARN). +An auto-created queue has a random part in its name, so every consumer has its own queue, which is deleted on +shutdown. With `deleteQueueOnShutdown=false`, the consumer creates a durable queue instead: its name is +`camel-eventbridge-<rule name>-<hash of the event bus and the rule name>`, so the next start reuses the queue (SQS +`CreateQueue` returns the existing queue), and the queue keeps its target on shutdown, like a queue given with +`queueUrl`. The queue collects the events while the consumer is stopped, and the consumers of the rule that use +`deleteQueueOnShutdown=false` share one queue (they compete for the events instead of each getting a copy). Delete the +queue yourself when the route is no longer used. + === Consumer Configuration Options [width="100%",cols="2,1,1,4",options="header"] @@ -649,7 +657,8 @@ consumers that share it use the same target (its id is derived from the queue AR |`deleteQueueOnShutdown` |`true` |boolean -|Whether to delete the auto-created queue and remove the EventBridge target on shutdown. +|Whether to delete the auto-created queue and remove the EventBridge target on shutdown. When `false`, the queue has a +name derived from the event bus and the rule name, the next start reuses it, and its target stays on the rule. |`maxMessagesPerPoll` |`10` diff --git a/components/camel-aws/camel-aws2-eventbridge/src/main/java/org/apache/camel/component/aws2/eventbridge/EventbridgeConfiguration.java b/components/camel-aws/camel-aws2-eventbridge/src/main/java/org/apache/camel/component/aws2/eventbridge/EventbridgeConfiguration.java index d7bf9b95e3e5..2348bf43d138 100644 --- a/components/camel-aws/camel-aws2-eventbridge/src/main/java/org/apache/camel/component/aws2/eventbridge/EventbridgeConfiguration.java +++ b/components/camel-aws/camel-aws2-eventbridge/src/main/java/org/apache/camel/component/aws2/eventbridge/EventbridgeConfiguration.java @@ -78,7 +78,10 @@ public class EventbridgeConfiguration implements Cloneable, AwsCommonConfigurati description = "Whether to auto-create an SQS queue and wire it as an EventBridge rule target.") private boolean autoCreateQueue = true; @UriParam(label = "consumer", defaultValue = "true", - description = "Whether to delete the auto-created SQS queue and remove the EventBridge target on shutdown.") + description = "Whether to delete the auto-created SQS queue and remove the EventBridge target on shutdown. " + + "When false, the queue and its target are kept, so the queue collects the events while the " + + "consumer is stopped, and the queue name is derived from the event bus and the rule name, " + + "so the next start reuses the queue.") private boolean deleteQueueOnShutdown = true; @UriParam(label = "consumer", defaultValue = "10", description = "The maximum number of messages to receive per poll from SQS.") @@ -342,7 +345,9 @@ public class EventbridgeConfiguration implements Cloneable, AwsCommonConfigurati } /** - * Whether to delete the auto-created SQS queue and remove the EventBridge target on shutdown + * Whether to delete the auto-created SQS queue and remove the EventBridge target on shutdown. When false, the queue + * and its target are kept, so the queue collects the events while the consumer is stopped, and the queue name is + * derived from the event bus and the rule name, so the next start reuses the queue. */ public void setDeleteQueueOnShutdown(boolean deleteQueueOnShutdown) { this.deleteQueueOnShutdown = deleteQueueOnShutdown; diff --git a/components/camel-aws/camel-aws2-eventbridge/src/main/java/org/apache/camel/component/aws2/eventbridge/EventbridgeConsumer.java b/components/camel-aws/camel-aws2-eventbridge/src/main/java/org/apache/camel/component/aws2/eventbridge/EventbridgeConsumer.java index 62d50802db3c..63a6991cad69 100644 --- a/components/camel-aws/camel-aws2-eventbridge/src/main/java/org/apache/camel/component/aws2/eventbridge/EventbridgeConsumer.java +++ b/components/camel-aws/camel-aws2-eventbridge/src/main/java/org/apache/camel/component/aws2/eventbridge/EventbridgeConsumer.java @@ -66,12 +66,16 @@ import software.amazon.awssdk.services.sqs.model.SetQueueAttributesRequest; * * <p> * The consumer can auto-create an SQS queue and wire it to the EventBridge rule, or use a user-provided queue. On - * shutdown, auto-provisioned resources are cleaned up by default. A user-provided queue keeps its target on the rule. + * shutdown, auto-provisioned resources are cleaned up by default. A user-provided queue keeps its target on the rule, + * and so does an auto-created queue that is kept on shutdown (deleteQueueOnShutdown=false), which the next start + * reuses. * </p> */ public class EventbridgeConsumer extends ScheduledBatchPollingConsumer { private static final Logger LOG = LoggerFactory.getLogger(EventbridgeConsumer.class); + // "camel-eventbridge-" + rule name + "-" + 16 hex characters fits the 80 characters of a queue name + private static final int MAX_RULE_NAME_IN_QUEUE_NAME = 45; private SqsClient sqsClient; private String queueUrl; @@ -166,9 +170,12 @@ public class EventbridgeConsumer extends ScheduledBatchPollingConsumer { // missing rule then fails before a queue is created String ruleArn = getRuleArn(ruleName, config.getEventbusName()); - // Auto-create queue - String queueName - = "camel-eventbridge-" + ruleName + "-" + UUID.randomUUID().toString().substring(0, 8); + // Auto-create queue. A queue that is kept on shutdown has a name derived from the event bus and the rule, so + // the next start reuses it (CreateQueue returns the existing queue) instead of leaving it behind. A queue + // that is deleted on shutdown has a random part, so every consumer has its own queue. + String queueName = config.isDeleteQueueOnShutdown() + ? queueNamePrefix(ruleName) + UUID.randomUUID().toString().substring(0, 8) + : durableQueueName(config.getEventbusName(), ruleName); CreateQueueResponse createResponse = sqsClient.createQueue( CreateQueueRequest.builder() @@ -187,16 +194,17 @@ public class EventbridgeConsumer extends ScheduledBatchPollingConsumer { .attributes(Map.of(QueueAttributeName.POLICY, policy)) .build()); - LOG.info("Auto-created SQS queue {} for EventBridge rule {}", queueName, ruleName); + LOG.info("Auto-created SQS queue {} for EventBridge rule {}", queueUrl, ruleName); } else { throw new IllegalArgumentException( "Either queueUrl must be specified or autoCreateQueue must be enabled for EventBridge consumer"); } - // Add queue as EventBridge target. The target of a user-provided queue has an id derived from the queue, so - // every start and every consumer that shares the queue updates the same target (putTargets replaces a target - // with the same id): two targets for one queue would each put a copy of every event on the queue. - targetId = queueAutoCreated + // Add queue as EventBridge target. The target of a queue that outlives the consumer (a user-provided queue, + // or an auto-created queue with deleteQueueOnShutdown=false) has an id derived from the queue, so every start + // and every consumer that shares the queue updates the same target (putTargets replaces a target with the same + // id): two targets for one queue would each put a copy of every event on the queue. + targetId = isQueueDeletedOnShutdown() ? "camel-eb-" + UUID.randomUUID().toString().substring(0, 8) : targetIdForQueue(queueArn); Target target = Target.builder() @@ -224,9 +232,11 @@ public class EventbridgeConsumer extends ScheduledBatchPollingConsumer { EventbridgeConfiguration config = getEndpoint().getConfiguration(); try { - // Remove the target of an auto-created queue. The target of a user-provided queue is kept: other consumers - // can share the queue, and the queue keeps collecting the events of the rule while this consumer is stopped. - if (queueAutoCreated && ObjectHelper.isNotEmpty(targetId)) { + // Remove the target of a queue that is deleted on shutdown. The target of a queue that is kept (a + // user-provided queue, or an auto-created queue with deleteQueueOnShutdown=false) is kept too: other + // consumers can share the queue, and the queue keeps collecting the events of the rule while this consumer + // is stopped. + if (isQueueDeletedOnShutdown() && ObjectHelper.isNotEmpty(targetId)) { EventBridgeClient ebClient = getEndpoint().getEventbridgeClient(); ebClient.removeTargets( RemoveTargetsRequest.builder() @@ -242,7 +252,7 @@ public class EventbridgeConsumer extends ScheduledBatchPollingConsumer { try { // Delete auto-created queue - if (queueAutoCreated && config.isDeleteQueueOnShutdown() && ObjectHelper.isNotEmpty(queueUrl)) { + if (isQueueDeletedOnShutdown() && ObjectHelper.isNotEmpty(queueUrl)) { sqsClient.deleteQueue( DeleteQueueRequest.builder() .queueUrl(queueUrl) @@ -279,19 +289,46 @@ public class EventbridgeConsumer extends ScheduledBatchPollingConsumer { } } + private boolean isQueueDeletedOnShutdown() { + return queueAutoCreated && getEndpoint().getConfiguration().isDeleteQueueOnShutdown(); + } + + private static String queueNamePrefix(String ruleName) { + // a queue name has at most 80 characters of [-_A-Za-z0-9], a rule name can also have dots + String name = ruleName.replace('.', '-'); + if (name.length() > MAX_RULE_NAME_IN_QUEUE_NAME) { + name = name.substring(0, MAX_RULE_NAME_IN_QUEUE_NAME); + } + return "camel-eventbridge-" + name + "-"; + } + /** - * The EventBridge target id for a user-provided queue: the same for every start and every consumer of the queue. + * The name of an auto-created queue that is kept on shutdown: the same for every start of a consumer of the rule, + * so the queue is reused. The hash tells apart the rules with the same name on other event buses, and the rules + * whose names are cut. */ - static String targetIdForQueue(String queueArn) { + static String durableQueueName(String eventBusName, String ruleName) { + return queueNamePrefix(ruleName) + sha256Hex(eventBusName + "/" + ruleName, 8); + } + + private static String sha256Hex(String value, int bytes) { try { - byte[] digest = MessageDigest.getInstance("SHA-256").digest(queueArn.getBytes(StandardCharsets.UTF_8)); - // a target id has at most 64 characters of [.-_A-Za-z0-9] - return "camel-eb-" + HexFormat.of().formatHex(digest, 0, 16); + byte[] digest = MessageDigest.getInstance("SHA-256").digest(value.getBytes(StandardCharsets.UTF_8)); + return HexFormat.of().formatHex(digest, 0, bytes); } catch (NoSuchAlgorithmException e) { throw new IllegalStateException("SHA-256 is not available", e); } } + /** + * The EventBridge target id for a queue that outlives the consumer: the same for every start and every consumer of + * the queue. + */ + static String targetIdForQueue(String queueArn) { + // a target id has at most 64 characters of [.-_A-Za-z0-9] + return "camel-eb-" + sha256Hex(queueArn, 16); + } + private String getQueueArn(String queueUrl) { GetQueueAttributesResponse response = sqsClient.getQueueAttributes( GetQueueAttributesRequest.builder() diff --git a/components/camel-aws/camel-aws2-eventbridge/src/test/java/org/apache/camel/component/aws2/eventbridge/EventbridgeConsumerTest.java b/components/camel-aws/camel-aws2-eventbridge/src/test/java/org/apache/camel/component/aws2/eventbridge/EventbridgeConsumerTest.java index 71687e3e7ea0..8a968678ba80 100644 --- a/components/camel-aws/camel-aws2-eventbridge/src/test/java/org/apache/camel/component/aws2/eventbridge/EventbridgeConsumerTest.java +++ b/components/camel-aws/camel-aws2-eventbridge/src/test/java/org/apache/camel/component/aws2/eventbridge/EventbridgeConsumerTest.java @@ -419,20 +419,82 @@ public class EventbridgeConsumerTest extends CamelTestSupport { } @Test - public void testDeleteQueueOnShutdownFalseKeepsQueue() throws Exception { + public void testDeleteQueueOnShutdownFalseKeepsQueueAndTarget() throws Exception { stubAutoCreateQueue(); stubPutTargetsSuccess(); - when(eventBridgeClient.removeTargets(any(RemoveTargetsRequest.class))) - .thenReturn(RemoveTargetsResponse.builder().build()); EventbridgeConsumer consumer = createConsumer("test-rule", null, true, false); consumer.start(); consumer.stop(); - // Target is still removed - verify(eventBridgeClient).removeTargets(any(RemoveTargetsRequest.class)); - // But queue is NOT deleted + // the queue and its target stay, so the queue keeps collecting the events of the rule until the next start + verify(eventBridgeClient, never()).removeTargets(any(RemoveTargetsRequest.class)); verify(sqsClient, never()).deleteQueue(any(DeleteQueueRequest.class)); + + ArgumentCaptor<PutTargetsRequest> captor = ArgumentCaptor.forClass(PutTargetsRequest.class); + verify(eventBridgeClient).putTargets(captor.capture()); + assertEquals(EventbridgeConsumer.targetIdForQueue(TEST_QUEUE_ARN), captor.getValue().targets().get(0).id()); + } + + @Test + public void testDeleteQueueOnShutdownFalseReusesQueueOnRestart() throws Exception { + stubAutoCreateQueue(); + stubPutTargetsSuccess(); + + EventbridgeConsumer consumer = createConsumer("test-rule", null, true, false); + consumer.start(); + consumer.stop(); + consumer.start(); + consumer.stop(); + + // both starts ask for the same queue name, so CreateQueue returns the queue of the first start + ArgumentCaptor<CreateQueueRequest> create = ArgumentCaptor.forClass(CreateQueueRequest.class); + verify(sqsClient, times(2)).createQueue(create.capture()); + String name = EventbridgeConsumer.durableQueueName("default", "test-rule"); + assertEquals(List.of(name, name), create.getAllValues().stream().map(CreateQueueRequest::queueName).toList()); + + // and update the same target + ArgumentCaptor<PutTargetsRequest> put = ArgumentCaptor.forClass(PutTargetsRequest.class); + verify(eventBridgeClient, times(2)).putTargets(put.capture()); + assertEquals(put.getAllValues().get(0).targets().get(0).id(), put.getAllValues().get(1).targets().get(0).id()); + } + + @Test + public void testDeleteQueueOnShutdownTrueCreatesOwnQueuePerStart() throws Exception { + stubAutoCreateQueue(); + stubPutTargetsSuccess(); + when(eventBridgeClient.removeTargets(any(RemoveTargetsRequest.class))) + .thenReturn(RemoveTargetsResponse.builder().build()); + + EventbridgeConsumer consumer = createConsumer("test-rule", null, true, true); + consumer.start(); + consumer.stop(); + consumer.start(); + consumer.stop(); + + ArgumentCaptor<CreateQueueRequest> create = ArgumentCaptor.forClass(CreateQueueRequest.class); + verify(sqsClient, times(2)).createQueue(create.capture()); + String first = create.getAllValues().get(0).queueName(); + String second = create.getAllValues().get(1).queueName(); + assertTrue(first.startsWith("camel-eventbridge-test-rule-"), first); + assertNotEquals(first, second); + } + + @Test + public void testDurableQueueName() { + String name = EventbridgeConsumer.durableQueueName("default", "test-rule"); + assertTrue(name.matches("camel-eventbridge-test-rule-[0-9a-f]{16}"), name); + assertEquals(name, EventbridgeConsumer.durableQueueName("default", "test-rule")); + // a rule with the same name on another event bus gets another queue + assertNotEquals(name, EventbridgeConsumer.durableQueueName("orders", "test-rule")); + + // a queue name has at most 80 characters of [-_A-Za-z0-9]; a rule name has up to 64 characters, dots too + String longRule = "a.b".repeat(21) + "x"; + String longName = EventbridgeConsumer.durableQueueName("default", longRule); + assertTrue(longName.length() <= 80, longName); + assertTrue(longName.matches("[-_A-Za-z0-9]+"), longName); + // rules that differ after the cut get other queues + assertNotEquals(longName, EventbridgeConsumer.durableQueueName("default", "a.b".repeat(21) + "y")); } @Test diff --git a/components/camel-aws/camel-aws2-eventbridge/src/test/java/org/apache/camel/component/aws2/eventbridge/localstack/EventbridgeConsumerKeepQueueIT.java b/components/camel-aws/camel-aws2-eventbridge/src/test/java/org/apache/camel/component/aws2/eventbridge/localstack/EventbridgeConsumerKeepQueueIT.java new file mode 100644 index 000000000000..2aa6f3685046 --- /dev/null +++ b/components/camel-aws/camel-aws2-eventbridge/src/test/java/org/apache/camel/component/aws2/eventbridge/localstack/EventbridgeConsumerKeepQueueIT.java @@ -0,0 +1,113 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.aws2.eventbridge.localstack; + +import java.util.List; +import java.util.Properties; + +import org.apache.camel.EndpointInject; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.test.infra.aws.common.AWSConfigs; +import org.apache.camel.test.infra.aws2.clients.AWSSDKClientUtils; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.DisabledOnOs; +import software.amazon.awssdk.services.eventbridge.EventBridgeClient; +import software.amazon.awssdk.services.eventbridge.model.PutEventsRequestEntry; +import software.amazon.awssdk.services.eventbridge.model.PutEventsResponse; +import software.amazon.awssdk.services.sqs.SqsClient; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * With deleteQueueOnShutdown=false, the queue that the consumer creates and its target stay on shutdown, so the queue + * collects the events while the consumer is stopped, and the next start reuses it instead of creating another queue. + */ +@DisabledOnOs(architectures = { "s390x", "ppc64le" }, + disabledReason = "Needs EventBridge delivering to SQS, as Floci does; these architectures use LocalStack") +public class EventbridgeConsumerKeepQueueIT extends Aws2EventbridgeBase { + + private static final String RULE_NAME = "camel-keep-queue-rule"; + + @EndpointInject("mock:result") + private MockEndpoint result; + + @Override + protected RouteBuilder createRouteBuilder() { + // the rule must exist before the consumer starts + try (EventBridgeClient ebClient = AWSSDKClientUtils.newEventBridgeClient()) { + ebClient.putRule(b -> b.name(RULE_NAME) + .eventBusName("default") + .eventPattern("{\"source\":[\"camel.keep\"]}")); + } + + Properties props = service.getConnectionProperties(); + String uri = "aws2-eventbridge://default?ruleName=" + RULE_NAME + + "&deleteQueueOnShutdown=false" + + "®ion=" + props.getProperty(AWSConfigs.REGION) + + "&accessKey=RAW(" + props.getProperty(AWSConfigs.ACCESS_KEY) + ")" + + "&secretKey=RAW(" + props.getProperty(AWSConfigs.SECRET_KEY) + ")" + + "&overrideEndpoint=true&uriEndpointOverride=" + props.getProperty(AWSConfigs.PROTOCOL) + "://" + + props.getProperty(AWSConfigs.AMAZON_AWS_HOST) + + "&waitTimeSeconds=1&delay=500"; + + return new RouteBuilder() { + @Override + public void configure() { + from(uri).routeId("keep").to("mock:result"); + } + }; + } + + @Test + public void restartReusesTheQueueAndGetsTheEventsSentWhileStopped() throws Exception { + List<String> queuesAtFirstStart = queuesOfTheRule(); + assertEquals(1, queuesAtFirstStart.size(), "queues of the consumer: " + queuesAtFirstStart); + + context.getRouteController().stopRoute("keep"); + + result.expectedMessageCount(1); + result.message(0).body().contains("Sent while stopped"); + putEvent("Sent while stopped"); + + context.getRouteController().startRoute("keep"); + + MockEndpoint.assertIsSatisfied(context); + assertEquals(queuesAtFirstStart, queuesOfTheRule()); + try (EventBridgeClient ebClient = AWSSDKClientUtils.newEventBridgeClient()) { + assertEquals(1, ebClient.listTargetsByRule(b -> b.rule(RULE_NAME).eventBusName("default")).targets().size()); + } + } + + private static List<String> queuesOfTheRule() { + try (SqsClient sqsClient = AWSSDKClientUtils.newSQSClient()) { + return sqsClient.listQueues(b -> b.queueNamePrefix("camel-eventbridge-" + RULE_NAME)).queueUrls(); + } + } + + private static void putEvent(String message) { + try (EventBridgeClient ebClient = AWSSDKClientUtils.newEventBridgeClient()) { + PutEventsResponse response = ebClient.putEvents(b -> b.entries(PutEventsRequestEntry.builder() + .eventBusName("default") + .source("camel.keep") + .detailType("CamelTestEvent") + .detail("{\"message\":\"" + message + "\"}") + .build())); + assertEquals(0, response.failedEntryCount()); + } + } +} diff --git a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc index dd695d576b3d..3f21cea9cc4e 100644 --- a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc +++ b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc @@ -2572,6 +2572,15 @@ To get the rule ARN, the consumer calls `DescribeRule` before it creates the que consumer now also needs `events:DescribeRule` on the rule. A rule that does not exist now fails the consumer start before a queue is created. A queue given with `queueUrl` keeps its own policy, as before. +=== camel-aws2-eventbridge - with deleteQueueOnShutdown=false the consumer reuses its queue + +With `deleteQueueOnShutdown=false`, the queue the consumer creates now has a name without a random part +(`camel-eventbridge-<rule name>-<hash of the event bus and the rule name>`), so the next start reuses it. Before, every +start created another queue and left the previous one behind. The queue now also keeps its target on the rule on +shutdown (as the option description already said), so it collects the events while the consumer is stopped, and the +consumers of the rule with `deleteQueueOnShutdown=false` share one queue. The queues that earlier versions left +behind (with a random part in the name) are not used any more and can be deleted. + === camel-azure-storage-blob - copyBlob validates the source account and container name `copyBlob` now rejects invalid source account or container names (headers `CamelAzureStorageBlobSourceBlobAccountName` diff --git a/dsl/camel-componentdsl/src/generated/java/org/apache/camel/builder/component/dsl/Aws2EventbridgeComponentBuilderFactory.java b/dsl/camel-componentdsl/src/generated/java/org/apache/camel/builder/component/dsl/Aws2EventbridgeComponentBuilderFactory.java index 9c239e5f67d0..c4b573b411b5 100644 --- a/dsl/camel-componentdsl/src/generated/java/org/apache/camel/builder/component/dsl/Aws2EventbridgeComponentBuilderFactory.java +++ b/dsl/camel-componentdsl/src/generated/java/org/apache/camel/builder/component/dsl/Aws2EventbridgeComponentBuilderFactory.java @@ -222,7 +222,10 @@ public interface Aws2EventbridgeComponentBuilderFactory { /** * Whether to delete the auto-created SQS queue and remove the - * EventBridge target on shutdown. + * EventBridge target on shutdown. When false, the queue and its target + * are kept, so the queue collects the events while the consumer is + * stopped, and the queue name is derived from the event bus and the + * rule name, so the next start reuses the queue. * * The option is a: <code>boolean</code> type. * diff --git a/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/EventbridgeEndpointBuilderFactory.java b/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/EventbridgeEndpointBuilderFactory.java index a26a2fb3297e..3e81053c54eb 100644 --- a/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/EventbridgeEndpointBuilderFactory.java +++ b/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/EventbridgeEndpointBuilderFactory.java @@ -223,7 +223,10 @@ public interface EventbridgeEndpointBuilderFactory { } /** * Whether to delete the auto-created SQS queue and remove the - * EventBridge target on shutdown. + * EventBridge target on shutdown. When false, the queue and its target + * are kept, so the queue collects the events while the consumer is + * stopped, and the queue name is derived from the event bus and the + * rule name, so the next start reuses the queue. * * The option is a: <code>boolean</code> type. * @@ -239,7 +242,10 @@ public interface EventbridgeEndpointBuilderFactory { } /** * Whether to delete the auto-created SQS queue and remove the - * EventBridge target on shutdown. + * EventBridge target on shutdown. When false, the queue and its target + * are kept, so the queue collects the events while the consumer is + * stopped, and the queue name is derived from the event bus and the + * rule name, so the next start reuses the queue. * * The option will be converted to a <code>boolean</code> type. *
