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

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


The following commit(s) were added to refs/heads/camel-4.22.x by this push:
     new 34ea03ed4538 [backport camel-4.22.x] CAMEL-27208: camel-grpc - a 
producer with streamRepliesTo fails the exchange of a failed call (#30480)
34ea03ed4538 is described below

commit 34ea03ed4538f18f227c876e7672dd6e2c14763a
Author: Guillaume Nodet <[email protected]>
AuthorDate: Fri Nov 20 09:12:00 2026 +0100

    [backport camel-4.22.x] CAMEL-27208: camel-grpc - a producer with 
streamRepliesTo fails the exchange of a failed call (#30480)
    
    With streamRepliesTo and the SIMPLE producer strategy (the default),
    GrpcResponseRouterStreamObserver completes the exchange of the call when
    the call ends. When the call failed (server not reachable, deadline
    exceeded, an error status) it completed the exchange without the error,
    and with forwardOnError=false (the default) the error was not routed
    anywhere either: the failed call was reported as a success and lost.
    
    - onError sets the error on the exchange of the call when the error is not
      forwarded to the replies endpoint (SIMPLE strategy). With
      forwardOnError=true the error is routed to the replies endpoint as
      before. STREAMING is unchanged: its exchanges complete when sent.
    - 4.24 upgrade guide entry.
    - GrpcProducerStreamRepliesToTest: error status from the server, server
      not reachable, a successful call and forwardOnError=true.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
    (cherry picked from commit af57508eb205f4abec51661aaca2000509a587d8)
---
 .../client/GrpcResponseRouterStreamObserver.java   |   6 +
 .../grpc/GrpcProducerStreamRepliesToTest.java      | 147 +++++++++++++++++++++
 2 files changed, 153 insertions(+)

diff --git 
a/components/camel-grpc/src/main/java/org/apache/camel/component/grpc/client/GrpcResponseRouterStreamObserver.java
 
b/components/camel-grpc/src/main/java/org/apache/camel/component/grpc/client/GrpcResponseRouterStreamObserver.java
index 152a43cb2e02..c6c05160c1db 100644
--- 
a/components/camel-grpc/src/main/java/org/apache/camel/component/grpc/client/GrpcResponseRouterStreamObserver.java
+++ 
b/components/camel-grpc/src/main/java/org/apache/camel/component/grpc/client/GrpcResponseRouterStreamObserver.java
@@ -25,6 +25,7 @@ import org.apache.camel.Endpoint;
 import org.apache.camel.Exchange;
 import org.apache.camel.component.grpc.GrpcConfiguration;
 import org.apache.camel.component.grpc.GrpcConstants;
+import org.apache.camel.component.grpc.GrpcProducerStrategy;
 
 /**
  * A stream observer that routes all responses to another endpoint.
@@ -66,6 +67,11 @@ public class GrpcResponseRouterStreamObserver implements 
StreamObserver<Object>
             
newExchange.getIn().setHeader(GrpcConstants.GRPC_EVENT_TYPE_HEADER, 
GrpcConstants.GRPC_EVENT_TYPE_ON_ERROR);
             newExchange.getIn().setBody(throwable);
             doSend(newExchange);
+        } else if (configuration.getProducerStrategy() == 
GrpcProducerStrategy.SIMPLE) {
+            // the call of this exchange failed, and the error is not routed 
to the replies endpoint: fail the exchange,
+            // else the failed call is reported as a success and the error is 
lost. In STREAMING mode the exchanges are
+            // completed when they are sent on the stream, before it ends
+            exchange.setException(throwable);
         }
         callback.done(true);
     }
diff --git 
a/components/camel-grpc/src/test/java/org/apache/camel/component/grpc/GrpcProducerStreamRepliesToTest.java
 
b/components/camel-grpc/src/test/java/org/apache/camel/component/grpc/GrpcProducerStreamRepliesToTest.java
new file mode 100644
index 000000000000..ee3254af9808
--- /dev/null
+++ 
b/components/camel-grpc/src/test/java/org/apache/camel/component/grpc/GrpcProducerStreamRepliesToTest.java
@@ -0,0 +1,147 @@
+/*
+ * 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.grpc;
+
+import java.net.ServerSocket;
+
+import io.grpc.Server;
+import io.grpc.ServerBuilder;
+import io.grpc.Status;
+import io.grpc.StatusRuntimeException;
+import io.grpc.stub.StreamObserver;
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.junit6.CamelTestSupport;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+/**
+ * A producer with streamRepliesTo (SIMPLE strategy): the exchange that made a 
call fails when the call fails, unless
+ * the error is routed to the replies endpoint (forwardOnError).
+ */
+class GrpcProducerStreamRepliesToTest extends CamelTestSupport {
+
+    private static final String SERVICE = 
"/org.apache.camel.component.grpc.PingPong";
+
+    private Server grpcServer;
+    private int unusedPort;
+
+    @Override
+    protected void setupResources() throws Exception {
+        grpcServer = ServerBuilder.forPort(0).addService(new 
PingPongImpl()).build().start();
+        // a port that nothing listens on
+        try (ServerSocket socket = new ServerSocket(0)) {
+            unusedPort = socket.getLocalPort();
+        }
+    }
+
+    @Override
+    protected void cleanupResources() {
+        if (grpcServer != null) {
+            grpcServer.shutdownNow();
+        }
+    }
+
+    @Test
+    void testFailedCallFailsTheExchange() throws Exception {
+        Exchange exchange = template.send("direct:simple",
+                e -> 
e.getIn().setBody(PingRequest.newBuilder().setPingName("error").build()));
+
+        StatusRuntimeException cause = 
assertInstanceOf(StatusRuntimeException.class, exchange.getException());
+        assertEquals(Status.Code.UNAVAILABLE, cause.getStatus().getCode());
+    }
+
+    @Test
+    void testServerNotReachableFailsTheExchange() throws Exception {
+        Exchange exchange = template.send("direct:unreachable",
+                e -> 
e.getIn().setBody(PingRequest.newBuilder().setPingName("ok").build()));
+
+        StatusRuntimeException cause = 
assertInstanceOf(StatusRuntimeException.class, exchange.getException());
+        assertEquals(Status.Code.UNAVAILABLE, cause.getStatus().getCode());
+    }
+
+    @Test
+    void testSuccessfulCallRoutesTheReplies() throws Exception {
+        MockEndpoint replies = getMockEndpoint("mock:simple-replies");
+        replies.expectedBodiesReceived(
+                PongResponse.newBuilder().setPongName("Hello ok 1").build(),
+                PongResponse.newBuilder().setPongName("Hello ok 2").build());
+
+        Exchange exchange = template.send("direct:simple",
+                e -> 
e.getIn().setBody(PingRequest.newBuilder().setPingName("ok").build()));
+
+        assertNull(exchange.getException());
+        replies.assertIsSatisfied();
+    }
+
+    @Test
+    void testForwardedErrorIsRoutedToTheReplies() throws Exception {
+        MockEndpoint replies = getMockEndpoint("mock:forward-replies");
+        replies.expectedMessageCount(1);
+        replies.expectedHeaderReceived(GrpcConstants.GRPC_EVENT_TYPE_HEADER, 
GrpcConstants.GRPC_EVENT_TYPE_ON_ERROR);
+
+        Exchange exchange = template.send("direct:simple-forward-on-error",
+                e -> 
e.getIn().setBody(PingRequest.newBuilder().setPingName("error").build()));
+
+        // the error is handled by the route of the replies, as forwardOnError 
asks
+        assertNull(exchange.getException());
+        replies.assertIsSatisfied();
+        assertInstanceOf(StatusRuntimeException.class, 
replies.getExchanges().get(0).getIn().getBody());
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            @Override
+            public void configure() {
+                String grpc = "grpc://localhost:" + grpcServer.getPort() + 
SERVICE;
+
+                from("direct:simple")
+                        .to(grpc + 
"?method=pingSyncAsync&streamRepliesTo=direct:simple-replies");
+                from("direct:simple-replies").to("mock:simple-replies");
+
+                from("direct:simple-forward-on-error")
+                        .to(grpc + 
"?method=pingSyncAsync&streamRepliesTo=direct:forward-replies&forwardOnError=true");
+                from("direct:forward-replies").to("mock:forward-replies");
+
+                from("direct:unreachable")
+                        .to("grpc://localhost:" + unusedPort + SERVICE
+                            + 
"?method=pingSyncAsync&streamRepliesTo=direct:simple-replies");
+            }
+        };
+    }
+
+    static class PingPongImpl extends PingPongGrpc.PingPongImplBase {
+
+        @Override
+        public void pingSyncAsync(PingRequest request, 
StreamObserver<PongResponse> responseObserver) {
+            if ("error".equals(request.getPingName())) {
+                
responseObserver.onError(Status.UNAVAILABLE.withDescription("Requested 
error").asRuntimeException());
+                return;
+            }
+            for (int i = 1; i <= 2; i++) {
+                responseObserver.onNext(
+                        PongResponse.newBuilder().setPongName("Hello " + 
request.getPingName() + " " + i).build());
+            }
+            responseObserver.onCompleted();
+        }
+    }
+}

Reply via email to