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

davsclaus 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 7bf589fc1af6 CAMEL-25247: camel-thrift - send the failure of the 
exchange to the client, and complete an asynchronous call once (#27244)
7bf589fc1af6 is described below

commit 7bf589fc1af66250cb5c74c70898f523f5d19c6f
Author: allthingssecurity <[email protected]>
AuthorDate: Fri Oct 2 15:05:26 2026 +0530

    CAMEL-25247: camel-thrift - send the failure of the exchange to the client, 
and complete an asynchronous call once (#27244)
    
    The asynchronous Thrift consumer called onError and then onComplete for a 
failed exchange, so two results were written for one call and the error could 
be read as the answer to the next call on the same connection. The synchronous 
consumer ignored the exception of the exchange. Now the synchronous handler 
throws the exception (declared IDL exceptions reach the client as such) and the 
asynchronous callback completes the call exactly once. Documented in the 
upgrade guide.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 .../thrift/server/ThriftMethodHandler.java         |  38 +++---
 .../thrift/ThriftConsumerExceptionTest.java        | 133 +++++++++++++++++++++
 .../ROOT/pages/camel-4x-upgrade-guide-4_23.adoc    |   9 ++
 3 files changed, 163 insertions(+), 17 deletions(-)

diff --git 
a/components/camel-thrift/src/main/java/org/apache/camel/component/thrift/server/ThriftMethodHandler.java
 
b/components/camel-thrift/src/main/java/org/apache/camel/component/thrift/server/ThriftMethodHandler.java
index f7f928737fa7..b17c4b6892ee 100644
--- 
a/components/camel-thrift/src/main/java/org/apache/camel/component/thrift/server/ThriftMethodHandler.java
+++ 
b/components/camel-thrift/src/main/java/org/apache/camel/component/thrift/server/ThriftMethodHandler.java
@@ -56,29 +56,29 @@ public class ThriftMethodHandler implements MethodHandler {
                 
exchange.getIn().setHeader(ThriftConstants.THRIFT_METHOD_NAME_HEADER, 
thisMethod.getName());
 
                 consumer.process(exchange, doneSync -> {
-                    Message message = null;
-                    Object response = null;
-                    Exception exception = exchange.getException();
-
-                    if (exception != null) {
-                        callback.onError(exception);
-                    }
+                    // complete the call exactly once: with the error of the 
exchange, or with its response
+                    try {
+                        Exception exception = exchange.getException();
+                        if (exception != null) {
+                            callback.onError(exception);
+                            return;
+                        }
 
-                    message = exchange.getMessage();
+                        Message message = exchange.getMessage();
+                        if (message == null) {
+                            callback.onError(new TException("Unable process 
null message"));
+                            return;
+                        }
 
-                    if (message != null) {
                         Class returnType = 
ThriftUtils.findMethodReturnType(args[args.length - 1].getClass(), 
"onComplete");
-                        if (returnType != null) {
-                            response = message.getBody(returnType);
-                        } else {
+                        if (returnType == null) {
                             callback.onError(new TException("Unable to detect 
method return type"));
+                            return;
                         }
-                    } else {
-                        callback.onError(new TException("Unable process null 
message"));
+                        callback.onComplete(message.getBody(returnType));
+                    } finally {
+                        consumer.releaseExchange(exchange, false);
                     }
-
-                    consumer.releaseExchange(exchange, false);
-                    callback.onComplete(response);
                 });
             } else {
                 Object responseBody = null;
@@ -88,6 +88,10 @@ public class ThriftMethodHandler implements MethodHandler {
                     
exchange.getIn().setHeader(ThriftConstants.THRIFT_METHOD_NAME_HEADER, 
thisMethod.getName());
 
                     consumer.getProcessor().process(exchange);
+                    if (exchange.getException() != null) {
+                        // let the server send the error to the client (a 
declared exception as such)
+                        throw exchange.getException();
+                    }
                     responseBody = 
exchange.getIn().getBody(thisMethod.getReturnType());
                 } finally {
                     consumer.releaseExchange(exchange, false);
diff --git 
a/components/camel-thrift/src/test/java/org/apache/camel/component/thrift/ThriftConsumerExceptionTest.java
 
b/components/camel-thrift/src/test/java/org/apache/camel/component/thrift/ThriftConsumerExceptionTest.java
new file mode 100644
index 000000000000..1463cf4a8330
--- /dev/null
+++ 
b/components/camel-thrift/src/test/java/org/apache/camel/component/thrift/ThriftConsumerExceptionTest.java
@@ -0,0 +1,133 @@
+/*
+ * 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.thrift;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.thrift.generated.Calculator;
+import org.apache.camel.component.thrift.generated.InvalidOperation;
+import org.apache.camel.component.thrift.generated.Operation;
+import org.apache.camel.component.thrift.generated.Work;
+import org.apache.camel.test.junit6.CamelTestSupport;
+import org.apache.thrift.TApplicationException;
+import org.apache.thrift.protocol.TBinaryProtocol;
+import org.apache.thrift.transport.TSocket;
+import org.apache.thrift.transport.TTransport;
+import org.apache.thrift.transport.layered.TFramedTransport;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+/**
+ * A failed exchange must be sent to the Thrift client as an error, once, by 
the synchronous and the asynchronous
+ * server.
+ */
+public class ThriftConsumerExceptionTest extends CamelTestSupport {
+
+    private static final Work WORK = new Work(12, 13, Operation.MULTIPLY);
+
+    @Test
+    public void testSyncDeclaredExceptionIsSent() throws Exception {
+        TTransport transport = open("sync");
+        try {
+            Calculator.Client client = new Calculator.Client(new 
TBinaryProtocol(new TFramedTransport(transport)));
+
+            InvalidOperation e = assertThrows(InvalidOperation.class, () -> 
client.calculate(1, WORK));
+            assertEquals("Forced", e.getWhy());
+        } finally {
+            transport.close();
+        }
+    }
+
+    @Test
+    public void testSyncFailedVoidMethodIsAnError() throws Exception {
+        TTransport transport = open("sync");
+        try {
+            Calculator.Client client = new Calculator.Client(new 
TBinaryProtocol(new TFramedTransport(transport)));
+
+            assertThrows(TApplicationException.class, client::ping);
+            // the connection is still usable
+            assertEquals(25, client.add(12, 13));
+        } finally {
+            transport.close();
+        }
+    }
+
+    @Test
+    public void testAsyncServerSendsDeclaredExceptionOnce() throws Exception {
+        TTransport transport = open("async");
+        try {
+            Calculator.Client client = new Calculator.Client(new 
TBinaryProtocol(new TFramedTransport(transport)));
+
+            InvalidOperation e = assertThrows(InvalidOperation.class, () -> 
client.calculate(1, WORK));
+            assertEquals("Forced", e.getWhy());
+            // the next call on the same connection gets its own response
+            assertEquals(25, client.add(12, 13));
+        } finally {
+            transport.close();
+        }
+    }
+
+    @Test
+    public void testAsyncServerSendsFailureOfVoidMethodOnce() throws Exception 
{
+        TTransport transport = open("async");
+        try {
+            Calculator.Client client = new Calculator.Client(new 
TBinaryProtocol(new TFramedTransport(transport)));
+
+            assertThrows(TApplicationException.class, client::ping);
+            // the next call on the same connection gets its own response
+            assertEquals(25, client.add(12, 13));
+        } finally {
+            transport.close();
+        }
+    }
+
+    private TTransport open(String routeId) throws Exception {
+        TTransport transport = new TSocket("localhost", getPort(routeId));
+        transport.open();
+        return transport;
+    }
+
+    private int getPort(String routeId) {
+        return ((ThriftConsumer) 
context.getRoute(routeId).getConsumer()).getLocalPort();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            @Override
+            public void configure() {
+                
from("thrift://localhost:0/org.apache.camel.component.thrift.generated.Calculator?synchronous=true")
+                        .routeId("sync")
+                        .to("direct:service");
+
+                
from("thrift://localhost:0/org.apache.camel.component.thrift.generated.Calculator")
+                        .routeId("async")
+                        .to("direct:service");
+
+                from("direct:service")
+                        .choice()
+                        
.when(header(ThriftConstants.THRIFT_METHOD_NAME_HEADER).isEqualTo("add"))
+                        .setBody(constant(25))
+                        
.when(header(ThriftConstants.THRIFT_METHOD_NAME_HEADER).isEqualTo("calculate"))
+                        .throwException(new InvalidOperation(1, "Forced"))
+                        .otherwise()
+                        .throwException(new IllegalStateException("Forced"));
+            }
+        };
+    }
+}
diff --git 
a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_23.adoc 
b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_23.adoc
index 3118cfab8d0e..7f612b5bcf92 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_23.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_23.adoc
@@ -2362,6 +2362,15 @@ inherit values set on `defaultInstance`, and 
`defaultInstance` itself remains un
 Routes that compared unmarshalled bodies by identity, or that mutated one body 
expecting the change to
 be visible on another, must be updated.
 
+==== Consumer - failed exchanges
+
+The Thrift consumer now sends the exception of a failed exchange to the client 
as the result of the call: an
+exception declared by the Thrift method (such as `throws (1:InvalidOperation 
ouch)`) as that exception, any other
+exception as a `TApplicationException`. Before, with `synchronous=true` the 
failure was ignored (a `void` method
+returned normally, other methods returned the message body converted to the 
return type, or failed with
+`Return type requires not empty body`), and in the default asynchronous mode 
both the error and a regular response
+were sent, so the error could be read as the answer to the next call on the 
same connection.
+
 === camel-shiro
 
 `ShiroSecurityProcessor` used to skip the Shiro `login()` call — and therefore 
the credential check —

Reply via email to