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

orpiske 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 e63955f5d53 CAMEL-18127: added adapter auto-configuration for Couchbase
e63955f5d53 is described below

commit e63955f5d536b7b2aad9423db61f5fb60e1ac1e9
Author: Otavio Rodolfo Piske <[email protected]>
AuthorDate: Thu Jun 9 18:21:28 2022 +0200

    CAMEL-18127: added adapter auto-configuration for Couchbase
---
 .../apache/camel/catalog/components/couchbase.json |  3 +-
 .../camel/component/couchbase/couchbase.json       |  3 +-
 .../component/couchbase/CouchbaseConstants.java    |  3 +
 .../component/couchbase/CouchbaseConsumer.java     | 12 +++-
 .../couchbase/CouchbaseResumeAdapter.java          |  8 +--
 .../support/DefaultCouchbaseResumeAdapter.java     | 72 ++++++++++++++++++++++
 .../org/apache/camel/resume/adapter.properties     | 19 ++++++
 .../integration/ConsumeResumeStrategyIT.java       | 23 ++++---
 8 files changed, 126 insertions(+), 17 deletions(-)

diff --git 
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/couchbase.json
 
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/couchbase.json
index c7a9e26d6c5..304417b8a09 100644
--- 
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/couchbase.json
+++ 
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/couchbase.json
@@ -31,7 +31,8 @@
     "CCB_ID": { "kind": "header", "displayName": "", "group": "common", 
"label": "", "required": false, "javaType": "String", "deprecated": false, 
"deprecationNote": "", "autowired": false, "secret": false, "description": "The 
document id", "constantName": 
"org.apache.camel.component.couchbase.CouchbaseConstants#HEADER_ID" },
     "CCB_TTL": { "kind": "header", "displayName": "", "group": "producer", 
"label": "producer", "required": false, "javaType": "String", "deprecated": 
false, "deprecationNote": "", "autowired": false, "secret": false, 
"description": "The expiry for the document in seconds", "constantName": 
"org.apache.camel.component.couchbase.CouchbaseConstants#HEADER_TTL" },
     "CCB_DDN": { "kind": "header", "displayName": "", "group": "consumer", 
"label": "consumer", "required": false, "javaType": "String", "deprecated": 
false, "deprecationNote": "", "autowired": false, "secret": false, 
"description": "The design document name", "constantName": 
"org.apache.camel.component.couchbase.CouchbaseConstants#HEADER_DESIGN_DOCUMENT_NAME"
 },
