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

davsclaus pushed a commit to branch camel-4.18.x
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/camel-4.18.x by this push:
     new 75d26febebe6 [backport camel-4.18.x] CAMEL-27192: camel-google-pubsub 
- cache a publisher per endpoint, and resume an ordering key after a failed 
publish (#30405)
75d26febebe6 is described below

commit 75d26febebe650be49fc9b7a5197604bfd746adf
Author: Claus Ibsen <[email protected]>
AuthorDate: Thu Nov 19 13:49:27 2026 +0100

    [backport camel-4.18.x] CAMEL-27192: camel-google-pubsub - cache a 
publisher per endpoint, and resume an ordering key after a failed publish 
(#30405)
    
    The publisher cache was keyed by the topic only, but a publisher is built
    from the settings of the endpoint (credentials, regional endpoint,
    ordering, retry), so a second endpoint on the same topic used the
    publisher of the first. The cache is now keyed by the endpoint, and built
    in doInit so publisherCacheSize and publisherCacheTimeout take effect.
    
    With ordering enabled, the Google publisher pauses an ordering key after
    a failed publish; the producer now calls resumePublish, so a redelivery
    and the later messages with that key are not failed at once. A failure of
    resumePublish is added as suppressed to the failure of the publish.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
    (cherry picked from commit 1ec6cc8d0fd345c1eb706d67beb03937b5367794)
---
 .../google/pubsub/GooglePubsubComponent.java       |  28 +++--
 .../google/pubsub/GooglePubsubProducer.java        |  17 ++-
 .../pubsub/unit/PubsubPublisherCacheTest.java      | 132 +++++++++++++++++++++
 3 files changed, 168 insertions(+), 9 deletions(-)

diff --git 
a/components/camel-google/camel-google-pubsub/src/main/java/org/apache/camel/component/google/pubsub/GooglePubsubComponent.java
 
b/components/camel-google/camel-google-pubsub/src/main/java/org/apache/camel/component/google/pubsub/GooglePubsubComponent.java
index d6049c822ccb..512db765bb0d 100644
--- 
a/components/camel-google/camel-google-pubsub/src/main/java/org/apache/camel/component/google/pubsub/GooglePubsubComponent.java
+++ 
b/components/camel-google/camel-google-pubsub/src/main/java/org/apache/camel/component/google/pubsub/GooglePubsubComponent.java
@@ -104,11 +104,8 @@ public class GooglePubsubComponent extends 
HeaderFilterStrategyComponent {
         }
     };
 
-    private Cache<String, Publisher> cachedPublishers = 
CacheBuilder.newBuilder()
-            .expireAfterWrite(publisherCacheTimeout, TimeUnit.MILLISECONDS)
-            .maximumSize(publisherCacheSize)
-            .removalListener(removalListener)
-            .build();
+    // built in doInit, so the configured size and timeout are used
+    private Cache<String, Publisher> cachedPublishers;
 
     public GooglePubsubComponent() {
     }
@@ -137,16 +134,31 @@ public class GooglePubsubComponent extends 
HeaderFilterStrategyComponent {
         return pubsubEndpoint;
     }
 
+    @Override
+    protected void doInit() throws Exception {
+        super.doInit();
+        cachedPublishers = CacheBuilder.newBuilder()
+                .expireAfterWrite(publisherCacheTimeout, TimeUnit.MILLISECONDS)
+                .maximumSize(publisherCacheSize)
+                .removalListener(removalListener)
+                .build();
+    }
+
     @Override
     protected void doShutdown() throws Exception {
-        cachedPublishers.cleanUp();
-        cachedPublishers.invalidateAll();
+        if (cachedPublishers != null) {
+            cachedPublishers.cleanUp();
+            cachedPublishers.invalidateAll();
+        }
         super.doShutdown();
     }
 
     public Publisher getPublisher(String topicName, GooglePubsubEndpoint 
googlePubsubEndpoint)
             throws ExecutionException {
-        return cachedPublishers.get(topicName, () -> buildPublisher(topicName, 
googlePubsubEndpoint));
+        // a publisher is built from the settings of the endpoint 
(credentials, regional endpoint, ordering, retry),
+        // so endpoints that publish to the same topic with other settings 
must not share it
+        String key = topicName + " " + googlePubsubEndpoint.getEndpointUri();
+        return cachedPublishers.get(key, () -> buildPublisher(topicName, 
googlePubsubEndpoint));
     }
 
     private Publisher buildPublisher(String topicName, GooglePubsubEndpoint 
googlePubsubEndpoint)
diff --git 
a/components/camel-google/camel-google-pubsub/src/main/java/org/apache/camel/component/google/pubsub/GooglePubsubProducer.java
 
b/components/camel-google/camel-google-pubsub/src/main/java/org/apache/camel/component/google/pubsub/GooglePubsubProducer.java
index d295dc870ab7..6813e05411e9 100644
--- 
a/components/camel-google/camel-google-pubsub/src/main/java/org/apache/camel/component/google/pubsub/GooglePubsubProducer.java
+++ 
b/components/camel-google/camel-google-pubsub/src/main/java/org/apache/camel/component/google/pubsub/GooglePubsubProducer.java
@@ -18,6 +18,7 @@ package org.apache.camel.component.google.pubsub;
 
 import java.util.List;
 import java.util.Map;
+import java.util.concurrent.ExecutionException;
 
 import com.google.api.core.ApiFuture;
 import com.google.cloud.pubsub.v1.Publisher;
@@ -148,6 +149,20 @@ public class GooglePubsubProducer extends DefaultProducer {
         PubsubMessage message = messageBuilder.build();
 
         ApiFuture<String> messageIdFuture = publisher.publish(message);
-        exchange.getIn().setHeader(GooglePubsubConstants.MESSAGE_ID, 
messageIdFuture.get());
+        try {
+            exchange.getIn().setHeader(GooglePubsubConstants.MESSAGE_ID, 
messageIdFuture.get());
+        } catch (ExecutionException e) {
+            if (orderingKey != null && endpoint.isMessageOrderingEnabled()) {
+                // the publisher pauses an ordering key after a failed 
publish, so a redelivery and the later messages
+                // with that key would fail at once
+                try {
+                    publisher.resumePublish(orderingKey);
+                } catch (RuntimeException resumeException) {
+                    // keep the failure of the publish as the cause
+                    e.addSuppressed(resumeException);
+                }
+            }
+            throw e;
+        }
     }
 }
