Skip to content

Commit

Permalink
[CAMEL-20683]use spring-boot-starter-undertow as the embedded servlet…
Browse files Browse the repository at this point in the history
… server and demonstrate how to configure undertow handlers
  • Loading branch information
ffang committed Apr 16, 2024
1 parent 17bc93e commit aa7c0c5
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 1 deletion.
12 changes: 11 additions & 1 deletion soap-cxf/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
53 changes: 53 additions & 0 deletions soap-cxf/src/main/java/sample/camel/UndertowConfiguration.java
Original file line number Diff line number Diff line change
@@ -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);
}
});
}
});


}
}
42 changes: 42 additions & 0 deletions soap-cxf/src/test/java/sample/camel/WsdlClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -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<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 {
Expand Down

0 comments on commit aa7c0c5

Please sign in to comment.