You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by fm...@apache.org on 2025/01/29 15:27:22 UTC

(camel-spring-boot) branch main updated (1250d772830 -> 671f91e386a)

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

fmariani pushed a change to branch main
in repository https://gitbox.apache.org/repos/asf/camel-spring-boot.git


    from 1250d772830 Regen
     new 2df89e23889 Do not spawn useslss threads
     new 671f91e386a Use CompletableFuture instead of Interceptor

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../springboot/SpringBootPlatformHttpConsumer.java   |  8 ++++++--
 .../springboot/SpringBootPlatformHttpEngine.java     | 18 +-----------------
 .../SpringBootPlatformHttpCertificationTest.java     | 20 +++++++++++++++++---
 3 files changed, 24 insertions(+), 22 deletions(-)


(camel-spring-boot) 01/02: Do not spawn useslss threads

Posted by fm...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

fmariani pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-spring-boot.git

commit 2df89e238897c29b2bbb4083070b00637a1b8603
Author: Croway <fe...@gmail.com>
AuthorDate: Mon Jan 27 15:51:17 2025 +0100

    Do not spawn useslss threads
---
 .../springboot/SpringBootPlatformHttpConsumer.java | 43 ++++++++++------------
 .../springboot/SpringBootPlatformHttpEngine.java   |  2 +-
 .../SpringBootPlatformHttpCertificationTest.java   | 19 +++++++++-
 3 files changed, 39 insertions(+), 25 deletions(-)

diff --git a/components-starter/camel-platform-http-starter/src/main/java/org/apache/camel/component/platform/http/springboot/SpringBootPlatformHttpConsumer.java b/components-starter/camel-platform-http-starter/src/main/java/org/apache/camel/component/platform/http/springboot/SpringBootPlatformHttpConsumer.java
index 3da18e89aa4..f906c77320e 100644
--- a/components-starter/camel-platform-http-starter/src/main/java/org/apache/camel/component/platform/http/springboot/SpringBootPlatformHttpConsumer.java
+++ b/components-starter/camel-platform-http-starter/src/main/java/org/apache/camel/component/platform/http/springboot/SpringBootPlatformHttpConsumer.java
@@ -17,8 +17,7 @@
 package org.apache.camel.component.platform.http.springboot;
 
 import java.util.concurrent.CompletableFuture;
-import java.util.concurrent.Executor;
-import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
 
 import jakarta.servlet.ServletException;
 import jakarta.servlet.http.Cookie;
@@ -41,6 +40,8 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.web.bind.annotation.CookieValue;
 import org.springframework.web.bind.annotation.ResponseBody;