-    "CCB_VN": { "kind": "header", "displayName": "", "group": "consumer", 
"label": "consumer", "required": false, "javaType": "String", "deprecated": 
false, "deprecationNote": "", "autowired": false, "secret": false, 
"description": "The view name", "constantName": 
"org.apache.camel.component.couchbase.CouchbaseConstants#HEADER_VIEWNAME" }
+    "CCB_VN": { "kind": "header", "displayName": "", "group": "consumer", 
"label": "consumer", "required": false, "javaType": "String", "deprecated": 
false, "deprecationNote": "", "autowired": false, "secret": false, 
"description": "The view name", "constantName": 
"org.apache.camel.component.couchbase.CouchbaseConstants#HEADER_VIEWNAME" },
+    "CamelCqlResumeQuery": { "kind": "header", "displayName": "", "group": 
"consumer", "label": "consumer", "required": false, "javaType": "String", 
"deprecated": false, "deprecationNote": "", "autowired": false, "secret": 
false, "description": "The resume action to execute when resuming.", 
"constantName": 
"org.apache.camel.component.couchbase.CouchbaseConstants#COUCHBASE_RESUME_ACTION"
 }
   },
   "properties": {
     "protocol": { "kind": "path", "displayName": "Protocol", "group": 
"common", "label": "", "required": true, "type": "string", "javaType": 
"java.lang.String", "deprecated": false, "deprecationNote": "", "autowired": 
false, "secret": false, "description": "The protocol to use" },
diff --git 
a/components/camel-couchbase/src/generated/resources/org/apache/camel/component/couchbase/couchbase.json
 
b/components/camel-couchbase/src/generated/resources/org/apache/camel/component/couchbase/couchbase.json
index c7a9e26d6c5..304417b8a09 100644
--- 
a/components/camel-couchbase/src/generated/resources/org/apache/camel/component/couchbase/couchbase.json
+++ 
b/components/camel-couchbase/src/generated/resources/org/apache/camel/component/couchbase/couchbase.json
@@ -31,7 +31,8 @@
     "CCB_ID": { "kind": "header", "displayName": "", "group": "common", 
"label": "", "required": false, "javaType": "String", "deprecated": false, 
"deprecationNote": "", "autowired": false, "secret": false, "description": "The 
document id", "constantName": 
"org.apache.camel.component.couchbase.CouchbaseConstants#HEADER_ID" },
     "CCB_TTL": { "kind": "header", "displayName": "", "group": "producer", 
"label": "producer", "required": false, "javaType": "String", "deprecated": 
false, "deprecationNote": "", "autowired": false, "secret": false, 
"description": "The expiry for the document in seconds", "constantName": 
"org.apache.camel.component.couchbase.CouchbaseConstants#HEADER_TTL" },
     "CCB_DDN": { "kind": "header", "displayName": "", "group": "consumer", 
"label": "consumer", "required": false, "javaType": "String", "deprecated": 
false, "deprecationNote": "", "autowired": false, "secret": false, 
"description": "The design document name", "constantName": 
"org.apache.camel.component.couchbase.CouchbaseConstants#HEADER_DESIGN_DOCUMENT_NAME"
 },
-    "CCB_VN": { "kind": "header", "displayName": "", "group": "consumer", 
"label": "consumer", "required": false, "javaType": "String", "deprecated": 
false, "deprecationNote": "", "autowired": false, "secret": false, 
"description": "The view name", "constantName": 
"org.apache.camel.component.couchbase.CouchbaseConstants#HEADER_VIEWNAME" }
+    "CCB_VN": { "kind": "header", "displayName": "", "group": "consumer", 
"label": "consumer", "required": false, "javaType": "String", "deprecated": 
false, "deprecationNote": "", "autowired": false, "secret": false, 
"description": "The view name", "constantName": 
"org.apache.camel.component.couchbase.CouchbaseConstants#HEADER_VIEWNAME" },
+    "CamelCqlResumeQuery": { "kind": "header", "displayName": "", "group": 
"consumer", "label": "consumer", "required": false, "javaType": "String", 
"deprecated": false, "deprecationNote": "", "autowired": false, "secret": 
false, "description": "The resume action to execute when resuming.", 
"constantName": 
"org.apache.camel.component.couchbase.CouchbaseConstants#COUCHBASE_RESUME_ACTION"
 }
   },
   "properties": {
     "protocol": { "kind": "path", "displayName": "Protocol", "group": 
"common", "label": "", "required": true, "type": "string", "javaType": 
"java.lang.String", "deprecated": false, "deprecationNote": "", "autowired": 
false, "secret": false, "description": "The protocol to use" },
diff --git 
a/components/camel-couchbase/src/main/java/org/apache/camel/component/couchbase/CouchbaseConstants.java
 
b/components/camel-couchbase/src/main/java/org/apache/camel/component/couchbase/CouchbaseConstants.java
index a637b8be351..ad1ec3ad3f1 100644
--- 
a/components/camel-couchbase/src/main/java/org/apache/camel/component/couchbase/CouchbaseConstants.java
+++ 
b/components/camel-couchbase/src/main/java/org/apache/camel/component/couchbase/CouchbaseConstants.java
@@ -42,6 +42,9 @@ public interface CouchbaseConstants {
     @Metadata(label = "consumer", description = "The view name", javaType = 
"String")
     String HEADER_VIEWNAME = "CCB_VN";
 
+    @Metadata(label = "consumer", description = "The resume action to execute 
when resuming.", javaType = "String")
+    String COUCHBASE_RESUME_ACTION = "CamelCqlResumeQuery";
+
     int DEFAULT_PRODUCER_RETRIES = 2;
     int DEFAULT_PAUSE_BETWEEN_RETRIES = 5000;
     int DEFAULT_COUCHBASE_PORT = 8091;
diff --git 
a/components/camel-couchbase/src/main/java/org/apache/camel/component/couchbase/CouchbaseConsumer.java
 
b/components/camel-couchbase/src/main/java/org/apache/camel/component/couchbase/CouchbaseConsumer.java
index a1dc66deefd..1de38c3ac39 100644
--- 
a/components/camel-couchbase/src/main/java/org/apache/camel/component/couchbase/CouchbaseConsumer.java
+++ 
b/components/camel-couchbase/src/main/java/org/apache/camel/component/couchbase/CouchbaseConsumer.java
@@ -26,12 +26,15 @@ import com.couchbase.client.java.view.ViewResult;
 import com.couchbase.client.java.view.ViewRow;
 import org.apache.camel.Exchange;
 import org.apache.camel.Processor;
+import org.apache.camel.resume.ResumeAction;
 import org.apache.camel.resume.ResumeAware;
 import org.apache.camel.resume.ResumeStrategy;
 import org.apache.camel.support.DefaultScheduledPollConsumer;
+import org.apache.camel.util.ObjectHelper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import static 
org.apache.camel.component.couchbase.CouchbaseConstants.COUCHBASE_RESUME_ACTION;
 import static 
org.apache.camel.component.couchbase.CouchbaseConstants.HEADER_DESIGN_DOCUMENT_NAME;
 import static 
org.apache.camel.component.couchbase.CouchbaseConstants.HEADER_ID;
 import static 
org.apache.camel.component.couchbase.CouchbaseConstants.HEADER_KEY;
@@ -96,11 +99,18 @@ public class CouchbaseConsumer extends 
DefaultScheduledPollConsumer implements R
         super.doStart();
 
         if (resumeStrategy != null) {
+            LOG.debug("Loading the resume cache");
+            resumeStrategy.loadCache();
+
             LOG.info("Couchbase consumer running with resume strategy 
enabled");
 
             CouchbaseResumeAdapter resumeAdapter = 
resumeStrategy.getAdapter(CouchbaseResumeAdapter.class);
             if (resumeAdapter != null) {
-                resumeAdapter.setBucket(bucket);
+                ResumeAction action = (ResumeAction) 
getEndpoint().getCamelContext().getRegistry()
+                        .lookupByName(COUCHBASE_RESUME_ACTION);
+                ObjectHelper.notNull(action, "The resume action cannot be 
null", this);
+
+                resumeAdapter.setResumeAction(action);
                 resumeAdapter.resume();
             }
         }
diff --git 
a/components/camel-couchbase/src/main/java/org/apache/camel/component/couchbase/CouchbaseResumeAdapter.java
 
b/components/camel-couchbase/src/main/java/org/apache/camel/component/couchbase/CouchbaseResumeAdapter.java
index b7e87ec6a47..56bd4747b23 100644
--- 
a/components/camel-couchbase/src/main/java/org/apache/camel/component/couchbase/CouchbaseResumeAdapter.java
+++ 
b/components/camel-couchbase/src/main/java/org/apache/camel/component/couchbase/CouchbaseResumeAdapter.java
@@ -17,7 +17,7 @@
 
 package org.apache.camel.component.couchbase;
 
-import com.couchbase.client.java.Bucket;
+import org.apache.camel.resume.ResumeAction;
 import org.apache.camel.resume.ResumeAdapter;
 
 /**
@@ -26,9 +26,9 @@ import org.apache.camel.resume.ResumeAdapter;
 public interface CouchbaseResumeAdapter extends ResumeAdapter {
 
     /**
-     * Sets the bucket in use
+     * Sets an action to be executed whenever the resume happens
      * 
-     * @param bucket the bucket in use
+     * @param resumeAction
      */
-    void setBucket(Bucket bucket);
+    void setResumeAction(ResumeAction resumeAction);
 }
diff --git 
a/components/camel-couchbase/src/main/java/org/apache/camel/component/couchbase/consumer/support/DefaultCouchbaseResumeAdapter.java
 
b/components/camel-couchbase/src/main/java/org/apache/camel/component/couchbase/consumer/support/DefaultCouchbaseResumeAdapter.java
new file mode 100644
index 00000000000..e96222c6e2e
--- /dev/null
+++ 
b/components/camel-couchbase/src/main/java/org/apache/camel/component/couchbase/consumer/support/DefaultCouchbaseResumeAdapter.java
@@ -0,0 +1,72 @@
+/*
+ * 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.couchbase.consumer.support;
+
+import java.nio.ByteBuffer;
+
+import org.apache.camel.component.couchbase.CouchbaseResumeAdapter;
+import org.apache.camel.resume.Cacheable;
+import org.apache.camel.resume.Deserializable;
+import org.apache.camel.resume.Offset;
+import org.apache.camel.resume.OffsetKey;
+import org.apache.camel.resume.ResumeAction;
+import org.apache.camel.resume.cache.ResumeCache;
+
+public class DefaultCouchbaseResumeAdapter implements CouchbaseResumeAdapter, 
Cacheable, Deserializable {
+    private ResumeAction resumeAction;
+    private ResumeCache<Object> cache;
+
+    @Override
+    public void setResumeAction(ResumeAction resumeAction) {
+        this.resumeAction = resumeAction;
+    }
+
+    @Override
+    public void resume() {
+        cache.forEach(resumeAction::evalEntry);
+    }
+
+    private boolean add(Object key, Object offset) {
+        cache.add(key, offset);
+
+        return true;
+    }
+
+    @Override
+    public boolean add(OffsetKey<?> key, Offset<?> offset) {
+        return add(key.getValue(), offset.getValue());
+    }
+
+    @Override
+    public void setCache(ResumeCache<?> cache) {
+        this.cache = (ResumeCache<Object>) cache;
+    }
+
+    @Override
+    public ResumeCache<?> getCache() {
+        return cache;
+    }
+
+    @Override
+    public boolean deserialize(ByteBuffer keyBuffer, ByteBuffer valueBuffer) {
+        Object key = deserializeObject(keyBuffer);
+        Object value = deserializeObject(valueBuffer);
+
+        return add(key, value);
+    }
+}
diff --git 
a/components/camel-couchbase/src/main/resources/org/apache/camel/resume/adapter.properties
 
b/components/camel-couchbase/src/main/resources/org/apache/camel/resume/adapter.properties
new file mode 100644
index 00000000000..5a7ec013b1e
--- /dev/null
+++ 
b/components/camel-couchbase/src/main/resources/org/apache/camel/resume/adapter.properties
@@ -0,0 +1,19 @@
+## ---------------------------------------------------------------------------
+## 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.
+## ---------------------------------------------------------------------------
+
+
+adapterClass=org.apache.camel.component.couchbase.consumer.support.DefaultCouchbaseResumeAdapter
diff --git 
a/components/camel-couchbase/src/test/java/org/apache/camel/component/couchbase/integration/ConsumeResumeStrategyIT.java
 
b/components/camel-couchbase/src/test/java/org/apache/camel/component/couchbase/integration/ConsumeResumeStrategyIT.java
index cacab287d9d..46ca0deb8cc 100644
--- 
a/components/camel-couchbase/src/test/java/org/apache/camel/component/couchbase/integration/ConsumeResumeStrategyIT.java
+++ 
b/components/camel-couchbase/src/test/java/org/apache/camel/component/couchbase/integration/ConsumeResumeStrategyIT.java
@@ -19,34 +19,35 @@ package org.apache.camel.component.couchbase.integration;
 import java.util.concurrent.ThreadLocalRandom;
 import java.util.concurrent.TimeUnit;
 
-import com.couchbase.client.java.Bucket;
 import org.apache.camel.Exchange;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.couchbase.CouchbaseResumeAdapter;
 import org.apache.camel.component.mock.MockEndpoint;
 import org.apache.camel.processor.resume.TransientResumeStrategy;
+import org.apache.camel.resume.ResumeAction;
 import org.apache.camel.support.resume.Resumables;
 import org.junit.jupiter.api.AfterEach;
 import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.Test;
 
+import static 
org.apache.camel.component.couchbase.CouchbaseConstants.COUCHBASE_RESUME_ACTION;
 import static org.awaitility.Awaitility.await;
 
 public class ConsumeResumeStrategyIT extends CouchbaseIntegrationTestBase {
     static class TestCouchbaseResumeAdapter implements CouchbaseResumeAdapter {
-        volatile boolean setBucketCalled;
-        volatile boolean bucketNotNull;
+        volatile boolean setResumeActionCalled;
+        volatile boolean resumeActionNotNull;
         volatile boolean resumeCalled;
 
         @Override
-        public void setBucket(Bucket bucket) {
-            setBucketCalled = true;
-            bucketNotNull = bucket != null;
+        public void resume() {
+            resumeCalled = true;
         }
 
         @Override
-        public void resume() {
-            resumeCalled = true;
+        public void setResumeAction(ResumeAction resumeAction) {
+            setResumeActionCalled = true;
+            resumeActionNotNull = resumeAction != null;
         }
     }
 
@@ -66,10 +67,10 @@ public class ConsumeResumeStrategyIT extends 
CouchbaseIntegrationTestBase {
         await().atMost(30, TimeUnit.SECONDS).until(() -> adapter != null);
 
         await().atMost(30, TimeUnit.SECONDS)
-                .untilAsserted(() -> 
Assertions.assertTrue(adapter.setBucketCalled,
+                .untilAsserted(() -> 
Assertions.assertTrue(adapter.setResumeActionCalled,
                         "The setBucket method should have been called"));
         await().atMost(3, TimeUnit.SECONDS)
-                .untilAsserted(() -> 
Assertions.assertTrue(adapter.bucketNotNull,
+                .untilAsserted(() -> 
Assertions.assertTrue(adapter.resumeActionNotNull,
                         "The input bucket should not have been null"));
         await().atMost(3, TimeUnit.SECONDS)
                 .untilAsserted(
@@ -86,6 +87,8 @@ public class ConsumeResumeStrategyIT extends 
CouchbaseIntegrationTestBase {
         return new RouteBuilder() {
             @Override
             public void configure() {
+                bindToRegistry(COUCHBASE_RESUME_ACTION, (ResumeAction) (key, 
value) -> true);
+
                 
from(String.format("%s&designDocumentName=%s&viewName=%s&limit=10", 
getConnectionUri(), bucketName, bucketName))
                         .resumable().resumeStrategy(resumeStrategy)
                         .setHeader(Exchange.OFFSET,

Reply via email to