This is an automated email from the ASF dual-hosted git repository. ffang pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel-spring-boot-examples.git
The following commit(s) were added to refs/heads/main by this push: new aa7c0c5 [CAMEL-20683]use spring-boot-starter-undertow as the embedded servlet server and demonstrate how to configure undertow handlers aa7c0c5 is described below commit aa7c0c5f92e73a0aeb5b63b15a110c894c3e91dd Author: Freeman Fang <freeman.f...@gmail.com> AuthorDate: Tue Apr 16 10:41:35 2024 -0400 [CAMEL-20683]use spring-boot-starter-undertow as the embedded servlet server and demonstrate how to configure undertow handlers --- soap-cxf/pom.xml | 12 ++++- .../java/sample/camel/UndertowConfiguration.java | 53 ++++++++++++++++++++++ .../src/test/java/sample/camel/WsdlClientTest.java | 42 +++++++++++++++++ 3 files changed, 106 insertions(+), 1 deletion(-) diff --git a/soap-cxf/pom.xml b/soap-cxf/pom.xml index 188a4b0..37659c9 100644 --- a/soap-cxf/pom.xml +++ b/soap-cxf/pom.xml @@ -65,13 +65,23 @@ </dependencyManagement> <dependencies> <dependency> - <groupId>org.apache.camel.springboot</groupId> + <groupId>org.apache.camel.springboot</groupId> <artifactId>camel-spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> + <exclusions> + <exclusion> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-tomcat</artifactId> + </exclusion> + </exclusions> </dependency> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-undertow</artifactId> + </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> 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 0000000..0da059e --- /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<UndertowServletWebServerFactory> { + @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 e27dcda..f234ca1 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 @@ package sample.camel; 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 com.example.customerservice.NoSuchCustomerException; 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 @@ public class WsdlClientTest { 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<Customer> 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 {