You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by GitBox <gi...@apache.org> on 2020/07/12 08:44:21 UTC

[GitHub] [camel] lburgazzoli commented on a change in pull request #3983: Feature camel ectd3 - Etcd3AggregationRepository using jetcd

lburgazzoli commented on a change in pull request #3983:
URL: https://github.com/apache/camel/pull/3983#discussion_r453283634



##########
File path: camel-dependencies/pom.xml
##########
@@ -486,6 +487,7 @@
     <rdf4j-rio-version>2.4.4</rdf4j-rio-version>
     <reactive-streams-version>1.0.3</reactive-streams-version>
     <reactor-version>3.2.16.RELEASE</reactor-version>
+	<redisson-version>3.13.2</redisson-version>

Review comment:
       does not seem to be related

##########
File path: camel-dependencies/pom.xml
##########
@@ -332,6 +332,7 @@
     <jcr-version>2.0</jcr-version>
     <jedis-client-version>3.1.0</jedis-client-version>
     <jersey-version>2.28</jersey-version>
+	<jetcd-version>0.5.3</jetcd-version>

Review comment:
       looks like this and other xml files have wrong indentation

##########
File path: components/camel-etcd3/src/main/java/org/apache/camel/component/etcd3/processor/aggregate/Etcd3AggregationRepository.java
##########
@@ -0,0 +1,459 @@
+/*
+ * 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.etcd3.processor.aggregate;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.util.Collections;
+import java.util.List;
+import java.util.Set;
+import java.util.TreeSet;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.Exchange;
+import org.apache.camel.impl.DefaultExchange;
+import org.apache.camel.impl.DefaultExchangeHolder;
+import org.apache.camel.spi.OptimisticLockingAggregationRepository;
+import org.apache.camel.spi.RecoverableAggregationRepository;
+import org.apache.camel.support.ServiceSupport;
+import org.apache.camel.util.StringHelper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import io.etcd.jetcd.ByteSequence;
+import io.etcd.jetcd.Client;
+import io.etcd.jetcd.KV;
+import io.etcd.jetcd.KeyValue;
+import io.etcd.jetcd.Lock;
+import io.etcd.jetcd.Txn;
+import io.etcd.jetcd.kv.DeleteResponse;
+import io.etcd.jetcd.kv.GetResponse;
+import io.etcd.jetcd.kv.PutResponse;
+import io.etcd.jetcd.op.Cmp;
+import io.etcd.jetcd.op.CmpTarget;
+import io.etcd.jetcd.op.Op;
+import io.etcd.jetcd.options.DeleteOption;
+import io.etcd.jetcd.options.GetOption;
+import io.etcd.jetcd.options.PutOption;
+
+public class Etcd3AggregationRepository extends ServiceSupport
+		implements RecoverableAggregationRepository, OptimisticLockingAggregationRepository {
+	private static final Logger LOG = LoggerFactory.getLogger(Etcd3AggregationRepository.class.getName());
+	private static final String COMPLETED_SUFFIX = "-completed";
+
+	private boolean optimistic;
+	private boolean useRecovery = true;
+	private String endpoint;
+	private Client client;
+	private KV kvClient;
+	private String prefixName;
+	private String persistencePrefixName;
+	private String deadLetterChannel;
+	private long recoveryInterval = 5000;
+	private int maximumRedeliveries = 3;
+	private boolean allowSerializedHeaders;
+
+	public Etcd3AggregationRepository(final String prefixName, final String endpoint) {
+		this.prefixName = prefixName;
+		this.persistencePrefixName = String.format("%s%s", prefixName, COMPLETED_SUFFIX);
+		this.optimistic = false;
+		this.endpoint = endpoint;
+	}
+
+	public Etcd3AggregationRepository(final String prefixName, final String persistencePrefixName,
+			final String endpoint) {
+		this.prefixName = prefixName;
+		this.persistencePrefixName = persistencePrefixName;
+		this.optimistic = false;
+		this.endpoint = endpoint;
+	}
+
+	public Etcd3AggregationRepository(final String prefixName, final String endpoint, boolean optimistic) {
+		this(prefixName, endpoint);
+		this.optimistic = optimistic;
+	}
+
+	public Etcd3AggregationRepository(final String repositoryName, final String persistentRepositoryName,
+			final String endpoint, boolean optimistic) {
+		this(repositoryName, persistentRepositoryName, endpoint);
+		this.optimistic = optimistic;
+	}
+
+	@Override
+	public Exchange add(CamelContext camelContext, String key, Exchange oldExchange, Exchange newExchange)
+			throws OptimisticLockingException {
+		if (!optimistic) {
+			throw new UnsupportedOperationException();
+		}
+		LOG.trace("Adding an Exchange with ID {} for key {} in an optimistic manner.", newExchange.getExchangeId(),
+				key);
+		try {
+			if (oldExchange == null) {
+				DefaultExchangeHolder holder = DefaultExchangeHolder.marshal(newExchange, true, allowSerializedHeaders);
+				CompletableFuture<GetResponse> completableGetResponse = kvClient
+						.get(ByteSequence.from(String.format("%s/%s", prefixName, key).getBytes()));
+				GetResponse getResponse = completableGetResponse.get();
+				List<KeyValue> keyValues = getResponse.getKvs();
+				if (keyValues.isEmpty()) {
+					ByteArrayOutputStream bos = new ByteArrayOutputStream();
+					ObjectOutputStream oos = new ObjectOutputStream(bos);
+					oos.writeObject(holder);
+					oos.flush();
+					byte[] data = bos.toByteArray();
+					CompletableFuture<PutResponse> completablePutResponse = kvClient.put(
+							ByteSequence.from(String.format("%s/%s", prefixName, key).getBytes()),
+							ByteSequence.from(data));
+					completablePutResponse.get();
+				} else {
+					byte[] data = keyValues.get(0).getValue().getBytes();
+					ByteArrayInputStream in = new ByteArrayInputStream(data);
+					ObjectInputStream is = new ObjectInputStream(in);
+					DefaultExchangeHolder misbehaviorHolder = (DefaultExchangeHolder) is.readObject();
+					Exchange misbehaviorEx = unmarshallExchange(camelContext, misbehaviorHolder);
+					LOG.error(
+							"Optimistic locking failed for exchange with key {}: kvClient.get returned Exchange with ID {}, while it's expected no exchanges to be returned",
+							key, misbehaviorEx != null ? misbehaviorEx.getExchangeId() : "<null>");
+					throw new OptimisticLockingException();
+				}
+			} else {
+				DefaultExchangeHolder newHolder = DefaultExchangeHolder.marshal(newExchange, true,
+						allowSerializedHeaders);
+				ByteArrayOutputStream bos = new ByteArrayOutputStream();
+				ObjectOutputStream oos = new ObjectOutputStream(bos);
+				oos.writeObject(newHolder);
+				oos.flush();
+				byte[] data = bos.toByteArray();
+				CompletableFuture<DeleteResponse> completableDeleteResponse = kvClient
+						.delete(ByteSequence.from(String.format("%s/%s", prefixName, key).getBytes()));
+				DeleteResponse deleteResponse = completableDeleteResponse.get();
+				if (deleteResponse.getDeleted() == 0) {
+					LOG.error(
+							"Optimistic locking failed for exchange with key {}: kvClient.get returned no Exchanges, while it's expected to replace one",
+							key);
+					throw new OptimisticLockingException();
+				}
+				CompletableFuture<PutResponse> completablePutResponse = kvClient.put(
+						ByteSequence.from(String.format("%s/%s", prefixName, key).getBytes()), ByteSequence.from(data));
+				completablePutResponse.get();
+			}
+		} catch (InterruptedException | ExecutionException | IOException | ClassNotFoundException e) {
+			LOG.error(e.getMessage(), e);
+			throw new OptimisticLockingException();
+		}
+		LOG.trace("Added an Exchange with ID {} for key {} in optimistic manner.", newExchange.getExchangeId(), key);
+		return oldExchange;
+	}
+
+	@Override
+	public Exchange add(CamelContext camelContext, String key, Exchange exchange) {
+		if (optimistic) {
+			throw new UnsupportedOperationException();
+		}
+		LOG.trace("Adding an Exchange with ID {} for key {} in a thread-safe manner.", exchange.getExchangeId(), key);
+		Lock lock = null;
+		DefaultExchangeHolder newHolder = DefaultExchangeHolder.marshal(exchange, true, allowSerializedHeaders);
+		try {
+			lock = client.getLockClient();
+			lock.lock(ByteSequence.from(key.getBytes()), 60000);

Review comment:
       the lease id need to be obtained from the `LeaseClient` and not hard-coded, however the `lock` APIs may not behave as you'd expect, see [notes-on-the-usage-of-lock-and-lease](https://github.com/etcd-io/etcd/blob/master/Documentation/learning/why.md#notes-on-the-usage-of-lock-and-lease).
   
   It may be better to use transactions as you can specify the conditions to be met when performing the operation.

##########
File path: components/camel-etcd3/src/main/java/org/apache/camel/component/etcd3/processor/aggregate/Etcd3AggregationRepository.java
##########
@@ -0,0 +1,459 @@
+/*
+ * 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.etcd3.processor.aggregate;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.util.Collections;
+import java.util.List;
+import java.util.Set;
+import java.util.TreeSet;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.Exchange;
+import org.apache.camel.impl.DefaultExchange;
+import org.apache.camel.impl.DefaultExchangeHolder;
+import org.apache.camel.spi.OptimisticLockingAggregationRepository;
+import org.apache.camel.spi.RecoverableAggregationRepository;
+import org.apache.camel.support.ServiceSupport;
+import org.apache.camel.util.StringHelper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import io.etcd.jetcd.ByteSequence;
+import io.etcd.jetcd.Client;
+import io.etcd.jetcd.KV;
+import io.etcd.jetcd.KeyValue;
+import io.etcd.jetcd.Lock;
+import io.etcd.jetcd.Txn;
+import io.etcd.jetcd.kv.DeleteResponse;
+import io.etcd.jetcd.kv.GetResponse;
+import io.etcd.jetcd.kv.PutResponse;
+import io.etcd.jetcd.op.Cmp;
+import io.etcd.jetcd.op.CmpTarget;
+import io.etcd.jetcd.op.Op;
+import io.etcd.jetcd.options.DeleteOption;
+import io.etcd.jetcd.options.GetOption;
+import io.etcd.jetcd.options.PutOption;
+
+public class Etcd3AggregationRepository extends ServiceSupport
+		implements RecoverableAggregationRepository, OptimisticLockingAggregationRepository {
+	private static final Logger LOG = LoggerFactory.getLogger(Etcd3AggregationRepository.class.getName());
+	private static final String COMPLETED_SUFFIX = "-completed";
+
+	private boolean optimistic;
+	private boolean useRecovery = true;
+	private String endpoint;
+	private Client client;
+	private KV kvClient;
+	private String prefixName;
+	private String persistencePrefixName;
+	private String deadLetterChannel;
+	private long recoveryInterval = 5000;
+	private int maximumRedeliveries = 3;
+	private boolean allowSerializedHeaders;
+
+	public Etcd3AggregationRepository(final String prefixName, final String endpoint) {
+		this.prefixName = prefixName;
+		this.persistencePrefixName = String.format("%s%s", prefixName, COMPLETED_SUFFIX);
+		this.optimistic = false;
+		this.endpoint = endpoint;
+	}
+
+	public Etcd3AggregationRepository(final String prefixName, final String persistencePrefixName,
+			final String endpoint) {
+		this.prefixName = prefixName;
+		this.persistencePrefixName = persistencePrefixName;
+		this.optimistic = false;
+		this.endpoint = endpoint;
+	}
+
+	public Etcd3AggregationRepository(final String prefixName, final String endpoint, boolean optimistic) {
+		this(prefixName, endpoint);
+		this.optimistic = optimistic;
+	}
+
+	public Etcd3AggregationRepository(final String repositoryName, final String persistentRepositoryName,
+			final String endpoint, boolean optimistic) {
+		this(repositoryName, persistentRepositoryName, endpoint);
+		this.optimistic = optimistic;
+	}
+
+	@Override
+	public Exchange add(CamelContext camelContext, String key, Exchange oldExchange, Exchange newExchange)
+			throws OptimisticLockingException {
+		if (!optimistic) {
+			throw new UnsupportedOperationException();
+		}
+		LOG.trace("Adding an Exchange with ID {} for key {} in an optimistic manner.", newExchange.getExchangeId(),
+				key);
+		try {
+			if (oldExchange == null) {
+				DefaultExchangeHolder holder = DefaultExchangeHolder.marshal(newExchange, true, allowSerializedHeaders);
+				CompletableFuture<GetResponse> completableGetResponse = kvClient
+						.get(ByteSequence.from(String.format("%s/%s", prefixName, key).getBytes()));
+				GetResponse getResponse = completableGetResponse.get();
+				List<KeyValue> keyValues = getResponse.getKvs();
+				if (keyValues.isEmpty()) {
+					ByteArrayOutputStream bos = new ByteArrayOutputStream();
+					ObjectOutputStream oos = new ObjectOutputStream(bos);
+					oos.writeObject(holder);
+					oos.flush();
+					byte[] data = bos.toByteArray();
+					CompletableFuture<PutResponse> completablePutResponse = kvClient.put(
+							ByteSequence.from(String.format("%s/%s", prefixName, key).getBytes()),
+							ByteSequence.from(data));
+					completablePutResponse.get();
+				} else {
+					byte[] data = keyValues.get(0).getValue().getBytes();
+					ByteArrayInputStream in = new ByteArrayInputStream(data);
+					ObjectInputStream is = new ObjectInputStream(in);
+					DefaultExchangeHolder misbehaviorHolder = (DefaultExchangeHolder) is.readObject();
+					Exchange misbehaviorEx = unmarshallExchange(camelContext, misbehaviorHolder);
+					LOG.error(
+							"Optimistic locking failed for exchange with key {}: kvClient.get returned Exchange with ID {}, while it's expected no exchanges to be returned",
+							key, misbehaviorEx != null ? misbehaviorEx.getExchangeId() : "<null>");
+					throw new OptimisticLockingException();
+				}
+			} else {
+				DefaultExchangeHolder newHolder = DefaultExchangeHolder.marshal(newExchange, true,
+						allowSerializedHeaders);
+				ByteArrayOutputStream bos = new ByteArrayOutputStream();
+				ObjectOutputStream oos = new ObjectOutputStream(bos);
+				oos.writeObject(newHolder);
+				oos.flush();
+				byte[] data = bos.toByteArray();
+				CompletableFuture<DeleteResponse> completableDeleteResponse = kvClient
+						.delete(ByteSequence.from(String.format("%s/%s", prefixName, key).getBytes()));
+				DeleteResponse deleteResponse = completableDeleteResponse.get();
+				if (deleteResponse.getDeleted() == 0) {
+					LOG.error(
+							"Optimistic locking failed for exchange with key {}: kvClient.get returned no Exchanges, while it's expected to replace one",
+							key);
+					throw new OptimisticLockingException();
+				}
+				CompletableFuture<PutResponse> completablePutResponse = kvClient.put(
+						ByteSequence.from(String.format("%s/%s", prefixName, key).getBytes()), ByteSequence.from(data));
+				completablePutResponse.get();
+			}
+		} catch (InterruptedException | ExecutionException | IOException | ClassNotFoundException e) {
+			LOG.error(e.getMessage(), e);
+			throw new OptimisticLockingException();
+		}
+		LOG.trace("Added an Exchange with ID {} for key {} in optimistic manner.", newExchange.getExchangeId(), key);
+		return oldExchange;
+	}
+
+	@Override
+	public Exchange add(CamelContext camelContext, String key, Exchange exchange) {
+		if (optimistic) {
+			throw new UnsupportedOperationException();
+		}
+		LOG.trace("Adding an Exchange with ID {} for key {} in a thread-safe manner.", exchange.getExchangeId(), key);
+		Lock lock = null;
+		DefaultExchangeHolder newHolder = DefaultExchangeHolder.marshal(exchange, true, allowSerializedHeaders);
+		try {
+			lock = client.getLockClient();
+			lock.lock(ByteSequence.from(key.getBytes()), 60000);
+			ByteArrayOutputStream bos = new ByteArrayOutputStream();
+			ObjectOutputStream oos = new ObjectOutputStream(bos);
+			oos.writeObject(newHolder);
+			oos.flush();
+			byte[] data = bos.toByteArray();
+			CompletableFuture<PutResponse> completablePutResponse = kvClient.put(
+					ByteSequence.from(String.format("%s/%s", prefixName, key).getBytes()), ByteSequence.from(data));
+			completablePutResponse.get();
+		} catch (InterruptedException | ExecutionException | IOException e) {
+			LOG.error(e.getMessage(), e);
+		} finally {
+			if (lock != null) {
+				lock.unlock(ByteSequence.from(key.getBytes()));
+			}
+		}
+		return unmarshallExchange(camelContext, newHolder);
+	}
+
+	@Override
+	public Set<String> scan(CamelContext camelContext) {
+		if (useRecovery) {
+			LOG.trace("Scanning for exchanges to recover in {} context", camelContext.getName());
+			CompletableFuture<GetResponse> completableGetResponse = kvClient.get(
+					ByteSequence.from(persistencePrefixName.getBytes()),
+					GetOption.newBuilder().withPrefix(ByteSequence.from(persistencePrefixName.getBytes())).build());
+			Set<String> scanned = Collections.unmodifiableSet(new TreeSet<>());
+			try {
+				GetResponse getResponse = completableGetResponse.get();
+				Set<String> keys = new TreeSet<>();
+				getResponse.getKvs().forEach(kv -> keys.add(new String(kv.getKey().getBytes())));
+				scanned = Collections.unmodifiableSet(keys);
+				LOG.trace("Found {} keys for exchanges to recover in {} context", scanned.size(),
+						camelContext.getName());
+			} catch (InterruptedException | ExecutionException e) {
+				LOG.error(e.getMessage(), e);
+			}
+			return scanned;
+		} else {
+			LOG.warn(
+					"What for to run recovery scans in {} context while prefix {} is running in non-recoverable aggregation repository mode?!",
+					camelContext.getName(), prefixName);
+			return Collections.emptySet();
+		}
+	}
+
+	@Override
+	public Exchange recover(CamelContext camelContext, String exchangeId) {
+		LOG.trace("Recovering an Exchange with ID {}.", exchangeId);
+		CompletableFuture<GetResponse> completableResponse = kvClient
+				.get(ByteSequence.from(String.format("%s/%s", persistencePrefixName, exchangeId).getBytes()));
+		try {
+			GetResponse getResponse = completableResponse.get();
+			byte[] data = getResponse.getKvs().get(0).getValue().getBytes();
+			ByteArrayInputStream in = new ByteArrayInputStream(data);
+			ObjectInputStream is = new ObjectInputStream(in);
+			DefaultExchangeHolder holder = (DefaultExchangeHolder) is.readObject();
+			return useRecovery ? unmarshallExchange(camelContext, holder) : null;
+		} catch (InterruptedException | ExecutionException | IOException | ClassNotFoundException e) {
+			LOG.error(e.getMessage(), e);
+		}
+		return null;
+	}
+
+	@Override
+	public void setRecoveryInterval(long interval, TimeUnit timeUnit) {
+		this.recoveryInterval = timeUnit.toMillis(interval);
+	}
+
+	@Override
+	public void setRecoveryInterval(long interval) {
+		this.recoveryInterval = interval;
+	}
+
+	@Override
+	public long getRecoveryIntervalInMillis() {
+		return recoveryInterval;
+	}
+
+	@Override
+	public void setUseRecovery(boolean useRecovery) {
+		this.useRecovery = useRecovery;
+	}
+
+	@Override
+	public boolean isUseRecovery() {
+		return useRecovery;
+	}
+
+	@Override
+	public void setDeadLetterUri(String deadLetterUri) {
+		this.deadLetterChannel = deadLetterUri;
+	}
+
+	@Override
+	public String getDeadLetterUri() {
+		return deadLetterChannel;
+	}
+
+	@Override
+	public void setMaximumRedeliveries(int maximumRedeliveries) {
+		this.maximumRedeliveries = maximumRedeliveries;
+	}
+
+	@Override
+	public int getMaximumRedeliveries() {
+		return maximumRedeliveries;
+	}
+
+	@Override
+	public Exchange get(CamelContext camelContext, String key) {
+		CompletableFuture<GetResponse> completableResponse = kvClient
+				.get(ByteSequence.from(String.format("%s/%s", prefixName, key).getBytes()));
+		try {
+			GetResponse getResponse = completableResponse.get();
+			byte[] data = getResponse.getKvs().get(0).getValue().getBytes();
+			ByteArrayInputStream in = new ByteArrayInputStream(data);
+			ObjectInputStream is = new ObjectInputStream(in);
+			DefaultExchangeHolder holder = (DefaultExchangeHolder) is.readObject();
+			return unmarshallExchange(camelContext, holder);
+		} catch (InterruptedException | ExecutionException | IOException | ClassNotFoundException e) {
+			LOG.error(e.getMessage(), e);
+		}
+		return null;
+	}
+
+	public boolean isAllowSerializedHeaders() {
+		return allowSerializedHeaders;
+	}
+
+	public void setAllowSerializedHeaders(boolean allowSerializedHeaders) {
+		this.allowSerializedHeaders = allowSerializedHeaders;
+	}
+
+	@Override
+	public void remove(CamelContext camelContext, String key, Exchange exchange) {
+		DefaultExchangeHolder holder = DefaultExchangeHolder.marshal(exchange, true, allowSerializedHeaders);
+		if (optimistic) {
+			LOG.trace("Removing an exchange with ID {} for key {} in an optimistic manner.", exchange.getExchangeId(),
+					key);
+			CompletableFuture<DeleteResponse> completableDeleteResponse = kvClient
+					.delete(ByteSequence.from(String.format("%s/%s", prefixName, key).getBytes()));
+			try {
+				DeleteResponse deleteResponse = completableDeleteResponse.get();
+				if (deleteResponse.getDeleted() == 0) {
+					LOG.error(
+							"Optimistic locking failed for exchange with key {}: kvClient.delete removed no Exchanges, while it's expected to remove one.",
+							key);
+					throw new OptimisticLockingException();
+				}
+			} catch (InterruptedException | ExecutionException e) {
+				LOG.error(e.getMessage(), e);
+			}
+			LOG.trace("Removed an exchange with ID {} for key {} in an optimistic manner.", exchange.getExchangeId(),
+					key);
+			if (useRecovery) {
+				LOG.trace(
+						"Putting an exchange with ID {} for key {} into a recoverable storage in an optimistic manner.",
+						exchange.getExchangeId(), key);
+				try {
+					ByteArrayOutputStream bos = new ByteArrayOutputStream();
+					ObjectOutputStream oos = new ObjectOutputStream(bos);
+					oos.writeObject(holder);
+					oos.flush();
+					byte[] data = bos.toByteArray();
+					CompletableFuture<PutResponse> completablePutResponse = kvClient.put(
+							ByteSequence.from(String.format("%s/%s", persistencePrefixName, key).getBytes()),
+							ByteSequence.from(data));
+					completablePutResponse.get();
+					LOG.trace(
+							"Put an exchange with ID {} for key {} into a recoverable storage in an optimistic manner.",
+							exchange.getExchangeId(), key);
+				} catch (IOException | InterruptedException | ExecutionException e) {
+					LOG.error(e.getMessage(), e);
+				}
+
+			}
+		} else {
+			if (useRecovery) {
+				LOG.trace("Removing an exchange with ID {} for key {} in a thread-safe manner.",
+						exchange.getExchangeId(), key);
+				Txn transaction = kvClient.txn();
+				try {
+					CompletableFuture<GetResponse> completableResponse = kvClient
+							.get(ByteSequence.from(String.format("%s/%s", prefixName, key).getBytes()));
+					GetResponse getResponse = completableResponse.get();
+					byte[] data = getResponse.getKvs().get(0).getValue().getBytes();
+					ByteArrayInputStream in = new ByteArrayInputStream(data);
+					ObjectInputStream is = new ObjectInputStream(in);
+					DefaultExchangeHolder removedHolder = (DefaultExchangeHolder) is.readObject();
+					ByteArrayOutputStream bos = new ByteArrayOutputStream();
+					ObjectOutputStream oos = new ObjectOutputStream(bos);
+					oos.writeObject(removedHolder);
+					oos.flush();
+					data = bos.toByteArray();
+					transaction
+							.If(new Cmp(ByteSequence.from(String.format("%s/%s", prefixName, key).getBytes()),
+									Cmp.Op.EQUAL,
+									CmpTarget.value(
+											ByteSequence.from(String.format("%s/%s", prefixName, key).getBytes()))))
+							.Then(Op.delete(ByteSequence.from(String.format("%s/%s", prefixName, key).getBytes()),
+									DeleteOption.DEFAULT),
+									Op.put(ByteSequence
+											.from(String.format("%s/%s", persistencePrefixName, key).getBytes()),
+											ByteSequence.from(data), PutOption.DEFAULT))
+							.commit();
+					LOG.trace("Removed an exchange with ID {} for key {} in a thread-safe manner.",
+							exchange.getExchangeId(), key);
+					LOG.trace(
+							"Put an exchange with ID {} for key {} into a recoverable storage in a thread-safe manner.",
+							exchange.getExchangeId(), key);
+				} catch (Throwable throwable) {
+					throw new RuntimeException(throwable);
+				}
+			} else {
+				CompletableFuture<DeleteResponse> completableDeleteResponse = kvClient
+						.delete(ByteSequence.from(String.format("%s/%s", prefixName, key).getBytes()));
+				try {
+					completableDeleteResponse.get();
+				} catch (InterruptedException | ExecutionException e) {
+					LOG.error(e.getMessage(), e);
+				}
+			}
+		}
+	}
+
+	@Override
+	public void confirm(CamelContext camelContext, String exchangeId) {
+		LOG.trace("Confirming an exchange with ID {}.", exchangeId);
+		if (useRecovery) {
+			CompletableFuture<DeleteResponse> completableDeleteResponse = kvClient
+					.delete(ByteSequence.from(String.format("%s/%s", persistencePrefixName, exchangeId).getBytes()));
+			try {
+				completableDeleteResponse.get();
+			} catch (InterruptedException | ExecutionException e) {
+				LOG.error(e.getMessage(), e);
+			}
+		}
+	}
+
+	@Override
+	public Set<String> getKeys() {
+		CompletableFuture<GetResponse> completableGetResponse = kvClient.get(ByteSequence.from(prefixName.getBytes()),
+				GetOption.newBuilder().withRange(ByteSequence.from(prefixName.getBytes())).build());
+		Set<String> scanned = Collections.unmodifiableSet(new TreeSet<>());
+		try {
+			GetResponse getResponse = completableGetResponse.get();
+			Set<String> keys = new TreeSet<>();
+			getResponse.getKvs().forEach(kv -> keys.add(new String(kv.getKey().getBytes())));
+			scanned = Collections.unmodifiableSet(keys);
+		} catch (InterruptedException | ExecutionException e) {
+			LOG.error(e.getMessage(), e);
+		}
+		return scanned;
+	}
+
+	@Override
+	protected void doStart() throws Exception {
+		if (maximumRedeliveries < 0) {
+			throw new IllegalArgumentException("Maximum redelivery retries must be zero or a positive integer.");
+		}
+		if (recoveryInterval < 0) {
+			throw new IllegalArgumentException("Recovery interval must be zero or a positive integer.");
+		}
+		StringHelper.notEmpty(prefixName, "prefixName");
+		client = Client.builder().endpoints(endpoint).build();
+		kvClient = client.getKVClient();
+	}
+
+	@Override
+	protected void doStop() throws Exception {
+		client.close();

Review comment:
       Nee to check if client is `null`

##########
File path: components/camel-etcd3/pom.xml
##########
@@ -0,0 +1,157 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+    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.
+
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+
+    <modelVersion>4.0.0</modelVersion>
+
+    <parent>
+        <groupId>org.apache.camel</groupId>
+        <artifactId>components</artifactId>
+        <version>3.5.0-SNAPSHOT</version>
+    </parent>
+
+    <artifactId>camel-etcd3</artifactId>
+    <packaging>jar</packaging>
+    <name>Camel :: Etcd3</name>
+    <description>Camel Etcd3 support</description>
+
+    <dependencies>
+
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-cloud</artifactId>
+        </dependency>
+
+        <!-- etcd -->
+        <dependency>
+            <groupId>org.apache.commons</groupId>
+            <artifactId>commons-lang3</artifactId>
+            <version>${commons-lang3-version}</version>
+        </dependency>
+        
+        <dependency>
+            <groupId>io.etcd</groupId>
+            <artifactId>jetcd-all</artifactId>
+            <version>${jetcd-version}</version>
+        </dependency>
+
+        <dependency>
+            <groupId>io.etcd</groupId>
+            <artifactId>jetcd-core</artifactId>

Review comment:
       either you use `jetcd-all` or you use `jetcd-core` as `jetcd-all` shades all the `jetcd` artefacts and dependencies

##########
File path: components/camel-etcd3/pom.xml
##########
@@ -0,0 +1,157 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+    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.
+
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+
+    <modelVersion>4.0.0</modelVersion>
+
+    <parent>
+        <groupId>org.apache.camel</groupId>
+        <artifactId>components</artifactId>
+        <version>3.5.0-SNAPSHOT</version>
+    </parent>
+
+    <artifactId>camel-etcd3</artifactId>
+    <packaging>jar</packaging>
+    <name>Camel :: Etcd3</name>
+    <description>Camel Etcd3 support</description>
+
+    <dependencies>
+
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-cloud</artifactId>
+        </dependency>
+
+        <!-- etcd -->
+        <dependency>
+            <groupId>org.apache.commons</groupId>
+            <artifactId>commons-lang3</artifactId>
+            <version>${commons-lang3-version}</version>
+        </dependency>
+        
+        <dependency>
+            <groupId>io.etcd</groupId>
+            <artifactId>jetcd-all</artifactId>
+            <version>${jetcd-version}</version>
+        </dependency>
+
+        <dependency>
+            <groupId>io.etcd</groupId>
+            <artifactId>jetcd-core</artifactId>
+            <version>${jetcd-version}</version>
+        </dependency>
+
+        <!-- logging -->
+        <dependency>
+            <groupId>org.apache.logging.log4j</groupId>
+            <artifactId>log4j-slf4j-impl</artifactId>
+            <scope>test</scope>
+        </dependency>
+
+        <!-- testing -->
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-testcontainers-spring-junit5</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-testcontainers-junit5</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-http</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-jetty</artifactId>
+            <scope>test</scope>
+        </dependency>
+

Review comment:
       Those dependencies need to be leaned up and you also need to add some test case for the aggregation repository

##########
File path: components/camel-etcd3/src/main/java/org/apache/camel/component/etcd3/processor/aggregate/Etcd3AggregationRepository.java
##########
@@ -0,0 +1,459 @@
+/*
+ * 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.etcd3.processor.aggregate;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.util.Collections;
+import java.util.List;
+import java.util.Set;
+import java.util.TreeSet;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.Exchange;
+import org.apache.camel.impl.DefaultExchange;
+import org.apache.camel.impl.DefaultExchangeHolder;
+import org.apache.camel.spi.OptimisticLockingAggregationRepository;
+import org.apache.camel.spi.RecoverableAggregationRepository;
+import org.apache.camel.support.ServiceSupport;
+import org.apache.camel.util.StringHelper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import io.etcd.jetcd.ByteSequence;
+import io.etcd.jetcd.Client;
+import io.etcd.jetcd.KV;
+import io.etcd.jetcd.KeyValue;
+import io.etcd.jetcd.Lock;
+import io.etcd.jetcd.Txn;
+import io.etcd.jetcd.kv.DeleteResponse;
+import io.etcd.jetcd.kv.GetResponse;
+import io.etcd.jetcd.kv.PutResponse;
+import io.etcd.jetcd.op.Cmp;
+import io.etcd.jetcd.op.CmpTarget;
+import io.etcd.jetcd.op.Op;
+import io.etcd.jetcd.options.DeleteOption;
+import io.etcd.jetcd.options.GetOption;
+import io.etcd.jetcd.options.PutOption;
+
+public class Etcd3AggregationRepository extends ServiceSupport
+		implements RecoverableAggregationRepository, OptimisticLockingAggregationRepository {
+	private static final Logger LOG = LoggerFactory.getLogger(Etcd3AggregationRepository.class.getName());
+	private static final String COMPLETED_SUFFIX = "-completed";
+
+	private boolean optimistic;
+	private boolean useRecovery = true;
+	private String endpoint;
+	private Client client;
+	private KV kvClient;
+	private String prefixName;
+	private String persistencePrefixName;
+	private String deadLetterChannel;
+	private long recoveryInterval = 5000;
+	private int maximumRedeliveries = 3;
+	private boolean allowSerializedHeaders;
+
+	public Etcd3AggregationRepository(final String prefixName, final String endpoint) {
+		this.prefixName = prefixName;
+		this.persistencePrefixName = String.format("%s%s", prefixName, COMPLETED_SUFFIX);
+		this.optimistic = false;
+		this.endpoint = endpoint;
+	}
+
+	public Etcd3AggregationRepository(final String prefixName, final String persistencePrefixName,
+			final String endpoint) {
+		this.prefixName = prefixName;
+		this.persistencePrefixName = persistencePrefixName;
+		this.optimistic = false;
+		this.endpoint = endpoint;
+	}
+
+	public Etcd3AggregationRepository(final String prefixName, final String endpoint, boolean optimistic) {
+		this(prefixName, endpoint);
+		this.optimistic = optimistic;
+	}
+
+	public Etcd3AggregationRepository(final String repositoryName, final String persistentRepositoryName,
+			final String endpoint, boolean optimistic) {
+		this(repositoryName, persistentRepositoryName, endpoint);
+		this.optimistic = optimistic;
+	}
+
+	@Override
+	public Exchange add(CamelContext camelContext, String key, Exchange oldExchange, Exchange newExchange)
+			throws OptimisticLockingException {
+		if (!optimistic) {
+			throw new UnsupportedOperationException();
+		}
+		LOG.trace("Adding an Exchange with ID {} for key {} in an optimistic manner.", newExchange.getExchangeId(),
+				key);
+		try {
+			if (oldExchange == null) {
+				DefaultExchangeHolder holder = DefaultExchangeHolder.marshal(newExchange, true, allowSerializedHeaders);
+				CompletableFuture<GetResponse> completableGetResponse = kvClient
+						.get(ByteSequence.from(String.format("%s/%s", prefixName, key).getBytes()));
+				GetResponse getResponse = completableGetResponse.get();
+				List<KeyValue> keyValues = getResponse.getKvs();
+				if (keyValues.isEmpty()) {
+					ByteArrayOutputStream bos = new ByteArrayOutputStream();
+					ObjectOutputStream oos = new ObjectOutputStream(bos);
+					oos.writeObject(holder);
+					oos.flush();
+					byte[] data = bos.toByteArray();
+					CompletableFuture<PutResponse> completablePutResponse = kvClient.put(
+							ByteSequence.from(String.format("%s/%s", prefixName, key).getBytes()),
+							ByteSequence.from(data));
+					completablePutResponse.get();
+				} else {
+					byte[] data = keyValues.get(0).getValue().getBytes();
+					ByteArrayInputStream in = new ByteArrayInputStream(data);
+					ObjectInputStream is = new ObjectInputStream(in);
+					DefaultExchangeHolder misbehaviorHolder = (DefaultExchangeHolder) is.readObject();
+					Exchange misbehaviorEx = unmarshallExchange(camelContext, misbehaviorHolder);
+					LOG.error(
+							"Optimistic locking failed for exchange with key {}: kvClient.get returned Exchange with ID {}, while it's expected no exchanges to be returned",
+							key, misbehaviorEx != null ? misbehaviorEx.getExchangeId() : "<null>");
+					throw new OptimisticLockingException();
+				}
+			} else {
+				DefaultExchangeHolder newHolder = DefaultExchangeHolder.marshal(newExchange, true,
+						allowSerializedHeaders);
+				ByteArrayOutputStream bos = new ByteArrayOutputStream();
+				ObjectOutputStream oos = new ObjectOutputStream(bos);
+				oos.writeObject(newHolder);
+				oos.flush();
+				byte[] data = bos.toByteArray();
+				CompletableFuture<DeleteResponse> completableDeleteResponse = kvClient
+						.delete(ByteSequence.from(String.format("%s/%s", prefixName, key).getBytes()));
+				DeleteResponse deleteResponse = completableDeleteResponse.get();
+				if (deleteResponse.getDeleted() == 0) {
+					LOG.error(
+							"Optimistic locking failed for exchange with key {}: kvClient.get returned no Exchanges, while it's expected to replace one",
+							key);
+					throw new OptimisticLockingException();
+				}
+				CompletableFuture<PutResponse> completablePutResponse = kvClient.put(
+						ByteSequence.from(String.format("%s/%s", prefixName, key).getBytes()), ByteSequence.from(data));
+				completablePutResponse.get();
+			}
+		} catch (InterruptedException | ExecutionException | IOException | ClassNotFoundException e) {
+			LOG.error(e.getMessage(), e);
+			throw new OptimisticLockingException();
+		}
+		LOG.trace("Added an Exchange with ID {} for key {} in optimistic manner.", newExchange.getExchangeId(), key);
+		return oldExchange;
+	}
+
+	@Override
+	public Exchange add(CamelContext camelContext, String key, Exchange exchange) {
+		if (optimistic) {
+			throw new UnsupportedOperationException();
+		}
+		LOG.trace("Adding an Exchange with ID {} for key {} in a thread-safe manner.", exchange.getExchangeId(), key);
+		Lock lock = null;
+		DefaultExchangeHolder newHolder = DefaultExchangeHolder.marshal(exchange, true, allowSerializedHeaders);
+		try {
+			lock = client.getLockClient();
+			lock.lock(ByteSequence.from(key.getBytes()), 60000);
+			ByteArrayOutputStream bos = new ByteArrayOutputStream();
+			ObjectOutputStream oos = new ObjectOutputStream(bos);
+			oos.writeObject(newHolder);
+			oos.flush();
+			byte[] data = bos.toByteArray();
+			CompletableFuture<PutResponse> completablePutResponse = kvClient.put(
+					ByteSequence.from(String.format("%s/%s", prefixName, key).getBytes()), ByteSequence.from(data));
+			completablePutResponse.get();
+		} catch (InterruptedException | ExecutionException | IOException e) {
+			LOG.error(e.getMessage(), e);

Review comment:
       doesn't this case need to be properly handled ?

##########
File path: components/camel-etcd3/src/main/java/org/apache/camel/component/etcd3/processor/aggregate/Etcd3AggregationRepository.java
##########
@@ -0,0 +1,459 @@
+/*
+ * 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.etcd3.processor.aggregate;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.util.Collections;
+import java.util.List;
+import java.util.Set;
+import java.util.TreeSet;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.Exchange;
+import org.apache.camel.impl.DefaultExchange;
+import org.apache.camel.impl.DefaultExchangeHolder;
+import org.apache.camel.spi.OptimisticLockingAggregationRepository;
+import org.apache.camel.spi.RecoverableAggregationRepository;
+import org.apache.camel.support.ServiceSupport;
+import org.apache.camel.util.StringHelper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import io.etcd.jetcd.ByteSequence;
+import io.etcd.jetcd.Client;
+import io.etcd.jetcd.KV;
+import io.etcd.jetcd.KeyValue;
+import io.etcd.jetcd.Lock;
+import io.etcd.jetcd.Txn;
+import io.etcd.jetcd.kv.DeleteResponse;
+import io.etcd.jetcd.kv.GetResponse;
+import io.etcd.jetcd.kv.PutResponse;
+import io.etcd.jetcd.op.Cmp;
+import io.etcd.jetcd.op.CmpTarget;
+import io.etcd.jetcd.op.Op;
+import io.etcd.jetcd.options.DeleteOption;
+import io.etcd.jetcd.options.GetOption;
+import io.etcd.jetcd.options.PutOption;
+
+public class Etcd3AggregationRepository extends ServiceSupport
+		implements RecoverableAggregationRepository, OptimisticLockingAggregationRepository {
+	private static final Logger LOG = LoggerFactory.getLogger(Etcd3AggregationRepository.class.getName());
+	private static final String COMPLETED_SUFFIX = "-completed";
+
+	private boolean optimistic;
+	private boolean useRecovery = true;
+	private String endpoint;
+	private Client client;
+	private KV kvClient;
+	private String prefixName;
+	private String persistencePrefixName;
+	private String deadLetterChannel;
+	private long recoveryInterval = 5000;
+	private int maximumRedeliveries = 3;
+	private boolean allowSerializedHeaders;
+
+	public Etcd3AggregationRepository(final String prefixName, final String endpoint) {
+		this.prefixName = prefixName;
+		this.persistencePrefixName = String.format("%s%s", prefixName, COMPLETED_SUFFIX);
+		this.optimistic = false;
+		this.endpoint = endpoint;
+	}
+
+	public Etcd3AggregationRepository(final String prefixName, final String persistencePrefixName,
+			final String endpoint) {
+		this.prefixName = prefixName;
+		this.persistencePrefixName = persistencePrefixName;
+		this.optimistic = false;
+		this.endpoint = endpoint;
+	}
+
+	public Etcd3AggregationRepository(final String prefixName, final String endpoint, boolean optimistic) {
+		this(prefixName, endpoint);
+		this.optimistic = optimistic;
+	}
+
+	public Etcd3AggregationRepository(final String repositoryName, final String persistentRepositoryName,
+			final String endpoint, boolean optimistic) {
+		this(repositoryName, persistentRepositoryName, endpoint);
+		this.optimistic = optimistic;
+	}
+
+	@Override
+	public Exchange add(CamelContext camelContext, String key, Exchange oldExchange, Exchange newExchange)
+			throws OptimisticLockingException {
+		if (!optimistic) {
+			throw new UnsupportedOperationException();
+		}
+		LOG.trace("Adding an Exchange with ID {} for key {} in an optimistic manner.", newExchange.getExchangeId(),
+				key);
+		try {
+			if (oldExchange == null) {
+				DefaultExchangeHolder holder = DefaultExchangeHolder.marshal(newExchange, true, allowSerializedHeaders);
+				CompletableFuture<GetResponse> completableGetResponse = kvClient
+						.get(ByteSequence.from(String.format("%s/%s", prefixName, key).getBytes()));
+				GetResponse getResponse = completableGetResponse.get();
+				List<KeyValue> keyValues = getResponse.getKvs();
+				if (keyValues.isEmpty()) {
+					ByteArrayOutputStream bos = new ByteArrayOutputStream();
+					ObjectOutputStream oos = new ObjectOutputStream(bos);
+					oos.writeObject(holder);
+					oos.flush();
+					byte[] data = bos.toByteArray();
+					CompletableFuture<PutResponse> completablePutResponse = kvClient.put(
+							ByteSequence.from(String.format("%s/%s", prefixName, key).getBytes()),
+							ByteSequence.from(data));
+					completablePutResponse.get();
+				} else {
+					byte[] data = keyValues.get(0).getValue().getBytes();
+					ByteArrayInputStream in = new ByteArrayInputStream(data);
+					ObjectInputStream is = new ObjectInputStream(in);
+					DefaultExchangeHolder misbehaviorHolder = (DefaultExchangeHolder) is.readObject();
+					Exchange misbehaviorEx = unmarshallExchange(camelContext, misbehaviorHolder);
+					LOG.error(
+							"Optimistic locking failed for exchange with key {}: kvClient.get returned Exchange with ID {}, while it's expected no exchanges to be returned",
+							key, misbehaviorEx != null ? misbehaviorEx.getExchangeId() : "<null>");
+					throw new OptimisticLockingException();
+				}
+			} else {
+				DefaultExchangeHolder newHolder = DefaultExchangeHolder.marshal(newExchange, true,
+						allowSerializedHeaders);
+				ByteArrayOutputStream bos = new ByteArrayOutputStream();
+				ObjectOutputStream oos = new ObjectOutputStream(bos);
+				oos.writeObject(newHolder);
+				oos.flush();
+				byte[] data = bos.toByteArray();
+				CompletableFuture<DeleteResponse> completableDeleteResponse = kvClient
+						.delete(ByteSequence.from(String.format("%s/%s", prefixName, key).getBytes()));
+				DeleteResponse deleteResponse = completableDeleteResponse.get();
+				if (deleteResponse.getDeleted() == 0) {
+					LOG.error(
+							"Optimistic locking failed for exchange with key {}: kvClient.get returned no Exchanges, while it's expected to replace one",
+							key);
+					throw new OptimisticLockingException();
+				}
+				CompletableFuture<PutResponse> completablePutResponse = kvClient.put(
+						ByteSequence.from(String.format("%s/%s", prefixName, key).getBytes()), ByteSequence.from(data));
+				completablePutResponse.get();
+			}
+		} catch (InterruptedException | ExecutionException | IOException | ClassNotFoundException e) {
+			LOG.error(e.getMessage(), e);
+			throw new OptimisticLockingException();
+		}
+		LOG.trace("Added an Exchange with ID {} for key {} in optimistic manner.", newExchange.getExchangeId(), key);
+		return oldExchange;
+	}
+
+	@Override
+	public Exchange add(CamelContext camelContext, String key, Exchange exchange) {
+		if (optimistic) {
+			throw new UnsupportedOperationException();
+		}
+		LOG.trace("Adding an Exchange with ID {} for key {} in a thread-safe manner.", exchange.getExchangeId(), key);
+		Lock lock = null;
+		DefaultExchangeHolder newHolder = DefaultExchangeHolder.marshal(exchange, true, allowSerializedHeaders);
+		try {
+			lock = client.getLockClient();
+			lock.lock(ByteSequence.from(key.getBytes()), 60000);
+			ByteArrayOutputStream bos = new ByteArrayOutputStream();
+			ObjectOutputStream oos = new ObjectOutputStream(bos);
+			oos.writeObject(newHolder);
+			oos.flush();
+			byte[] data = bos.toByteArray();
+			CompletableFuture<PutResponse> completablePutResponse = kvClient.put(
+					ByteSequence.from(String.format("%s/%s", prefixName, key).getBytes()), ByteSequence.from(data));
+			completablePutResponse.get();
+		} catch (InterruptedException | ExecutionException | IOException e) {
+			LOG.error(e.getMessage(), e);
+		} finally {
+			if (lock != null) {
+				lock.unlock(ByteSequence.from(key.getBytes()));
+			}
+		}
+		return unmarshallExchange(camelContext, newHolder);
+	}
+
+	@Override
+	public Set<String> scan(CamelContext camelContext) {
+		if (useRecovery) {
+			LOG.trace("Scanning for exchanges to recover in {} context", camelContext.getName());
+			CompletableFuture<GetResponse> completableGetResponse = kvClient.get(
+					ByteSequence.from(persistencePrefixName.getBytes()),
+					GetOption.newBuilder().withPrefix(ByteSequence.from(persistencePrefixName.getBytes())).build());
+			Set<String> scanned = Collections.unmodifiableSet(new TreeSet<>());
+			try {
+				GetResponse getResponse = completableGetResponse.get();
+				Set<String> keys = new TreeSet<>();
+				getResponse.getKvs().forEach(kv -> keys.add(new String(kv.getKey().getBytes())));
+				scanned = Collections.unmodifiableSet(keys);
+				LOG.trace("Found {} keys for exchanges to recover in {} context", scanned.size(),
+						camelContext.getName());
+			} catch (InterruptedException | ExecutionException e) {
+				LOG.error(e.getMessage(), e);
+			}
+			return scanned;
+		} else {
+			LOG.warn(
+					"What for to run recovery scans in {} context while prefix {} is running in non-recoverable aggregation repository mode?!",
+					camelContext.getName(), prefixName);
+			return Collections.emptySet();
+		}
+	}
+
+	@Override
+	public Exchange recover(CamelContext camelContext, String exchangeId) {
+		LOG.trace("Recovering an Exchange with ID {}.", exchangeId);
+		CompletableFuture<GetResponse> completableResponse = kvClient
+				.get(ByteSequence.from(String.format("%s/%s", persistencePrefixName, exchangeId).getBytes()));
+		try {
+			GetResponse getResponse = completableResponse.get();
+			byte[] data = getResponse.getKvs().get(0).getValue().getBytes();
+			ByteArrayInputStream in = new ByteArrayInputStream(data);
+			ObjectInputStream is = new ObjectInputStream(in);
+			DefaultExchangeHolder holder = (DefaultExchangeHolder) is.readObject();
+			return useRecovery ? unmarshallExchange(camelContext, holder) : null;
+		} catch (InterruptedException | ExecutionException | IOException | ClassNotFoundException e) {
+			LOG.error(e.getMessage(), e);
+		}
+		return null;
+	}
+
+	@Override
+	public void setRecoveryInterval(long interval, TimeUnit timeUnit) {
+		this.recoveryInterval = timeUnit.toMillis(interval);
+	}
+
+	@Override
+	public void setRecoveryInterval(long interval) {
+		this.recoveryInterval = interval;
+	}
+
+	@Override
+	public long getRecoveryIntervalInMillis() {
+		return recoveryInterval;
+	}
+
+	@Override
+	public void setUseRecovery(boolean useRecovery) {
+		this.useRecovery = useRecovery;
+	}
+
+	@Override
+	public boolean isUseRecovery() {
+		return useRecovery;
+	}
+
+	@Override
+	public void setDeadLetterUri(String deadLetterUri) {
+		this.deadLetterChannel = deadLetterUri;
+	}
+
+	@Override
+	public String getDeadLetterUri() {
+		return deadLetterChannel;
+	}
+
+	@Override
+	public void setMaximumRedeliveries(int maximumRedeliveries) {
+		this.maximumRedeliveries = maximumRedeliveries;
+	}
+
+	@Override
+	public int getMaximumRedeliveries() {
+		return maximumRedeliveries;
+	}
+
+	@Override
+	public Exchange get(CamelContext camelContext, String key) {
+		CompletableFuture<GetResponse> completableResponse = kvClient
+				.get(ByteSequence.from(String.format("%s/%s", prefixName, key).getBytes()));
+		try {
+			GetResponse getResponse = completableResponse.get();
+			byte[] data = getResponse.getKvs().get(0).getValue().getBytes();
+			ByteArrayInputStream in = new ByteArrayInputStream(data);
+			ObjectInputStream is = new ObjectInputStream(in);
+			DefaultExchangeHolder holder = (DefaultExchangeHolder) is.readObject();
+			return unmarshallExchange(camelContext, holder);
+		} catch (InterruptedException | ExecutionException | IOException | ClassNotFoundException e) {
+			LOG.error(e.getMessage(), e);
+		}
+		return null;
+	}
+
+	public boolean isAllowSerializedHeaders() {
+		return allowSerializedHeaders;
+	}
+
+	public void setAllowSerializedHeaders(boolean allowSerializedHeaders) {
+		this.allowSerializedHeaders = allowSerializedHeaders;
+	}
+
+	@Override
+	public void remove(CamelContext camelContext, String key, Exchange exchange) {
+		DefaultExchangeHolder holder = DefaultExchangeHolder.marshal(exchange, true, allowSerializedHeaders);
+		if (optimistic) {
+			LOG.trace("Removing an exchange with ID {} for key {} in an optimistic manner.", exchange.getExchangeId(),
+					key);
+			CompletableFuture<DeleteResponse> completableDeleteResponse = kvClient
+					.delete(ByteSequence.from(String.format("%s/%s", prefixName, key).getBytes()));
+			try {
+				DeleteResponse deleteResponse = completableDeleteResponse.get();
+				if (deleteResponse.getDeleted() == 0) {
+					LOG.error(
+							"Optimistic locking failed for exchange with key {}: kvClient.delete removed no Exchanges, while it's expected to remove one.",
+							key);
+					throw new OptimisticLockingException();
+				}
+			} catch (InterruptedException | ExecutionException e) {
+				LOG.error(e.getMessage(), e);
+			}
+			LOG.trace("Removed an exchange with ID {} for key {} in an optimistic manner.", exchange.getExchangeId(),
+					key);
+			if (useRecovery) {
+				LOG.trace(
+						"Putting an exchange with ID {} for key {} into a recoverable storage in an optimistic manner.",
+						exchange.getExchangeId(), key);
+				try {
+					ByteArrayOutputStream bos = new ByteArrayOutputStream();
+					ObjectOutputStream oos = new ObjectOutputStream(bos);
+					oos.writeObject(holder);
+					oos.flush();
+					byte[] data = bos.toByteArray();
+					CompletableFuture<PutResponse> completablePutResponse = kvClient.put(
+							ByteSequence.from(String.format("%s/%s", persistencePrefixName, key).getBytes()),
+							ByteSequence.from(data));
+					completablePutResponse.get();
+					LOG.trace(
+							"Put an exchange with ID {} for key {} into a recoverable storage in an optimistic manner.",
+							exchange.getExchangeId(), key);
+				} catch (IOException | InterruptedException | ExecutionException e) {
+					LOG.error(e.getMessage(), e);
+				}
+
+			}
+		} else {
+			if (useRecovery) {
+				LOG.trace("Removing an exchange with ID {} for key {} in a thread-safe manner.",
+						exchange.getExchangeId(), key);
+				Txn transaction = kvClient.txn();
+				try {
+					CompletableFuture<GetResponse> completableResponse = kvClient
+							.get(ByteSequence.from(String.format("%s/%s", prefixName, key).getBytes()));
+					GetResponse getResponse = completableResponse.get();
+					byte[] data = getResponse.getKvs().get(0).getValue().getBytes();
+					ByteArrayInputStream in = new ByteArrayInputStream(data);
+					ObjectInputStream is = new ObjectInputStream(in);
+					DefaultExchangeHolder removedHolder = (DefaultExchangeHolder) is.readObject();
+					ByteArrayOutputStream bos = new ByteArrayOutputStream();
+					ObjectOutputStream oos = new ObjectOutputStream(bos);
+					oos.writeObject(removedHolder);
+					oos.flush();
+					data = bos.toByteArray();
+					transaction
+							.If(new Cmp(ByteSequence.from(String.format("%s/%s", prefixName, key).getBytes()),
+									Cmp.Op.EQUAL,
+									CmpTarget.value(
+											ByteSequence.from(String.format("%s/%s", prefixName, key).getBytes()))))
+							.Then(Op.delete(ByteSequence.from(String.format("%s/%s", prefixName, key).getBytes()),
+									DeleteOption.DEFAULT),
+									Op.put(ByteSequence
+											.from(String.format("%s/%s", persistencePrefixName, key).getBytes()),
+											ByteSequence.from(data), PutOption.DEFAULT))
+							.commit();
+					LOG.trace("Removed an exchange with ID {} for key {} in a thread-safe manner.",
+							exchange.getExchangeId(), key);
+					LOG.trace(
+							"Put an exchange with ID {} for key {} into a recoverable storage in a thread-safe manner.",
+							exchange.getExchangeId(), key);
+				} catch (Throwable throwable) {
+					throw new RuntimeException(throwable);
+				}
+			} else {
+				CompletableFuture<DeleteResponse> completableDeleteResponse = kvClient
+						.delete(ByteSequence.from(String.format("%s/%s", prefixName, key).getBytes()));
+				try {
+					completableDeleteResponse.get();
+				} catch (InterruptedException | ExecutionException e) {
+					LOG.error(e.getMessage(), e);
+				}
+			}
+		}
+	}
+
+	@Override
+	public void confirm(CamelContext camelContext, String exchangeId) {
+		LOG.trace("Confirming an exchange with ID {}.", exchangeId);
+		if (useRecovery) {
+			CompletableFuture<DeleteResponse> completableDeleteResponse = kvClient
+					.delete(ByteSequence.from(String.format("%s/%s", persistencePrefixName, exchangeId).getBytes()));
+			try {
+				completableDeleteResponse.get();
+			} catch (InterruptedException | ExecutionException e) {
+				LOG.error(e.getMessage(), e);
+			}
+		}
+	}
+
+	@Override
+	public Set<String> getKeys() {
+		CompletableFuture<GetResponse> completableGetResponse = kvClient.get(ByteSequence.from(prefixName.getBytes()),
+				GetOption.newBuilder().withRange(ByteSequence.from(prefixName.getBytes())).build());
+		Set<String> scanned = Collections.unmodifiableSet(new TreeSet<>());
+		try {
+			GetResponse getResponse = completableGetResponse.get();
+			Set<String> keys = new TreeSet<>();
+			getResponse.getKvs().forEach(kv -> keys.add(new String(kv.getKey().getBytes())));
+			scanned = Collections.unmodifiableSet(keys);
+		} catch (InterruptedException | ExecutionException e) {
+			LOG.error(e.getMessage(), e);
+		}
+		return scanned;
+	}
+
+	@Override
+	protected void doStart() throws Exception {
+		if (maximumRedeliveries < 0) {

Review comment:
       Validation can be moved to `onInit` to check correctness of the parameters early

##########
File path: components/camel-etcd3/src/main/java/org/apache/camel/component/etcd3/processor/aggregate/Etcd3AggregationRepository.java
##########
@@ -0,0 +1,459 @@
+/*
+ * 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.etcd3.processor.aggregate;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.util.Collections;
+import java.util.List;
+import java.util.Set;
+import java.util.TreeSet;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.Exchange;
+import org.apache.camel.impl.DefaultExchange;
+import org.apache.camel.impl.DefaultExchangeHolder;
+import org.apache.camel.spi.OptimisticLockingAggregationRepository;
+import org.apache.camel.spi.RecoverableAggregationRepository;
+import org.apache.camel.support.ServiceSupport;
+import org.apache.camel.util.StringHelper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import io.etcd.jetcd.ByteSequence;
+import io.etcd.jetcd.Client;
+import io.etcd.jetcd.KV;
+import io.etcd.jetcd.KeyValue;
+import io.etcd.jetcd.Lock;
+import io.etcd.jetcd.Txn;
+import io.etcd.jetcd.kv.DeleteResponse;
+import io.etcd.jetcd.kv.GetResponse;
+import io.etcd.jetcd.kv.PutResponse;
+import io.etcd.jetcd.op.Cmp;
+import io.etcd.jetcd.op.CmpTarget;
+import io.etcd.jetcd.op.Op;
+import io.etcd.jetcd.options.DeleteOption;
+import io.etcd.jetcd.options.GetOption;
+import io.etcd.jetcd.options.PutOption;
+
+public class Etcd3AggregationRepository extends ServiceSupport
+		implements RecoverableAggregationRepository, OptimisticLockingAggregationRepository {
+	private static final Logger LOG = LoggerFactory.getLogger(Etcd3AggregationRepository.class.getName());
+	private static final String COMPLETED_SUFFIX = "-completed";
+
+	private boolean optimistic;
+	private boolean useRecovery = true;
+	private String endpoint;
+	private Client client;
+	private KV kvClient;
+	private String prefixName;
+	private String persistencePrefixName;
+	private String deadLetterChannel;
+	private long recoveryInterval = 5000;
+	private int maximumRedeliveries = 3;
+	private boolean allowSerializedHeaders;
+
+	public Etcd3AggregationRepository(final String prefixName, final String endpoint) {
+		this.prefixName = prefixName;
+		this.persistencePrefixName = String.format("%s%s", prefixName, COMPLETED_SUFFIX);
+		this.optimistic = false;
+		this.endpoint = endpoint;
+	}
+
+	public Etcd3AggregationRepository(final String prefixName, final String persistencePrefixName,
+			final String endpoint) {
+		this.prefixName = prefixName;
+		this.persistencePrefixName = persistencePrefixName;
+		this.optimistic = false;
+		this.endpoint = endpoint;
+	}
+
+	public Etcd3AggregationRepository(final String prefixName, final String endpoint, boolean optimistic) {
+		this(prefixName, endpoint);
+		this.optimistic = optimistic;
+	}
+
+	public Etcd3AggregationRepository(final String repositoryName, final String persistentRepositoryName,
+			final String endpoint, boolean optimistic) {
+		this(repositoryName, persistentRepositoryName, endpoint);
+		this.optimistic = optimistic;
+	}
+
+	@Override
+	public Exchange add(CamelContext camelContext, String key, Exchange oldExchange, Exchange newExchange)
+			throws OptimisticLockingException {
+		if (!optimistic) {
+			throw new UnsupportedOperationException();
+		}
+		LOG.trace("Adding an Exchange with ID {} for key {} in an optimistic manner.", newExchange.getExchangeId(),
+				key);
+		try {
+			if (oldExchange == null) {
+				DefaultExchangeHolder holder = DefaultExchangeHolder.marshal(newExchange, true, allowSerializedHeaders);
+				CompletableFuture<GetResponse> completableGetResponse = kvClient
+						.get(ByteSequence.from(String.format("%s/%s", prefixName, key).getBytes()));
+				GetResponse getResponse = completableGetResponse.get();
+				List<KeyValue> keyValues = getResponse.getKvs();
+				if (keyValues.isEmpty()) {
+					ByteArrayOutputStream bos = new ByteArrayOutputStream();
+					ObjectOutputStream oos = new ObjectOutputStream(bos);
+					oos.writeObject(holder);
+					oos.flush();
+					byte[] data = bos.toByteArray();
+					CompletableFuture<PutResponse> completablePutResponse = kvClient.put(
+							ByteSequence.from(String.format("%s/%s", prefixName, key).getBytes()),
+							ByteSequence.from(data));
+					completablePutResponse.get();
+				} else {
+					byte[] data = keyValues.get(0).getValue().getBytes();
+					ByteArrayInputStream in = new ByteArrayInputStream(data);
+					ObjectInputStream is = new ObjectInputStream(in);
+					DefaultExchangeHolder misbehaviorHolder = (DefaultExchangeHolder) is.readObject();
+					Exchange misbehaviorEx = unmarshallExchange(camelContext, misbehaviorHolder);
+					LOG.error(
+							"Optimistic locking failed for exchange with key {}: kvClient.get returned Exchange with ID {}, while it's expected no exchanges to be returned",
+							key, misbehaviorEx != null ? misbehaviorEx.getExchangeId() : "<null>");
+					throw new OptimisticLockingException();
+				}
+			} else {
+				DefaultExchangeHolder newHolder = DefaultExchangeHolder.marshal(newExchange, true,
+						allowSerializedHeaders);
+				ByteArrayOutputStream bos = new ByteArrayOutputStream();
+				ObjectOutputStream oos = new ObjectOutputStream(bos);
+				oos.writeObject(newHolder);
+				oos.flush();
+				byte[] data = bos.toByteArray();
+				CompletableFuture<DeleteResponse> completableDeleteResponse = kvClient
+						.delete(ByteSequence.from(String.format("%s/%s", prefixName, key).getBytes()));
+				DeleteResponse deleteResponse = completableDeleteResponse.get();
+				if (deleteResponse.getDeleted() == 0) {
+					LOG.error(
+							"Optimistic locking failed for exchange with key {}: kvClient.get returned no Exchanges, while it's expected to replace one",
+							key);
+					throw new OptimisticLockingException();
+				}
+				CompletableFuture<PutResponse> completablePutResponse = kvClient.put(
+						ByteSequence.from(String.format("%s/%s", prefixName, key).getBytes()), ByteSequence.from(data));
+				completablePutResponse.get();
+			}
+		} catch (InterruptedException | ExecutionException | IOException | ClassNotFoundException e) {
+			LOG.error(e.getMessage(), e);
+			throw new OptimisticLockingException();
+		}
+		LOG.trace("Added an Exchange with ID {} for key {} in optimistic manner.", newExchange.getExchangeId(), key);
+		return oldExchange;
+	}
+
+	@Override
+	public Exchange add(CamelContext camelContext, String key, Exchange exchange) {
+		if (optimistic) {
+			throw new UnsupportedOperationException();
+		}
+		LOG.trace("Adding an Exchange with ID {} for key {} in a thread-safe manner.", exchange.getExchangeId(), key);
+		Lock lock = null;
+		DefaultExchangeHolder newHolder = DefaultExchangeHolder.marshal(exchange, true, allowSerializedHeaders);
+		try {
+			lock = client.getLockClient();
+			lock.lock(ByteSequence.from(key.getBytes()), 60000);
+			ByteArrayOutputStream bos = new ByteArrayOutputStream();
+			ObjectOutputStream oos = new ObjectOutputStream(bos);
+			oos.writeObject(newHolder);
+			oos.flush();
+			byte[] data = bos.toByteArray();
+			CompletableFuture<PutResponse> completablePutResponse = kvClient.put(
+					ByteSequence.from(String.format("%s/%s", prefixName, key).getBytes()), ByteSequence.from(data));
+			completablePutResponse.get();
+		} catch (InterruptedException | ExecutionException | IOException e) {
+			LOG.error(e.getMessage(), e);
+		} finally {
+			if (lock != null) {
+				lock.unlock(ByteSequence.from(key.getBytes()));
+			}
+		}
+		return unmarshallExchange(camelContext, newHolder);
+	}
+
+	@Override
+	public Set<String> scan(CamelContext camelContext) {
+		if (useRecovery) {
+			LOG.trace("Scanning for exchanges to recover in {} context", camelContext.getName());
+			CompletableFuture<GetResponse> completableGetResponse = kvClient.get(
+					ByteSequence.from(persistencePrefixName.getBytes()),
+					GetOption.newBuilder().withPrefix(ByteSequence.from(persistencePrefixName.getBytes())).build());
+			Set<String> scanned = Collections.unmodifiableSet(new TreeSet<>());
+			try {
+				GetResponse getResponse = completableGetResponse.get();
+				Set<String> keys = new TreeSet<>();
+				getResponse.getKvs().forEach(kv -> keys.add(new String(kv.getKey().getBytes())));
+				scanned = Collections.unmodifiableSet(keys);
+				LOG.trace("Found {} keys for exchanges to recover in {} context", scanned.size(),
+						camelContext.getName());
+			} catch (InterruptedException | ExecutionException e) {
+				LOG.error(e.getMessage(), e);
+			}
+			return scanned;
+		} else {
+			LOG.warn(
+					"What for to run recovery scans in {} context while prefix {} is running in non-recoverable aggregation repository mode?!",
+					camelContext.getName(), prefixName);
+			return Collections.emptySet();
+		}
+	}
+
+	@Override
+	public Exchange recover(CamelContext camelContext, String exchangeId) {
+		LOG.trace("Recovering an Exchange with ID {}.", exchangeId);
+		CompletableFuture<GetResponse> completableResponse = kvClient
+				.get(ByteSequence.from(String.format("%s/%s", persistencePrefixName, exchangeId).getBytes()));
+		try {
+			GetResponse getResponse = completableResponse.get();
+			byte[] data = getResponse.getKvs().get(0).getValue().getBytes();
+			ByteArrayInputStream in = new ByteArrayInputStream(data);
+			ObjectInputStream is = new ObjectInputStream(in);
+			DefaultExchangeHolder holder = (DefaultExchangeHolder) is.readObject();
+			return useRecovery ? unmarshallExchange(camelContext, holder) : null;
+		} catch (InterruptedException | ExecutionException | IOException | ClassNotFoundException e) {
+			LOG.error(e.getMessage(), e);
+		}
+		return null;
+	}
+
+	@Override
+	public void setRecoveryInterval(long interval, TimeUnit timeUnit) {
+		this.recoveryInterval = timeUnit.toMillis(interval);
+	}
+
+	@Override
+	public void setRecoveryInterval(long interval) {
+		this.recoveryInterval = interval;
+	}
+
+	@Override
+	public long getRecoveryIntervalInMillis() {
+		return recoveryInterval;
+	}
+
+	@Override
+	public void setUseRecovery(boolean useRecovery) {
+		this.useRecovery = useRecovery;
+	}
+
+	@Override
+	public boolean isUseRecovery() {
+		return useRecovery;
+	}
+
+	@Override
+	public void setDeadLetterUri(String deadLetterUri) {
+		this.deadLetterChannel = deadLetterUri;
+	}
+
+	@Override
+	public String getDeadLetterUri() {
+		return deadLetterChannel;
+	}
+
+	@Override
+	public void setMaximumRedeliveries(int maximumRedeliveries) {
+		this.maximumRedeliveries = maximumRedeliveries;
+	}
+
+	@Override
+	public int getMaximumRedeliveries() {
+		return maximumRedeliveries;
+	}
+
+	@Override
+	public Exchange get(CamelContext camelContext, String key) {
+		CompletableFuture<GetResponse> completableResponse = kvClient
+				.get(ByteSequence.from(String.format("%s/%s", prefixName, key).getBytes()));
+		try {
+			GetResponse getResponse = completableResponse.get();
+			byte[] data = getResponse.getKvs().get(0).getValue().getBytes();
+			ByteArrayInputStream in = new ByteArrayInputStream(data);
+			ObjectInputStream is = new ObjectInputStream(in);
+			DefaultExchangeHolder holder = (DefaultExchangeHolder) is.readObject();
+			return unmarshallExchange(camelContext, holder);
+		} catch (InterruptedException | ExecutionException | IOException | ClassNotFoundException e) {
+			LOG.error(e.getMessage(), e);
+		}
+		return null;
+	}
+
+	public boolean isAllowSerializedHeaders() {
+		return allowSerializedHeaders;
+	}
+
+	public void setAllowSerializedHeaders(boolean allowSerializedHeaders) {
+		this.allowSerializedHeaders = allowSerializedHeaders;
+	}
+
+	@Override
+	public void remove(CamelContext camelContext, String key, Exchange exchange) {
+		DefaultExchangeHolder holder = DefaultExchangeHolder.marshal(exchange, true, allowSerializedHeaders);
+		if (optimistic) {
+			LOG.trace("Removing an exchange with ID {} for key {} in an optimistic manner.", exchange.getExchangeId(),
+					key);
+			CompletableFuture<DeleteResponse> completableDeleteResponse = kvClient
+					.delete(ByteSequence.from(String.format("%s/%s", prefixName, key).getBytes()));
+			try {
+				DeleteResponse deleteResponse = completableDeleteResponse.get();
+				if (deleteResponse.getDeleted() == 0) {
+					LOG.error(
+							"Optimistic locking failed for exchange with key {}: kvClient.delete removed no Exchanges, while it's expected to remove one.",
+							key);
+					throw new OptimisticLockingException();
+				}
+			} catch (InterruptedException | ExecutionException e) {
+				LOG.error(e.getMessage(), e);
+			}
+			LOG.trace("Removed an exchange with ID {} for key {} in an optimistic manner.", exchange.getExchangeId(),
+					key);
+			if (useRecovery) {
+				LOG.trace(
+						"Putting an exchange with ID {} for key {} into a recoverable storage in an optimistic manner.",
+						exchange.getExchangeId(), key);
+				try {
+					ByteArrayOutputStream bos = new ByteArrayOutputStream();
+					ObjectOutputStream oos = new ObjectOutputStream(bos);
+					oos.writeObject(holder);
+					oos.flush();

Review comment:
       seems like serialize/deserialize of the exchange holder is quite common so you may think to extract the logic in some methods 




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org