You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by or...@apache.org on 2023/10/30 10:49:38 UTC

(camel) branch main updated: (chores) camel-platform-http-vertx: break large methods

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 fa375eddb03  (chores) camel-platform-http-vertx: break large methods
fa375eddb03 is described below

commit fa375eddb0396a4c7d2bb651da2bda16353ee9dc
Author: Otavio Rodolfo Piske <an...@gmail.com>
AuthorDate: Mon Oct 30 10:11:14 2023 +0100

     (chores) camel-platform-http-vertx: break large methods
    
    This should help obtain better information when profiling the code
---
 .../http/vertx/VertxPlatformHttpConsumer.java      | 68 +++++++++++++---------
 .../http/vertx/VertxPlatformHttpSupport.java       | 34 +++++++----
 2 files changed, 61 insertions(+), 41 deletions(-)

diff --git a/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpConsumer.java b/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpConsumer.java
index 708e0a86e25..d948d5c833a 100644
--- a/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpConsumer.java
+++ b/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpConsumer.java
@@ -26,6 +26,7 @@ import java.util.regex.Pattern;
 
 import jakarta.activation.DataHandler;
 
+import io.vertx.core.AsyncResult;
 import io.vertx.core.Handler;
 import io.vertx.core.MultiMap;
 import io.vertx.core.Vertx;
@@ -183,35 +184,44 @@ public class VertxPlatformHttpConsumer extends DefaultConsumer implements Suspen
             handleProxy(ctx, exchange);
         }
 
-        vertx.executeBlocking(() -> {
-            createUoW(exchange);
-            getProcessor().process(exchange);
-            return null;
-        }, false)
-                .onComplete(result -> {
-                    Throwable failure = null;
-                    try {
-                        if (result.succeeded()) {
-                            try {
-                                writeResponse(ctx, exchange, getEndpoint().getHeaderFilterStrategy(), muteExceptions);
-                            } catch (Exception e) {
-                                failure = e;
-                            }
-                        } else {
-                            failure = result.cause();
-                        }
-
-                        if (failure != null) {
-                            getExceptionHandler().handleException(
-                                    "Failed handling platform-http endpoint " + getEndpoint().getPath(),
-                                    failure);
-                            ctx.fail(failure);
-                        }
-                    } finally {
-                        doneUoW(exchange);
-                        releaseExchange(exchange, false);
-                    }
-                });
+        vertx.executeBlocking(() -> processRequest(exchange), false)
+                .onComplete(result -> processResult(ctx, result, exchange));
+    }
+
+    private void processResult(
+            RoutingContext ctx, AsyncResult<Object> result, Exchange exchange) {
+        Throwable failure = null;
+        try {
+            if (result.succeeded()) {
+                try {
+                    writeResponse(ctx, exchange, getEndpoint().getHeaderFilterStrategy(), muteExceptions);
+                } catch (Exception e) {
+                    failure = e;
+                }
+            } else {
+                failure = result.cause();
+            }
+
+            if (failure != null) {
+                handleFailure(ctx, failure);
+            }
+        } finally {
+            doneUoW(exchange);
+            releaseExchange(exchange, false);
+        }
+    }
+
+    private void handleFailure(RoutingContext ctx, Throwable failure) {
+        getExceptionHandler().handleException(
+                "Failed handling platform-http endpoint " + getEndpoint().getPath(),
+                failure);
+        ctx.fail(failure);
+    }
+
+    private Object processRequest(Exchange exchange) throws Exception {
+        createUoW(exchange);
+        getProcessor().process(exchange);
+        return null;
     }
 
     private static void handleSuspend(RoutingContext ctx) {
diff --git a/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpSupport.java b/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpSupport.java
index 74cec0c62e1..73e3e22641c 100644
--- a/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpSupport.java
+++ b/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpSupport.java
@@ -35,6 +35,7 @@ import io.vertx.core.net.SocketAddress;
 import io.vertx.ext.web.RoutingContext;
 import org.apache.camel.Exchange;
 import org.apache.camel.Message;
+import org.apache.camel.NoTypeConversionAvailableException;
 import org.apache.camel.TypeConverter;
 import org.apache.camel.spi.HeaderFilterStrategy;
 import org.apache.camel.support.ExchangeHelper;
@@ -204,21 +205,30 @@ public final class VertxPlatformHttpSupport {
         } else if (body instanceof Buffer) {
             response.end((Buffer) body);
         } else {
-            final TypeConverter tc = camelExchange.getContext().getTypeConverter();
-            // Try to convert to ByteBuffer for performance reason
-            final ByteBuffer bb = tc.tryConvertTo(ByteBuffer.class, camelExchange, body);
-            if (bb != null) {
-                final Buffer b = Buffer.buffer(bb.capacity());
-                b.setBytes(0, bb);
-                response.end(b);
-            } else {
-                // Otherwise fallback to most generic InputStream conversion
-                final InputStream is = tc.mandatoryConvertTo(InputStream.class, camelExchange, body);
-                writeResponseAs(response, is);
-            }
+            writeResponseAsFallback(camelExchange, body, response);
         }
     }
 
+    private static void writeResponseAsFallback(Exchange camelExchange, Object body, HttpServerResponse response)
+            throws NoTypeConversionAvailableException, IOException {
+        final TypeConverter tc = camelExchange.getContext().getTypeConverter();
+        // Try to convert to ByteBuffer for performance reason
+        final ByteBuffer bb = tc.tryConvertTo(ByteBuffer.class, camelExchange, body);
+        if (bb != null) {
+            writeResponseAs(response, bb);
+        } else {
+            // Otherwise fallback to most generic InputStream conversion
+            final InputStream is = tc.mandatoryConvertTo(InputStream.class, camelExchange, body);
+            writeResponseAs(response, is);
+        }
+    }
+
+    private static void writeResponseAs(HttpServerResponse response, ByteBuffer bb) {
+        final Buffer b = Buffer.buffer(bb.capacity());
+        b.setBytes(0, bb);
+        response.end(b);
+    }
+
     private static void writeResponseAs(HttpServerResponse response, InputStream is) throws IOException {
         final byte[] bytes = new byte[4096];
         try (InputStream in = is) {