diff --git 
a/components/camel-google/camel-google-pubsub/src/test/java/org/apache/camel/component/google/pubsub/unit/PubsubPublisherCacheTest.java
 
b/components/camel-google/camel-google-pubsub/src/test/java/org/apache/camel/component/google/pubsub/unit/PubsubPublisherCacheTest.java
new file mode 100644
index 000000000000..088a64a72f5c
--- /dev/null
+++ 
b/components/camel-google/camel-google-pubsub/src/test/java/org/apache/camel/component/google/pubsub/unit/PubsubPublisherCacheTest.java
@@ -0,0 +1,132 @@
+/*
+ * 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.google.pubsub.unit;
+
+import com.google.api.core.ApiFutures;
+import com.google.cloud.pubsub.v1.Publisher;
+import org.apache.camel.Exchange;
+import org.apache.camel.component.google.pubsub.GooglePubsubComponent;
+import org.apache.camel.component.google.pubsub.GooglePubsubConstants;
+import org.apache.camel.component.google.pubsub.GooglePubsubEndpoint;
+import org.apache.camel.component.google.pubsub.GooglePubsubProducer;
+import 
org.apache.camel.component.google.pubsub.serializer.DefaultGooglePubsubSerializer;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.camel.support.DefaultExchange;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+/**
+ * The publishers of the component are cached per endpoint, as each is built 
from the settings of its endpoint, and a
+ * failed publish with an ordering key does not leave that key paused.
+ */
+class PubsubPublisherCacheTest {
+
+    private static final String TOPIC = "projects/project/topics/orders";
+
+    private final DefaultCamelContext context = new DefaultCamelContext();
+    private GooglePubsubComponent component;
+
+    @BeforeEach
+    void setUp() {
+        component = new GooglePubsubComponent();
+        // a local address with no credentials, so the publishers are built 
without Google Cloud
+        component.setEndpoint("localhost:1");
+        component.setAuthenticate(false);
+        component.setPublisherCacheSize(2);
+        context.addComponent("google-pubsub", component);
+        context.start();
+    }
+
+    @AfterEach
+    void tearDown() {
+        context.stop();
+    }
+
+    @Test
+    void endpointsWithOtherSettingsGetTheirOwnPublisher() throws Exception {
+        GooglePubsubEndpoint plain = endpoint("google-pubsub:project:orders");
+        GooglePubsubEndpoint ordered = endpoint(
+                
"google-pubsub:project:orders?messageOrderingEnabled=true&pubsubEndpoint=europe-west1-pubsub.googleapis.com:443");
+
+        Publisher first = component.getPublisher(TOPIC, plain);
+
+        assertThat(component.getPublisher(TOPIC, ordered)).isNotSameAs(first);
+        assertThat(component.getPublisher(TOPIC, plain)).isSameAs(first);
+    }
+
+    @Test
+    void theConfiguredCacheSizeIsUsed() throws Exception {
+        GooglePubsubEndpoint a = endpoint("google-pubsub:project:a");
+        GooglePubsubEndpoint b = endpoint("google-pubsub:project:b");
+        GooglePubsubEndpoint c = endpoint("google-pubsub:project:c");
+
+        Publisher first = component.getPublisher("projects/project/topics/a", 
a);
+        component.getPublisher("projects/project/topics/b", b);
+        component.getPublisher("projects/project/topics/c", c);
+
+        // with room for 2 publishers, the one of a was evicted (and shut down)
+        assertThat(component.getPublisher("projects/project/topics/a", 
a)).isNotSameAs(first);
+    }
+
+    @Test
+    void aFailedOrderedPublishResumesTheOrderingKey() throws Exception {
+        Publisher publisher = publishFails(true);
+
+        verify(publisher).resumePublish("customer-1");
+    }
+
+    @Test
+    void aFailedPublishWithoutOrderingDoesNotResume() throws Exception {
+        Publisher publisher = publishFails(false);
+
+        verify(publisher, never()).resumePublish(anyString());
+    }
+
+    private Publisher publishFails(boolean ordering) throws Exception {
+        GooglePubsubEndpoint endpoint = mock();
+        GooglePubsubComponent mockComponent = mock();
+        Publisher publisher = mock();
+        when(endpoint.getProjectId()).thenReturn("project");
+        when(endpoint.getDestinationName()).thenReturn("orders");
+        when(endpoint.getComponent()).thenReturn(mockComponent);
+        when(endpoint.getSerializer()).thenReturn(new 
DefaultGooglePubsubSerializer());
+        when(endpoint.isMessageOrderingEnabled()).thenReturn(ordering);
+        when(mockComponent.getPublisher(anyString(), 
any())).thenReturn(publisher);
+        
when(publisher.publish(any())).thenReturn(ApiFutures.immediateFailedFuture(new 
IllegalStateException("down")));
+
+        Exchange exchange = new DefaultExchange(context);
+        exchange.getIn().setBody("order");
+        exchange.getIn().setHeader(GooglePubsubConstants.ORDERING_KEY, 
"customer-1");
+
+        assertThatThrownBy(() -> new 
GooglePubsubProducer(endpoint).process(exchange)).hasRootCauseMessage("down");
+        return publisher;
+    }
+
+    private GooglePubsubEndpoint endpoint(String uri) {
+        return context.getEndpoint(uri, GooglePubsubEndpoint.class);
+    }
+}

Reply via email to