From aa7c0c5f92e73a0aeb5b63b15a110c894c3e91dd Mon Sep 17 00:00:00 2001 From: Freeman Fang Date: Tue, 16 Apr 2024 10:41:35 -0400 Subject: [PATCH] [CAMEL-20683]use spring-boot-starter-undertow as the embedded servlet server and demonstrate how to configure undertow handlers --- soap-cxf/pom.xml | 12 ++++- .../sample/camel/UndertowConfiguration.java | 53 +++++++++++++++++++ .../java/sample/camel/WsdlClientTest.java | 42 +++++++++++++++ 3 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 soap-cxf/src/main/java/sample/camel/UndertowConfiguration.java diff --git a/soap-cxf/pom.xml b/soap-cxf/pom.xml index 188a4b0d..37659c96 100644 --- a/soap-cxf/pom.xml +++ b/soap-cxf/pom.xml @@ -65,13 +65,23 @@ - org.apache.camel.springboot + org.apache.camel.springboot camel-spring-boot-starter org.springframework.boot spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-tomcat + + + + org.springframework.boot + spring-boot-starter-undertow + org.springframework.boot spring-boot-starter-actuator diff --git a/soap-cxf/src/main/java/sample/camel/UndertowConfiguration.java b/soap-cxf/src/main/java/sample/camel/UndertowConfiguration.java new file mode 100644 index 00000000..0da059ef --- /dev/null +++ b/soap-cxf/src/main/java/sample/camel/UndertowConfiguration.java @@ -0,0 +1,53 @@ +/* + * 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 sample.camel; + + + +import org.springframework.boot.web.embedded.undertow.UndertowDeploymentInfoCustomizer; +import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory; +import org.springframework.boot.web.server.WebServerFactoryCustomizer; + +import org.springframework.stereotype.Component; + +import io.undertow.server.HandlerWrapper; +import io.undertow.server.HttpHandler; +import io.undertow.server.handlers.RequestLimitingHandler; +import io.undertow.servlet.api.DeploymentInfo; + +@Component +public class UndertowConfiguration implements WebServerFactoryCustomizer { + @Override + public void customize(UndertowServletWebServerFactory factory) { + factory.addDeploymentInfoCustomizers(new UndertowDeploymentInfoCustomizer() { + @Override + public void customize(DeploymentInfo deploymentInfo) { + // Enable RequestLimitingHandler + // we actually can configure all applicable undertow handlers here + deploymentInfo.addOuterHandlerChainWrapper(new HandlerWrapper() { + @Override + public HttpHandler wrap(HttpHandler handler) { + + return new RequestLimitingHandler(1, 1, handler); + } + }); + } + }); + + + } +} diff --git a/soap-cxf/src/test/java/sample/camel/WsdlClientTest.java b/soap-cxf/src/test/java/sample/camel/WsdlClientTest.java index e27dcda8..f234ca1f 100644 --- a/soap-cxf/src/test/java/sample/camel/WsdlClientTest.java +++ b/soap-cxf/src/test/java/sample/camel/WsdlClientTest.java @@ -18,8 +18,10 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; import jakarta.xml.ws.WebServiceException; + import org.apache.cxf.ext.logging.LoggingFeature; import org.apache.cxf.frontend.ClientProxyFactoryBean; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; @@ -39,6 +41,9 @@ import java.time.Duration; import java.time.LocalDate; import java.util.List; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class WsdlClientTest { @@ -62,6 +67,43 @@ protected CustomerService createCustomerClient() { public void before() { cxfClient = createCustomerClient(); } + + @Test + public void testRequestLimiting() throws Exception { + CountDownLatch latch = new CountDownLatch(50); + + ExecutorService executor = Executors.newFixedThreadPool(200); + + for (int i = 0; i < 50; i++) { + executor.execute(new SendRequest(latch)); + } + latch.await(); + } + + class SendRequest implements Runnable { + + CountDownLatch latch; + + SendRequest(CountDownLatch latch) { + this.latch = latch; + } + + @Override + public void run() { + try { + List customers = cxfClient.getCustomersByName("test"); + assertEquals(customers.get(0).getName(), "test"); + assertEquals(customers.get(0).getCustomerId(), 1); + + } catch (Exception ex) { + // some requests are expected to fail and receive 503 error + // cause Server side limit the concurrent request + assertTrue(ex.getCause().getMessage().contains("503: Service Unavailable")); + } finally { + latch.countDown(); + } + } + } @Test public void testGetCustomer() throws Exception {