+import org.springframework.web.context.request.RequestAttributes;
+import org.springframework.web.context.request.RequestContextHolder;
 
 public class SpringBootPlatformHttpConsumer extends DefaultConsumer implements PlatformHttpConsumer, Suspendable, SuspendableService {
 
@@ -48,7 +49,6 @@ public class SpringBootPlatformHttpConsumer extends DefaultConsumer implements P
 
     private HttpBinding binding;
     private final boolean handleWriteResponseError;
-    private Executor executor;
     private CookieConfiguration cookieConfiguration;
 
     public SpringBootPlatformHttpConsumer(PlatformHttpEndpoint endpoint, Processor processor) {
@@ -59,12 +59,6 @@ public class SpringBootPlatformHttpConsumer extends DefaultConsumer implements P
         this.binding.setFileNameExtWhitelist(endpoint.getFileNameExtWhitelist());
         this.binding.setUseReaderForPayload(!endpoint.isUseStreaming());
         this.handleWriteResponseError = endpoint.isHandleWriteResponseError();
-        this.executor = Executors.newSingleThreadExecutor();
-    }
-
-    public SpringBootPlatformHttpConsumer(PlatformHttpEndpoint endpoint, Processor processor, Executor executor) {
-        this(endpoint, processor);
-        this.executor = executor;
     }
 
     /**
@@ -80,26 +74,29 @@ public class SpringBootPlatformHttpConsumer extends DefaultConsumer implements P
     }
 
     /**
-     * This method is invoked by Spring Boot when invoking Camel via platform-http
+     * This method is invoked by Spring Boot when invoking Camel via platform-http.
+     *
+     * The method is already running asynchronously via AsyncExecutionInterceptor.
+     *
+     * Returns an empty CompletableFuture as per documentation https://spring.io/guides/gs/async-method
      */
     @ResponseBody
     public CompletableFuture<Void> service(HttpServletRequest request, HttpServletResponse response) {
-        return CompletableFuture.runAsync(() -> {
-            LOG.trace("Service: {}", request);
+        LOG.trace("Service: {}", request);
+        try {
+            handleService(request, response);
+        } catch (Exception e) {
+            // do not leak exception back to caller
+            LOG.warn("Error handling request due to: {}", e.getMessage(), e);
             try {
-                handleService(request, response);
-            } catch (Exception e) {
-                // do not leak exception back to caller
-                LOG.warn("Error handling request due to: {}", e.getMessage(), e);
-                try {
-                    if (!response.isCommitted()) {
-                        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
-                    }
-                } catch (Exception e1) {
-                    // ignore
+                if (!response.isCommitted()) {
+                    response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
                 }
+            } catch (Exception e1) {
+                // ignore
             }
-        }, executor);
+        }
+        return CompletableFuture.completedFuture(null);
     }
 
     protected void handleService(HttpServletRequest request, HttpServletResponse response) throws Exception {
diff --git a/components-starter/camel-platform-http-starter/src/main/java/org/apache/camel/component/platform/http/springboot/SpringBootPlatformHttpEngine.java b/components-starter/camel-platform-http-starter/src/main/java/org/apache/camel/component/platform/http/springboot/SpringBootPlatformHttpEngine.java
index 4e5d3c9ab16..dde685cb1a8 100644
--- a/components-starter/camel-platform-http-starter/src/main/java/org/apache/camel/component/platform/http/springboot/SpringBootPlatformHttpEngine.java
+++ b/components-starter/camel-platform-http-starter/src/main/java/org/apache/camel/component/platform/http/springboot/SpringBootPlatformHttpEngine.java
@@ -44,7 +44,7 @@ public class SpringBootPlatformHttpEngine implements PlatformHttpEngine {
     @Override
     public PlatformHttpConsumer createConsumer(PlatformHttpEndpoint endpoint, Processor processor) {
         ProxyFactory factory = new ProxyFactory();
-        factory.setTarget(new SpringBootPlatformHttpConsumer(endpoint, processor, executor));
+        factory.setTarget(new SpringBootPlatformHttpConsumer(endpoint, processor));
 
         JdkRegexpMethodPointcut jdkRegexpMethodPointcut = new JdkRegexpMethodPointcut();
         jdkRegexpMethodPointcut.setPattern("org.apache.camel.component.platform.http.springboot.SpringBootPlatformHttpConsumer.service");
diff --git a/components-starter/camel-platform-http-starter/src/test/java/org/apache/camel/component/platform/http/springboot/SpringBootPlatformHttpCertificationTest.java b/components-starter/camel-platform-http-starter/src/test/java/org/apache/camel/component/platform/http/springboot/SpringBootPlatformHttpCertificationTest.java
index 8f44699417b..4ddf6722a48 100644
--- a/components-starter/camel-platform-http-starter/src/test/java/org/apache/camel/component/platform/http/springboot/SpringBootPlatformHttpCertificationTest.java
+++ b/components-starter/camel-platform-http-starter/src/test/java/org/apache/camel/component/platform/http/springboot/SpringBootPlatformHttpCertificationTest.java
@@ -23,8 +23,15 @@ import java.io.InputStream;
 import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
 import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.List;
 import java.util.Map;
 import java.util.Random;
+import java.util.concurrent.Callable;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
 
 import io.restassured.RestAssured;
 import io.restassured.http.ContentType;
@@ -179,8 +186,18 @@ public class SpringBootPlatformHttpCertificationTest extends PlatformHttpBase {
     public void testLoad() throws Exception {
         waitUntilRouteIsStarted(1, getGetRouteId());
 
+        ExecutorService es = Executors.newFixedThreadPool(10);
+        List<CompletableFuture> tasks = new ArrayList();
         for (int i = 0; i < 1_000; i++) {
-            Assertions.assertThat(restTemplate.getForEntity("/myget", String.class).getStatusCode().value()).isEqualTo(200);
+            tasks.add(CompletableFuture.runAsync(() ->
+                    Assertions.assertThat(restTemplate.getForEntity("/myget", String.class)
+                            .getStatusCode().value()).isEqualTo(200), es));
+        }
+        for (CompletableFuture<Void> task : tasks) {
+            task.get();
+            if (task.isCompletedExceptionally()) {
+                org.junit.jupiter.api.Assertions.fail("Exception thrown during execution, check logs");
+            }
         }
     }
 


(camel-spring-boot) 02/02: Use CompletableFuture instead of Interceptor

Posted by fm...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

fmariani pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-spring-boot.git

commit 671f91e386ad62970bd8811f927110e8b14d03aa
Author: Croway <fe...@gmail.com>
AuthorDate: Wed Jan 29 15:00:30 2025 +0100

    Use CompletableFuture instead of Interceptor
---
 .../springboot/SpringBootPlatformHttpConsumer.java | 37 +++++++++++++---------
 .../springboot/SpringBootPlatformHttpEngine.java   | 18 +----------
 .../SpringBootPlatformHttpCertificationTest.java   |  5 +--
 3 files changed, 24 insertions(+), 36 deletions(-)

diff --git a/components-starter/camel-platform-http-starter/src/main/java/org/apache/camel/component/platform/http/springboot/SpringBootPlatformHttpConsumer.java b/components-starter/camel-platform-http-starter/src/main/java/org/apache/camel/component/platform/http/springboot/SpringBootPlatformHttpConsumer.java
index f906c77320e..af5c2e9ea7c 100644
--- a/components-starter/camel-platform-http-starter/src/main/java/org/apache/camel/component/platform/http/springboot/SpringBootPlatformHttpConsumer.java
+++ b/components-starter/camel-platform-http-starter/src/main/java/org/apache/camel/component/platform/http/springboot/SpringBootPlatformHttpConsumer.java
@@ -17,7 +17,8 @@
 package org.apache.camel.component.platform.http.springboot;
 
 import java.util.concurrent.CompletableFuture;
-import java.util.concurrent.Future;
+import java.util.concurrent.Executor;
+import java.util.concurrent.Executors;
 
 import jakarta.servlet.ServletException;
 import jakarta.servlet.http.Cookie;
@@ -40,8 +41,6 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.web.bind.annotation.CookieValue;
 import org.springframework.web.bind.annotation.ResponseBody;
-import org.springframework.web.context.request.RequestAttributes;
-import org.springframework.web.context.request.RequestContextHolder;
 
 public class SpringBootPlatformHttpConsumer extends DefaultConsumer implements PlatformHttpConsumer, Suspendable, SuspendableService {
 
@@ -50,6 +49,7 @@ public class SpringBootPlatformHttpConsumer extends DefaultConsumer implements P
     private HttpBinding binding;
     private final boolean handleWriteResponseError;
     private CookieConfiguration cookieConfiguration;
+    private Executor executor;
 
     public SpringBootPlatformHttpConsumer(PlatformHttpEndpoint endpoint, Processor processor) {
         super(endpoint, processor);
@@ -59,6 +59,12 @@ public class SpringBootPlatformHttpConsumer extends DefaultConsumer implements P
         this.binding.setFileNameExtWhitelist(endpoint.getFileNameExtWhitelist());
         this.binding.setUseReaderForPayload(!endpoint.isUseStreaming());
         this.handleWriteResponseError = endpoint.isHandleWriteResponseError();
+        this.executor = Executors.newSingleThreadExecutor();
+    }
+
+    public SpringBootPlatformHttpConsumer(PlatformHttpEndpoint endpoint, Processor processor, Executor executor) {
+        this(endpoint, processor);
+        this.executor = executor;
     }
 
     /**
@@ -82,21 +88,22 @@ public class SpringBootPlatformHttpConsumer extends DefaultConsumer implements P
      */
     @ResponseBody
     public CompletableFuture<Void> service(HttpServletRequest request, HttpServletResponse response) {
-        LOG.trace("Service: {}", request);
-        try {
-            handleService(request, response);
-        } catch (Exception e) {
-            // do not leak exception back to caller
-            LOG.warn("Error handling request due to: {}", e.getMessage(), e);
+        return CompletableFuture.runAsync(() -> {
+            LOG.trace("Service: {}", request);
             try {
-                if (!response.isCommitted()) {
-                    response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
+                handleService(request, response);
+            } catch (Exception e) {
+                // do not leak exception back to caller
+                LOG.warn("Error handling request due to: {}", e.getMessage(), e);
+                try {
+                    if (!response.isCommitted()) {
+                        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
+                    }
+                } catch (Exception e1) {
+                    // ignore
                 }
-            } catch (Exception e1) {
-                // ignore
             }
-        }
-        return CompletableFuture.completedFuture(null);
+        }, executor);
     }
 
     protected void handleService(HttpServletRequest request, HttpServletResponse response) throws Exception {
diff --git a/components-starter/camel-platform-http-starter/src/main/java/org/apache/camel/component/platform/http/springboot/SpringBootPlatformHttpEngine.java b/components-starter/camel-platform-http-starter/src/main/java/org/apache/camel/component/platform/http/springboot/SpringBootPlatformHttpEngine.java
index dde685cb1a8..79542a0fc8e 100644
--- a/components-starter/camel-platform-http-starter/src/main/java/org/apache/camel/component/platform/http/springboot/SpringBootPlatformHttpEngine.java
+++ b/components-starter/camel-platform-http-starter/src/main/java/org/apache/camel/component/platform/http/springboot/SpringBootPlatformHttpEngine.java
@@ -20,10 +20,6 @@ import org.apache.camel.Processor;
 import org.apache.camel.component.platform.http.PlatformHttpEndpoint;
 import org.apache.camel.component.platform.http.spi.PlatformHttpConsumer;
 import org.apache.camel.component.platform.http.spi.PlatformHttpEngine;
-import org.springframework.aop.framework.ProxyFactory;
-import org.springframework.aop.interceptor.AsyncExecutionInterceptor;
-import org.springframework.aop.support.DefaultPointcutAdvisor;
-import org.springframework.aop.support.JdkRegexpMethodPointcut;
 
 import java.util.concurrent.Executor;
 
@@ -43,19 +39,7 @@ public class SpringBootPlatformHttpEngine implements PlatformHttpEngine {
 
     @Override
     public PlatformHttpConsumer createConsumer(PlatformHttpEndpoint endpoint, Processor processor) {
-        ProxyFactory factory = new ProxyFactory();
-        factory.setTarget(new SpringBootPlatformHttpConsumer(endpoint, processor));
-
-        JdkRegexpMethodPointcut jdkRegexpMethodPointcut = new JdkRegexpMethodPointcut();
-        jdkRegexpMethodPointcut.setPattern("org.apache.camel.component.platform.http.springboot.SpringBootPlatformHttpConsumer.service");
-
-        DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor();
-        advisor.setAdvice(new AsyncExecutionInterceptor(executor));
-        advisor.setPointcut(jdkRegexpMethodPointcut);
-
-        factory.addAdvisor(advisor);
-
-        return (SpringBootPlatformHttpConsumer) factory.getProxy();
+        return new SpringBootPlatformHttpConsumer(endpoint, processor, executor);
     }
 
     @Override
diff --git a/components-starter/camel-platform-http-starter/src/test/java/org/apache/camel/component/platform/http/springboot/SpringBootPlatformHttpCertificationTest.java b/components-starter/camel-platform-http-starter/src/test/java/org/apache/camel/component/platform/http/springboot/SpringBootPlatformHttpCertificationTest.java
index 4ddf6722a48..9f9af941ec0 100644
--- a/components-starter/camel-platform-http-starter/src/test/java/org/apache/camel/component/platform/http/springboot/SpringBootPlatformHttpCertificationTest.java
+++ b/components-starter/camel-platform-http-starter/src/test/java/org/apache/camel/component/platform/http/springboot/SpringBootPlatformHttpCertificationTest.java
@@ -27,11 +27,9 @@ import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 import java.util.Random;
-import java.util.concurrent.Callable;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
-import java.util.concurrent.TimeUnit;
 
 import io.restassured.RestAssured;
 import io.restassured.http.ContentType;
@@ -400,8 +398,7 @@ public class SpringBootPlatformHttpCertificationTest extends PlatformHttpBase {
                         detailedCookie()
                                 .value("bar")
                                 .path(CookieConfiguration.DEFAULT_PATH)
-                                .domain((String) null)
-                                .sameSite(CookieConfiguration.DEFAULT_SAME_SITE.getValue()))
+                                .domain((String) null))
                 .body(equalTo("add"));
     }
 }