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 54692d054a4e CAMEL-27286: camel-consul - a producer with a key acts on
its key, not on the key a consumer received (#30533)
54692d054a4e is described below
commit 54692d054a4e8087e53f397900b3e84f38963b13
Author: allthingssecurity <[email protected]>
AuthorDate: Sat Nov 21 14:37:59 2026 +0530
CAMEL-27286: camel-consul - a producer with a key acts on its key, not on
the key a consumer received (#30533)
The kv and event consumers set the CamelConsulKey header to the key (or
event name) they received, and the producers took the key from that
header before their key option. A route from one key to another wrote,
deleted, locked or fired the key it came from: a PUT or FIRE started the
watch again and looped for ever, and the key of the producer was never
written.
The consumers now also set the exchange property CamelConsulReceivedKey
to the received key. A producer with a key uses it while the key header
still holds the received key; a key header set by the route is still
used, and a producer without key still acts on the received key, as
camel-azure-eventhubs does with its received partition properties.
Component page and 4.24 upgrade guide updated.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
.../apache/camel/catalog/components/consul.json | 4 +-
.../camel/catalog/docs/consul-component.adoc | 8 +
.../org/apache/camel/component/consul/consul.json | 4 +-
.../src/main/docs/consul-component.adoc | 8 +
.../component/consul/ConsulConfiguration.java | 4 +-
.../camel/component/consul/ConsulConstants.java | 3 +
.../consul/endpoint/AbstractConsulProducer.java | 27 +++
.../consul/endpoint/ConsulEventConsumer.java | 1 +
.../consul/endpoint/ConsulEventProducer.java | 4 +-
.../consul/endpoint/ConsulKeyValueConsumer.java | 1 +
.../consul/endpoint/ConsulKeyValueProducer.java | 22 +--
.../consul/ConsulProducerReceivedKeyTest.java | 208 +++++++++++++++++++++
.../ROOT/pages/camel-4x-upgrade-guide-4_24.adoc | 18 ++
.../dsl/ConsulComponentBuilderFactory.java | 6 +-
.../endpoint/dsl/ConsulEndpointBuilderFactory.java | 18 +-
15 files changed, 314 insertions(+), 22 deletions(-)
diff --git
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/consul.json
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/consul.json
index fddd611901f6..54fd74631164 100644
---
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/consul.json
+++
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/consul.json
@@ -25,7 +25,7 @@
},
"componentProperties": {
"connectTimeout": { "index": 0, "kind": "property", "displayName":
"Connect Timeout", "group": "common", "label": "", "required": false, "type":
"duration", "javaType": "java.time.Duration", "deprecated": false, "autowired":
false, "secret": false, "configurationClass":
"org.apache.camel.component.consul.ConsulConfiguration", "configurationField":
"configuration", "description": "Connect timeout" },
- "key": { "index": 1, "kind": "property", "displayName": "Key", "group":
"common", "label": "", "required": false, "type": "string", "javaType":
"java.lang.String", "deprecated": false, "autowired": false, "secret": false,
"configurationClass": "org.apache.camel.component.consul.ConsulConfiguration",
"configurationField": "configuration", "description": "The default key. Can be
overridden by CamelConsulKey" },
+ "key": { "index": 1, "kind": "property", "displayName": "Key", "group":
"common", "label": "", "required": false, "type": "string", "javaType":
"java.lang.String", "deprecated": false, "autowired": false, "secret": false,
"configurationClass": "org.apache.camel.component.consul.ConsulConfiguration",
"configurationField": "configuration", "description": "The key (kv) or event
name (event) that the consumer watches or that the producer acts on; the other
APIs do not use it. In a produc [...]
"pingInstance": { "index": 2, "kind": "property", "displayName": "Ping
Instance", "group": "common", "label": "", "required": false, "type":
"boolean", "javaType": "boolean", "deprecated": false, "autowired": false,
"secret": false, "defaultValue": true, "configurationClass":
"org.apache.camel.component.consul.ConsulConfiguration", "configurationField":
"configuration", "description": "Configure if the AgentClient should attempt a
ping before returning the Consul instance" },
"readTimeout": { "index": 3, "kind": "property", "displayName": "Read
Timeout", "group": "common", "label": "", "required": false, "type":
"duration", "javaType": "java.time.Duration", "deprecated": false, "autowired":
false, "secret": false, "configurationClass":
"org.apache.camel.component.consul.ConsulConfiguration", "configurationField":
"configuration", "description": "Read timeout" },
"tags": { "index": 4, "kind": "property", "displayName": "Tags", "group":
"common", "label": "", "required": false, "type": "string", "javaType":
"java.lang.String", "deprecated": false, "autowired": false, "secret": false,
"configurationClass": "org.apache.camel.component.consul.ConsulConfiguration",
"configurationField": "configuration", "description": "Set tags. You can
separate multiple tags by comma." },
@@ -92,7 +92,7 @@
"properties": {
"apiEndpoint": { "index": 0, "kind": "path", "displayName": "Api
Endpoint", "group": "common", "label": "", "required": true, "type": "string",
"javaType": "java.lang.String", "deprecated": false, "autowired": false,
"secret": false, "description": "The API endpoint" },
"connectTimeout": { "index": 1, "kind": "parameter", "displayName":
"Connect Timeout", "group": "common", "label": "", "required": false, "type":
"duration", "javaType": "java.time.Duration", "deprecated": false, "autowired":
false, "secret": false, "configurationClass":
"org.apache.camel.component.consul.ConsulConfiguration", "configurationField":
"configuration", "description": "Connect timeout" },
- "key": { "index": 2, "kind": "parameter", "displayName": "Key", "group":
"common", "label": "", "required": false, "type": "string", "javaType":
"java.lang.String", "deprecated": false, "autowired": false, "secret": false,
"configurationClass": "org.apache.camel.component.consul.ConsulConfiguration",
"configurationField": "configuration", "description": "The default key. Can be
overridden by CamelConsulKey" },
+ "key": { "index": 2, "kind": "parameter", "displayName": "Key", "group":
"common", "label": "", "required": false, "type": "string", "javaType":
"java.lang.String", "deprecated": false, "autowired": false, "secret": false,
"configurationClass": "org.apache.camel.component.consul.ConsulConfiguration",
"configurationField": "configuration", "description": "The key (kv) or event
name (event) that the consumer watches or that the producer acts on; the other
APIs do not use it. In a produ [...]
"pingInstance": { "index": 3, "kind": "parameter", "displayName": "Ping
Instance", "group": "common", "label": "", "required": false, "type":
"boolean", "javaType": "boolean", "deprecated": false, "autowired": false,
"secret": false, "defaultValue": true, "configurationClass":
"org.apache.camel.component.consul.ConsulConfiguration", "configurationField":
"configuration", "description": "Configure if the AgentClient should attempt a
ping before returning the Consul instance" },
"readTimeout": { "index": 4, "kind": "parameter", "displayName": "Read
Timeout", "group": "common", "label": "", "required": false, "type":
"duration", "javaType": "java.time.Duration", "deprecated": false, "autowired":
false, "secret": false, "configurationClass":
"org.apache.camel.component.consul.ConsulConfiguration", "configurationField":
"configuration", "description": "Read timeout" },
"tags": { "index": 5, "kind": "parameter", "displayName": "Tags", "group":
"common", "label": "", "required": false, "type": "string", "javaType":
"java.lang.String", "deprecated": false, "autowired": false, "secret": false,
"configurationClass": "org.apache.camel.component.consul.ConsulConfiguration",
"configurationField": "configuration", "description": "Set tags. You can
separate multiple tags by comma." },
diff --git
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/consul-component.adoc
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/consul-component.adoc
index 1b8a97222470..fd0362f89fd6 100644
---
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/consul-component.adoc
+++
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/consul-component.adoc
@@ -154,6 +154,14 @@ The body is the value base64 encoded, as Consul stores it,
unless `valueAsString
message: "${header.CamelConsulKey} is now ${body}"
----
+The `kv` and `event` consumers set the `CamelConsulKey` header to the key (or
event name) they received. While the
+header still holds that key, a producer that has a `key` of its own uses its
`key`, so a route from one key to another
+(`from("consul:kv?key=a").to("consul:kv?key=b&action=PUT")`) writes `b`, not
`a`. A key header set by the route is used,
+and a producer without `key` acts on the received key, for example to delete
each key of a recursive watch once it is
+processed. To act on the received key with a producer that has a `key`, remove
the `CamelConsulReceivedKey` exchange
+property (which the consumers set to the received key) before the producer,
such as with
+`.removeProperty(ConsulConstants.CONSUL_RECEIVED_KEY)`.
+
The client connects to the Consul agent at `http://localhost:8500`, unless you
set the `url` option,
for example with `camel.component.consul.url` in `application.properties`.
diff --git
a/components/camel-consul/src/generated/resources/META-INF/org/apache/camel/component/consul/consul.json
b/components/camel-consul/src/generated/resources/META-INF/org/apache/camel/component/consul/consul.json
index fddd611901f6..54fd74631164 100644
---
a/components/camel-consul/src/generated/resources/META-INF/org/apache/camel/component/consul/consul.json
+++
b/components/camel-consul/src/generated/resources/META-INF/org/apache/camel/component/consul/consul.json
@@ -25,7 +25,7 @@
},
"componentProperties": {
"connectTimeout": { "index": 0, "kind": "property", "displayName":
"Connect Timeout", "group": "common", "label": "", "required": false, "type":
"duration", "javaType": "java.time.Duration", "deprecated": false, "autowired":
false, "secret": false, "configurationClass":
"org.apache.camel.component.consul.ConsulConfiguration", "configurationField":
"configuration", "description": "Connect timeout" },
- "key": { "index": 1, "kind": "property", "displayName": "Key", "group":
"common", "label": "", "required": false, "type": "string", "javaType":
"java.lang.String", "deprecated": false, "autowired": false, "secret": false,
"configurationClass": "org.apache.camel.component.consul.ConsulConfiguration",
"configurationField": "configuration", "description": "The default key. Can be
overridden by CamelConsulKey" },
+ "key": { "index": 1, "kind": "property", "displayName": "Key", "group":
"common", "label": "", "required": false, "type": "string", "javaType":
"java.lang.String", "deprecated": false, "autowired": false, "secret": false,
"configurationClass": "org.apache.camel.component.consul.ConsulConfiguration",
"configurationField": "configuration", "description": "The key (kv) or event
name (event) that the consumer watches or that the producer acts on; the other
APIs do not use it. In a produc [...]
"pingInstance": { "index": 2, "kind": "property", "displayName": "Ping
Instance", "group": "common", "label": "", "required": false, "type":
"boolean", "javaType": "boolean", "deprecated": false, "autowired": false,
"secret": false, "defaultValue": true, "configurationClass":
"org.apache.camel.component.consul.ConsulConfiguration", "configurationField":
"configuration", "description": "Configure if the AgentClient should attempt a
ping before returning the Consul instance" },
"readTimeout": { "index": 3, "kind": "property", "displayName": "Read
Timeout", "group": "common", "label": "", "required": false, "type":
"duration", "javaType": "java.time.Duration", "deprecated": false, "autowired":
false, "secret": false, "configurationClass":
"org.apache.camel.component.consul.ConsulConfiguration", "configurationField":
"configuration", "description": "Read timeout" },
"tags": { "index": 4, "kind": "property", "displayName": "Tags", "group":
"common", "label": "", "required": false, "type": "string", "javaType":
"java.lang.String", "deprecated": false, "autowired": false, "secret": false,
"configurationClass": "org.apache.camel.component.consul.ConsulConfiguration",
"configurationField": "configuration", "description": "Set tags. You can
separate multiple tags by comma." },
@@ -92,7 +92,7 @@
"properties": {
"apiEndpoint": { "index": 0, "kind": "path", "displayName": "Api
Endpoint", "group": "common", "label": "", "required": true, "type": "string",
"javaType": "java.lang.String", "deprecated": false, "autowired": false,
"secret": false, "description": "The API endpoint" },
"connectTimeout": { "index": 1, "kind": "parameter", "displayName":
"Connect Timeout", "group": "common", "label": "", "required": false, "type":
"duration", "javaType": "java.time.Duration", "deprecated": false, "autowired":
false, "secret": false, "configurationClass":
"org.apache.camel.component.consul.ConsulConfiguration", "configurationField":
"configuration", "description": "Connect timeout" },
- "key": { "index": 2, "kind": "parameter", "displayName": "Key", "group":
"common", "label": "", "required": false, "type": "string", "javaType":
"java.lang.String", "deprecated": false, "autowired": false, "secret": false,
"configurationClass": "org.apache.camel.component.consul.ConsulConfiguration",
"configurationField": "configuration", "description": "The default key. Can be
overridden by CamelConsulKey" },
+ "key": { "index": 2, "kind": "parameter", "displayName": "Key", "group":
"common", "label": "", "required": false, "type": "string", "javaType":
"java.lang.String", "deprecated": false, "autowired": false, "secret": false,
"configurationClass": "org.apache.camel.component.consul.ConsulConfiguration",
"configurationField": "configuration", "description": "The key (kv) or event
name (event) that the consumer watches or that the producer acts on; the other
APIs do not use it. In a produ [...]
"pingInstance": { "index": 3, "kind": "parameter", "displayName": "Ping
Instance", "group": "common", "label": "", "required": false, "type":
"boolean", "javaType": "boolean", "deprecated": false, "autowired": false,
"secret": false, "defaultValue": true, "configurationClass":
"org.apache.camel.component.consul.ConsulConfiguration", "configurationField":
"configuration", "description": "Configure if the AgentClient should attempt a
ping before returning the Consul instance" },
"readTimeout": { "index": 4, "kind": "parameter", "displayName": "Read
Timeout", "group": "common", "label": "", "required": false, "type":
"duration", "javaType": "java.time.Duration", "deprecated": false, "autowired":
false, "secret": false, "configurationClass":
"org.apache.camel.component.consul.ConsulConfiguration", "configurationField":
"configuration", "description": "Read timeout" },
"tags": { "index": 5, "kind": "parameter", "displayName": "Tags", "group":
"common", "label": "", "required": false, "type": "string", "javaType":
"java.lang.String", "deprecated": false, "autowired": false, "secret": false,
"configurationClass": "org.apache.camel.component.consul.ConsulConfiguration",
"configurationField": "configuration", "description": "Set tags. You can
separate multiple tags by comma." },
diff --git a/components/camel-consul/src/main/docs/consul-component.adoc
b/components/camel-consul/src/main/docs/consul-component.adoc
index 1b8a97222470..fd0362f89fd6 100644
--- a/components/camel-consul/src/main/docs/consul-component.adoc
+++ b/components/camel-consul/src/main/docs/consul-component.adoc
@@ -154,6 +154,14 @@ The body is the value base64 encoded, as Consul stores it,
unless `valueAsString
message: "${header.CamelConsulKey} is now ${body}"
----
+The `kv` and `event` consumers set the `CamelConsulKey` header to the key (or
event name) they received. While the
+header still holds that key, a producer that has a `key` of its own uses its
`key`, so a route from one key to another
+(`from("consul:kv?key=a").to("consul:kv?key=b&action=PUT")`) writes `b`, not
`a`. A key header set by the route is used,
+and a producer without `key` acts on the received key, for example to delete
each key of a recursive watch once it is
+processed. To act on the received key with a producer that has a `key`, remove
the `CamelConsulReceivedKey` exchange
+property (which the consumers set to the received key) before the producer,
such as with
+`.removeProperty(ConsulConstants.CONSUL_RECEIVED_KEY)`.
+
The client connects to the Consul agent at `http://localhost:8500`, unless you
set the `url` option,
for example with `camel.component.consul.url` in `application.properties`.
diff --git
a/components/camel-consul/src/main/java/org/apache/camel/component/consul/ConsulConfiguration.java
b/components/camel-consul/src/main/java/org/apache/camel/component/consul/ConsulConfiguration.java
index b4ee78ac8714..fb33ebbcb382 100644
---
a/components/camel-consul/src/main/java/org/apache/camel/component/consul/ConsulConfiguration.java
+++
b/components/camel-consul/src/main/java/org/apache/camel/component/consul/ConsulConfiguration.java
@@ -74,7 +74,9 @@ public class ConsulConfiguration extends
ConsulClientConfiguration {
}
/**
- * The default key. Can be overridden by CamelConsulKey
+ * The key (kv) or event name (event) that the consumer watches or that
the producer acts on; the other APIs do not
+ * use it. In a producer, a CamelConsulKey header overrides it, unless the
header still holds the key that a consul
+ * consumer received (the CamelConsulReceivedKey exchange property).
*/
public void setKey(String key) {
this.key = key;
diff --git
a/components/camel-consul/src/main/java/org/apache/camel/component/consul/ConsulConstants.java
b/components/camel-consul/src/main/java/org/apache/camel/component/consul/ConsulConstants.java
index 06beb8b9bcbb..6258df09e258 100644
---
a/components/camel-consul/src/main/java/org/apache/camel/component/consul/ConsulConstants.java
+++
b/components/camel-consul/src/main/java/org/apache/camel/component/consul/ConsulConstants.java
@@ -30,6 +30,9 @@ public interface ConsulConstants {
String CONSUL_ACTION = "CamelConsulAction";
@Metadata(description = "The Key on which the action should applied",
javaType = "String")
String CONSUL_KEY = "CamelConsulKey";
+ // exchange property the key/value and event consumers set to the key they
received, so that a producer can tell the
+ // key header the consumer sets apart from a key chosen by the route
+ String CONSUL_RECEIVED_KEY = "CamelConsulReceivedKey";
@Metadata(label = "consumer", description = "The event id", javaType =
"String")
String CONSUL_EVENT_ID = "CamelConsulEventId";
@Metadata(label = "consumer", description = "The event name", javaType =
"String")
diff --git
a/components/camel-consul/src/main/java/org/apache/camel/component/consul/endpoint/AbstractConsulProducer.java
b/components/camel-consul/src/main/java/org/apache/camel/component/consul/endpoint/AbstractConsulProducer.java
index f33f632ebac7..d79ca2c3d277 100644
---
a/components/camel-consul/src/main/java/org/apache/camel/component/consul/endpoint/AbstractConsulProducer.java
+++
b/components/camel-consul/src/main/java/org/apache/camel/component/consul/endpoint/AbstractConsulProducer.java
@@ -80,6 +80,33 @@ abstract class AbstractConsulProducer<C> extends
HeaderSelectorProducer {
return value;
}
+ /**
+ * The key of the action: the CamelConsulKey header, else the key of this
endpoint. A header that still holds the
+ * key a consul consumer received (a key/value that changed, an event)
describes that message: when this endpoint
+ * has a key of its own, that key is used, so that a route from one key to
another does not act on the key it came
+ * from.
+ */
+ protected String getKey(Message message) {
+ String key = message.getHeader(ConsulConstants.CONSUL_KEY,
String.class);
+ String endpointKey = configuration.getKey();
+ if (key == null) {
+ return endpointKey;
+ }
+ if (endpointKey != null
+ &&
key.equals(message.getExchange().getProperty(ConsulConstants.CONSUL_RECEIVED_KEY,
String.class))) {
+ return endpointKey;
+ }
+ return key;
+ }
+
+ protected String getMandatoryKey(Message message) throws
NoSuchHeaderException {
+ String key = getKey(message);
+ if (key == null) {
+ throw new NoSuchHeaderException(message.getExchange(),
ConsulConstants.CONSUL_KEY, String.class);
+ }
+ return key;
+ }
+
protected QueryOptions buildQueryOptions(Message message,
ConsulConfiguration conf) {
ImmutableQueryOptions.Builder builder =
ImmutableQueryOptions.builder();
diff --git
a/components/camel-consul/src/main/java/org/apache/camel/component/consul/endpoint/ConsulEventConsumer.java
b/components/camel-consul/src/main/java/org/apache/camel/component/consul/endpoint/ConsulEventConsumer.java
index 7f6a857d013d..3ae01ebb574c 100644
---
a/components/camel-consul/src/main/java/org/apache/camel/component/consul/endpoint/ConsulEventConsumer.java
+++
b/components/camel-consul/src/main/java/org/apache/camel/component/consul/endpoint/ConsulEventConsumer.java
@@ -117,6 +117,7 @@ public final class ConsulEventConsumer extends
AbstractConsulConsumer<EventClien
final Message message = exchange.getIn();
message.setHeader(ConsulConstants.CONSUL_KEY, key);
+ exchange.setProperty(ConsulConstants.CONSUL_RECEIVED_KEY, key);
message.setHeader(ConsulConstants.CONSUL_RESULT, true);
message.setHeader(ConsulConstants.CONSUL_EVENT_ID,
event.getId());
message.setHeader(ConsulConstants.CONSUL_EVENT_NAME,
event.getName());
diff --git
a/components/camel-consul/src/main/java/org/apache/camel/component/consul/endpoint/ConsulEventProducer.java
b/components/camel-consul/src/main/java/org/apache/camel/component/consul/endpoint/ConsulEventProducer.java
index 8bfa59e14b3c..b21d52e6a630 100644
---
a/components/camel-consul/src/main/java/org/apache/camel/component/consul/endpoint/ConsulEventProducer.java
+++
b/components/camel-consul/src/main/java/org/apache/camel/component/consul/endpoint/ConsulEventProducer.java
@@ -34,7 +34,7 @@ public final class ConsulEventProducer extends
AbstractConsulProducer<EventClien
@InvokeOnHeader(ConsulEventActions.FIRE)
protected void fire(Message message) throws Exception {
- String name = getMandatoryHeader(message, ConsulConstants.CONSUL_KEY,
getConfiguration().getKey(), String.class);
+ String name = getMandatoryKey(message);
EventOptions options =
message.getHeader(ConsulConstants.CONSUL_OPTIONS, EventOptions.BLANK,
EventOptions.class);
String payload = message.getBody(String.class);
@@ -49,7 +49,7 @@ public final class ConsulEventProducer extends
AbstractConsulProducer<EventClien
@InvokeOnHeader(ConsulEventActions.LIST)
protected void list(Message message) throws Exception {
setBodyAndResult(message,
-
getClient().listEvents(message.getHeader(ConsulConstants.CONSUL_KEY,
getConfiguration().getKey(), String.class),
+ getClient().listEvents(getKey(message),
message.getHeader(ConsulConstants.CONSUL_OPTIONS,
QueryOptions.BLANK, QueryOptions.class)));
}
}
diff --git
a/components/camel-consul/src/main/java/org/apache/camel/component/consul/endpoint/ConsulKeyValueConsumer.java
b/components/camel-consul/src/main/java/org/apache/camel/component/consul/endpoint/ConsulKeyValueConsumer.java
index 8c9e141c28c5..f6858ae72f64 100644
---
a/components/camel-consul/src/main/java/org/apache/camel/component/consul/endpoint/ConsulKeyValueConsumer.java
+++
b/components/camel-consul/src/main/java/org/apache/camel/component/consul/endpoint/ConsulKeyValueConsumer.java
@@ -103,6 +103,7 @@ public final class ConsulKeyValueConsumer extends
AbstractConsulConsumer<KeyValu
final Message message = exchange.getIn();
message.setHeader(ConsulConstants.CONSUL_KEY, value.getKey());
+ exchange.setProperty(ConsulConstants.CONSUL_RECEIVED_KEY,
value.getKey());
message.setHeader(ConsulConstants.CONSUL_RESULT, true);
message.setHeader(ConsulConstants.CONSUL_FLAGS,
value.getFlags());
message.setHeader(ConsulConstants.CONSUL_CREATE_INDEX,
value.getCreateIndex());
diff --git
a/components/camel-consul/src/main/java/org/apache/camel/component/consul/endpoint/ConsulKeyValueProducer.java
b/components/camel-consul/src/main/java/org/apache/camel/component/consul/endpoint/ConsulKeyValueProducer.java
index fd320a009484..c68e99624398 100644
---
a/components/camel-consul/src/main/java/org/apache/camel/component/consul/endpoint/ConsulKeyValueProducer.java
+++
b/components/camel-consul/src/main/java/org/apache/camel/component/consul/endpoint/ConsulKeyValueProducer.java
@@ -36,7 +36,7 @@ public final class ConsulKeyValueProducer extends
AbstractConsulProducer<KeyValu
protected void put(Message message) throws Exception {
message.setHeader(ConsulConstants.CONSUL_RESULT,
getClient().putValue(
- getMandatoryHeader(message,
ConsulConstants.CONSUL_KEY, getConfiguration().getKey(), String.class),
+ getMandatoryKey(message),
message.getBody(String.class),
message.getHeader(ConsulConstants.CONSUL_FLAGS, 0L,
Long.class),
message.getHeader(ConsulConstants.CONSUL_OPTIONS,
PutOptions.BLANK, PutOptions.class)));
@@ -51,12 +51,12 @@ public final class ConsulKeyValueProducer extends
AbstractConsulProducer<KeyValu
if (asString) {
result = getClient()
.getValueAsString(
- getMandatoryHeader(message,
ConsulConstants.CONSUL_KEY, getConfiguration().getKey(), String.class))
+ getMandatoryKey(message))
.orElse(null);
} else {
result = getClient()
.getValue(
- getMandatoryHeader(message,
ConsulConstants.CONSUL_KEY, getConfiguration().getKey(), String.class),
+ getMandatoryKey(message),
message.getHeader(ConsulConstants.CONSUL_OPTIONS,
QueryOptions.BLANK, QueryOptions.class))
.orElse(null);
}
@@ -72,10 +72,10 @@ public final class ConsulKeyValueProducer extends
AbstractConsulProducer<KeyValu
Boolean.class);
if (asString) {
result = getClient().getValuesAsString(
- getMandatoryHeader(message, ConsulConstants.CONSUL_KEY,
getConfiguration().getKey(), String.class));
+ getMandatoryKey(message));
} else {
result = getClient().getValues(
- getMandatoryHeader(message, ConsulConstants.CONSUL_KEY,
getConfiguration().getKey(), String.class),
+ getMandatoryKey(message),
message.getHeader(ConsulConstants.CONSUL_OPTIONS,
QueryOptions.BLANK, QueryOptions.class));
}
@@ -85,20 +85,20 @@ public final class ConsulKeyValueProducer extends
AbstractConsulProducer<KeyValu
@InvokeOnHeader(ConsulKeyValueActions.GET_KEYS)
protected void getKeys(Message message) throws Exception {
setBodyAndResult(message, getClient()
- .getKeys(getMandatoryHeader(message,
ConsulConstants.CONSUL_KEY, getConfiguration().getKey(), String.class)));
+ .getKeys(getMandatoryKey(message)));
}
@InvokeOnHeader(ConsulKeyValueActions.GET_SESSIONS)
protected void getSessions(Message message) throws Exception {
setBodyAndResult(message, getClient().getSession(
- getMandatoryHeader(message, ConsulConstants.CONSUL_KEY,
getConfiguration().getKey(), String.class))
+ getMandatoryKey(message))
.orElse(null));
}
@InvokeOnHeader(ConsulKeyValueActions.DELETE_KEY)
protected void deleteKey(Message message) throws Exception {
getClient()
- .deleteKey(getMandatoryHeader(message,
ConsulConstants.CONSUL_KEY, getConfiguration().getKey(), String.class));
+ .deleteKey(getMandatoryKey(message));
message.setHeader(ConsulConstants.CONSUL_RESULT, true);
}
@@ -106,7 +106,7 @@ public final class ConsulKeyValueProducer extends
AbstractConsulProducer<KeyValu
@InvokeOnHeader(ConsulKeyValueActions.DELETE_KEYS)
protected void deleteKeys(Message message) throws Exception {
getClient()
- .deleteKeys(getMandatoryHeader(message,
ConsulConstants.CONSUL_KEY, getConfiguration().getKey(), String.class));
+ .deleteKeys(getMandatoryKey(message));
message.setHeader(ConsulConstants.CONSUL_RESULT, true);
}
@@ -115,7 +115,7 @@ public final class ConsulKeyValueProducer extends
AbstractConsulProducer<KeyValu
protected void lock(Message message) throws Exception {
message.setHeader(ConsulConstants.CONSUL_RESULT,
getClient().acquireLock(
- getMandatoryHeader(message,
ConsulConstants.CONSUL_KEY, getConfiguration().getKey(), String.class),
+ getMandatoryKey(message),
message.getBody(String.class),
message.getHeader(ConsulConstants.CONSUL_SESSION, "",
String.class)));
}
@@ -124,7 +124,7 @@ public final class ConsulKeyValueProducer extends
AbstractConsulProducer<KeyValu
protected void unlock(Message message) throws Exception {
message.setHeader(ConsulConstants.CONSUL_RESULT,
getClient().releaseLock(
- getMandatoryHeader(message,
ConsulConstants.CONSUL_KEY, getConfiguration().getKey(), String.class),
+ getMandatoryKey(message),
getMandatoryHeader(message,
ConsulConstants.CONSUL_SESSION, String.class)));
}
}
diff --git
a/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulProducerReceivedKeyTest.java
b/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulProducerReceivedKeyTest.java
new file mode 100644
index 000000000000..f013ccecea51
--- /dev/null
+++
b/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulProducerReceivedKeyTest.java
@@ -0,0 +1,208 @@
+/*
+ * 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.consul;
+
+import java.math.BigInteger;
+import java.util.Base64;
+import java.util.List;
+import java.util.Optional;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.camel.BindToRegistry;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.test.junit6.CamelTestSupport;
+import org.junit.jupiter.api.Test;
+import org.kiwiproject.consul.Consul;
+import org.kiwiproject.consul.EventClient;
+import org.kiwiproject.consul.KeyValueClient;
+import org.kiwiproject.consul.async.ConsulResponseCallback;
+import org.kiwiproject.consul.async.EventResponseCallback;
+import org.kiwiproject.consul.model.ConsulResponse;
+import org.kiwiproject.consul.model.EventResponse;
+import org.kiwiproject.consul.model.ImmutableEventResponse;
+import org.kiwiproject.consul.model.event.ImmutableEvent;
+import org.kiwiproject.consul.model.kv.ImmutableValue;
+import org.kiwiproject.consul.model.kv.Value;
+import org.kiwiproject.consul.option.EventOptions;
+import org.kiwiproject.consul.option.PutOptions;
+import org.kiwiproject.consul.option.QueryOptions;
+
+import static org.awaitility.Awaitility.await;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyLong;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.timeout;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+/**
+ * The key/value and event consumers set the CamelConsulKey header to the key
they received. A producer with a key of
+ * its own must act on that key, not on the key the message came from, unless
the route chose another key.
+ */
+class ConsulProducerReceivedKeyTest extends CamelTestSupport {
+
+ private final KeyValueClient keyValueClient = mock(KeyValueClient.class);
+ private final EventClient eventClient = mock(EventClient.class);
+ private final List<ConsulResponseCallback<Optional<Value>>> valueQueries =
new CopyOnWriteArrayList<>();
+ private final List<ConsulResponseCallback<List<Value>>> valuesQueries =
new CopyOnWriteArrayList<>();
+ private final List<EventResponseCallback> eventQueries = new
CopyOnWriteArrayList<>();
+
+ @BindToRegistry("consul")
+ public Consul consul() {
+ Consul consul = mock(Consul.class);
+ when(consul.keyValueClient()).thenReturn(keyValueClient);
+ when(consul.eventClient()).thenReturn(eventClient);
+ // the blocking queries of the consumers stay pending until the test
answers them
+ doAnswer(inv ->
valueQueries.add(inv.getArgument(2))).when(keyValueClient)
+ .getValue(anyString(), any(QueryOptions.class), any());
+ doAnswer(inv ->
valuesQueries.add(inv.getArgument(2))).when(keyValueClient)
+ .getValues(anyString(), any(QueryOptions.class), any());
+ doAnswer(inv -> eventQueries.add(inv.getArgument(2))).when(eventClient)
+ .listEvents(anyString(), any(QueryOptions.class),
any(EventResponseCallback.class));
+ return consul;
+ }
+
+ @Override
+ public boolean isUseRouteBuilder() {
+ return false;
+ }
+
+ @Test
+ void putWritesTheKeyOfTheEndpoint() throws Exception {
+
route("consul:kv?key=camel/source&valueAsString=true&blockSeconds=1&consulClient=#consul",
+ "consul:kv?key=camel/target&action=PUT&consulClient=#consul");
+
+ await().atMost(5, TimeUnit.SECONDS).until(() ->
!valueQueries.isEmpty());
+ valueQueries.get(0).onComplete(valueResponse("camel/source", "bar"));
+
+ verify(keyValueClient, timeout(5000)).putValue(eq("camel/target"),
eq("bar"), anyLong(), any(PutOptions.class));
+ verify(keyValueClient, never()).putValue(eq("camel/source"),
anyString(), anyLong(), any(PutOptions.class));
+ }
+
+ @Test
+ void deleteKeyDeletesTheKeyOfTheEndpoint() throws Exception {
+
route("consul:kv?key=camel/source&valueAsString=true&blockSeconds=1&consulClient=#consul",
+
"consul:kv?key=camel/target&action=DELETE_KEY&consulClient=#consul");
+
+ await().atMost(5, TimeUnit.SECONDS).until(() ->
!valueQueries.isEmpty());
+ valueQueries.get(0).onComplete(valueResponse("camel/source", "bar"));
+
+ verify(keyValueClient, timeout(5000)).deleteKey("camel/target");
+ verify(keyValueClient, never()).deleteKey("camel/source");
+ }
+
+ @Test
+ void fireFiresTheEventOfTheEndpoint() throws Exception {
+
route("consul:event?key=camel-source&blockSeconds=1&consulClient=#consul",
+
"consul:event?key=camel-target&action=FIRE&consulClient=#consul");
+
+ await().atMost(5, TimeUnit.SECONDS).until(() ->
!eventQueries.isEmpty());
+ eventQueries.get(0).onComplete(eventResponse("camel-source", "bar"));
+
+ verify(eventClient, timeout(5000)).fireEvent(eq("camel-target"),
any(EventOptions.class), eq("bar"));
+ verify(eventClient, never()).fireEvent(eq("camel-source"),
any(EventOptions.class), anyString());
+ }
+
+ @Test
+ void producerWithoutKeyActsOnTheReceivedKey() throws Exception {
+ // a recursive watch of a folder, each changed key deleted once it is
processed
+
route("consul:kv?key=camel/jobs&recursive=true&valueAsString=true&blockSeconds=1&consulClient=#consul",
+ "consul:kv?action=DELETE_KEY&consulClient=#consul");
+
+ await().atMost(5, TimeUnit.SECONDS).until(() ->
!valuesQueries.isEmpty());
+ valuesQueries.get(0).onComplete(new ConsulResponse<>(
+ List.of(value("camel/jobs/1", "bar")), 0, true,
BigInteger.TWO, (String) null, (String) null));
+
+ verify(keyValueClient, timeout(5000)).deleteKey("camel/jobs/1");
+ }
+
+ @Test
+ void producerWithTheWatchedFolderAsKeyActsOnTheFolder() throws Exception {
+ // the upgrade case: a producer that repeats the folder of a recursive
watch as its key acts on the folder, not
+ // on each key the watch returns; without a key it acts on that key
(producerWithoutKeyActsOnTheReceivedKey)
+
route("consul:kv?key=camel/jobs&recursive=true&valueAsString=true&blockSeconds=1&consulClient=#consul",
+
"consul:kv?key=camel/jobs&action=DELETE_KEYS&consulClient=#consul");
+
+ await().atMost(5, TimeUnit.SECONDS).until(() ->
!valuesQueries.isEmpty());
+ valuesQueries.get(0).onComplete(new ConsulResponse<>(
+ List.of(value("camel/jobs/1", "bar")), 0, true,
BigInteger.TWO, (String) null, (String) null));
+
+ verify(keyValueClient, timeout(5000)).deleteKeys("camel/jobs");
+ verify(keyValueClient, never()).deleteKeys("camel/jobs/1");
+ }
+
+ @Test
+ void keyChosenByTheRouteIsUsed() throws Exception {
+ context.addRoutes(new RouteBuilder() {
+ @Override
+ public void configure() {
+
from("consul:kv?key=camel/source&valueAsString=true&blockSeconds=1&consulClient=#consul")
+ .setHeader(ConsulConstants.CONSUL_KEY,
constant("camel/other"))
+
.to("consul:kv?key=camel/target&action=PUT&consulClient=#consul");
+ }
+ });
+ context.start();
+
+ await().atMost(5, TimeUnit.SECONDS).until(() ->
!valueQueries.isEmpty());
+ valueQueries.get(0).onComplete(valueResponse("camel/source", "bar"));
+
+ verify(keyValueClient, timeout(5000)).putValue(eq("camel/other"),
eq("bar"), anyLong(), any(PutOptions.class));
+ }
+
+ private void route(String from, String to) throws Exception {
+ context.addRoutes(new RouteBuilder() {
+ @Override
+ public void configure() {
+ from(from).to(to);
+ }
+ });
+ context.start();
+ }
+
+ private static Value value(String key, String value) {
+ return ImmutableValue.builder()
+ .key(key)
+ .value(Base64.getEncoder().encodeToString(value.getBytes()))
+ .createIndex(1)
+ .modifyIndex(2)
+ .lockIndex(0)
+ .flags(0)
+ .build();
+ }
+
+ private static ConsulResponse<Optional<Value>> valueResponse(String key,
String value) {
+ return new ConsulResponse<>(Optional.of(value(key, value)), 0, true,
BigInteger.TWO, (String) null, (String) null);
+ }
+
+ private static EventResponse eventResponse(String name, String payload) {
+ return ImmutableEventResponse.builder()
+ .addEvents(ImmutableEvent.builder()
+ .id("2a7d7cbc-d6ad-4d5e-8e3c-4b36a54f6bb7")
+ .name(name)
+ .payload(payload)
+ .version(1)
+ .lTime(1L)
+ .build())
+ .index(BigInteger.ONE)
+ .build();
+ }
+}
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 cfe0169fd322..fb7dff1a864c 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
@@ -2762,6 +2762,24 @@ The actions of `consul:agent` and `consul:status` now
set the `CamelConsulResult
The `FIRE` action of `consul:event` with a message without body failed with a
`NullPointerException`. It now fires the
event without payload.
+=== camel-consul - a producer with a key acts on its key, not on the key a
consumer received
+
+The `consul:kv` and `consul:event` consumers set the `CamelConsulKey` header
to the key (or event name) they received,
+and the producers took the key from that header before their `key` option. So
a route from one key to another
+(`from("consul:kv?key=a").to("consul:kv?key=b&action=PUT")`) wrote, deleted or
fired the key it came from, and with PUT
+or FIRE looped for ever. A producer that has a `key` now uses it while the
header still holds the received key (the
+consumers set the new `CamelConsulReceivedKey` exchange property to that key).
A key header set by the route is still
+used, and a producer without `key` still acts on the received key. To act on
the received key with a producer that has
+a `key` of its own, remove the `CamelConsulReceivedKey` property before the
producer.
+
+*Breaking change:* a producer behind a `recursive=true` watch whose `key` is
the watched folder now acts on that key,
+not on each key the watch returns. For example,
+`from("consul:kv?key=jobs&recursive=true").to("consul:kv?key=jobs&action=DELETE_KEYS")`
deleted each key the watch
+returned (such as `jobs/1`) after processing it, and now deletes every key
that starts with `jobs` on the first message
+(at route start if the folder has keys, with the default `firstIndex`),
including keys added after the watch read them,
+and again on each later message. The same applies to the other actions
(`DELETE_KEY`, `GET_VALUE`, `LOCK`, ...). Remove
+`key` from such a producer to keep acting on the received key.
+
=== camel-aws - the HTTP client is no longer looked up on the classpath
The camel-aws components now set the HTTP client of the AWS SDK clients they
create for their endpoints: the Apache 5
diff --git
a/dsl/camel-componentdsl/src/generated/java/org/apache/camel/builder/component/dsl/ConsulComponentBuilderFactory.java
b/dsl/camel-componentdsl/src/generated/java/org/apache/camel/builder/component/dsl/ConsulComponentBuilderFactory.java
index 3da552e249da..7ec4f334f5be 100644
---
a/dsl/camel-componentdsl/src/generated/java/org/apache/camel/builder/component/dsl/ConsulComponentBuilderFactory.java
+++
b/dsl/camel-componentdsl/src/generated/java/org/apache/camel/builder/component/dsl/ConsulComponentBuilderFactory.java
@@ -66,7 +66,11 @@ public interface ConsulComponentBuilderFactory {
}
/**
- * The default key. Can be overridden by CamelConsulKey.
+ * The key (kv) or event name (event) that the consumer watches or that
+ * the producer acts on; the other APIs do not use it. In a producer, a
+ * CamelConsulKey header overrides it, unless the header still holds
the
+ * key that a consul consumer received (the CamelConsulReceivedKey
+ * exchange property).
*
* The option is a: <code>java.lang.String</code> type.
*
diff --git
a/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/ConsulEndpointBuilderFactory.java
b/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/ConsulEndpointBuilderFactory.java
index a6ea38241ca6..9b7b6d415afd 100644
---
a/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/ConsulEndpointBuilderFactory.java
+++
b/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/ConsulEndpointBuilderFactory.java
@@ -73,7 +73,11 @@ public interface ConsulEndpointBuilderFactory {
return this;
}
/**
- * The default key. Can be overridden by CamelConsulKey.
+ * The key (kv) or event name (event) that the consumer watches or that
+ * the producer acts on; the other APIs do not use it. In a producer, a
+ * CamelConsulKey header overrides it, unless the header still holds
the
+ * key that a consul consumer received (the CamelConsulReceivedKey
+ * exchange property).
*
* The option is a: <code>java.lang.String</code> type.
*
@@ -677,7 +681,11 @@ public interface ConsulEndpointBuilderFactory {
return this;
}
/**
- * The default key. Can be overridden by CamelConsulKey.
+ * The key (kv) or event name (event) that the consumer watches or that
+ * the producer acts on; the other APIs do not use it. In a producer, a
+ * CamelConsulKey header overrides it, unless the header still holds
the
+ * key that a consul consumer received (the CamelConsulReceivedKey
+ * exchange property).
*
* The option is a: <code>java.lang.String</code> type.
*
@@ -1141,7 +1149,11 @@ public interface ConsulEndpointBuilderFactory {
return this;
}
/**
- * The default key. Can be overridden by CamelConsulKey.
+ * The key (kv) or event name (event) that the consumer watches or that
+ * the producer acts on; the other APIs do not use it. In a producer, a
+ * CamelConsulKey header overrides it, unless the header still holds
the
+ * key that a consul consumer received (the CamelConsulReceivedKey
+ * exchange property).
*
* The option is a: <code>java.lang.String</code> type.
*