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 4c0988f9827b CAMEL-27208: camel-grpc - a producer with streamRepliesTo
fails the exchange of a failed call (#30479)
4c0988f9827b is described below
commit 4c0988f9827b661fcf2c753f70f5cb6093393a26
Author: Guillaume Nodet <[email protected]>
AuthorDate: Fri Nov 20 10:50:52 2026 +0100
CAMEL-27208: camel-grpc - a producer with streamRepliesTo fails the
exchange of a failed call (#30479)
Backport of #30464 to camel-4.18.x (cherry picked from commit
af57508eb205f4abec51661aaca2000509a587d8).
With streamRepliesTo and the SIMPLE producer strategy (the default), a
failed call (server not reachable,
deadline exceeded, an error status) now fails the exchange of the call
instead of completing it as a
success, unless forwardOnError=true routes the error to the replies
endpoint. The backported test uses
camel-test-junit5, as camel-4.18.x has no camel-test-junit6.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
.../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..ad5bb3dc33be
--- /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.junit5.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();
+ }
+ }
+}