Skip to content

Commit

Permalink
CAMEL-19182: camel-platform-http-starter - Async processing with Spri…
Browse files Browse the repository at this point in the history
…ng Boot (#1199)

- Spring MVC automatically detects CompletableFuture return type of the handler method and executes asynchronously; @responsebody on the handler method to disable attempts to resolve mvc view from a response.
- testing async execution with MockMvc
- @EnableAutoConfiguration instead of @SpringBootApplication on test classes to not scan another tests' configurations
  • Loading branch information
kulagaIA committed Aug 21, 2024
1 parent 670853c commit ed5530a
Show file tree
Hide file tree
Showing 6 changed files with 203 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.concurrent.CompletableFuture;
import org.apache.camel.Exchange;
import org.apache.camel.ExchangePattern;
import org.apache.camel.Processor;
Expand All @@ -31,8 +33,7 @@
import org.apache.camel.support.DefaultConsumer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import org.springframework.web.bind.annotation.ResponseBody;

public class SpringBootPlatformHttpConsumer extends DefaultConsumer implements PlatformHttpConsumer, Suspendable, SuspendableService {

Expand All @@ -57,21 +58,24 @@ public PlatformHttpEndpoint getEndpoint() {
/**
* This method is invoked by Spring Boot when invoking Camel via platform-http
*/
public 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);
@ResponseBody
public CompletableFuture<Void> service(HttpServletRequest request, HttpServletResponse response) {
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
}
}
});
}

protected void handleService(HttpServletRequest request, HttpServletResponse response) throws Exception {
Expand All @@ -94,8 +98,6 @@ protected void handleService(HttpServletRequest request, HttpServletResponse res
exchange.getIn().setHeader(Exchange.HTTP_PATH, httpPath.substring(contextPath.length()));
}

// TODO: async with CompletionStage returned to spring boot?

// we want to handle the UoW
try {
createUoW(exchange);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* 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 org.apache.camel.component.platform.http.springboot;

import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.spring.boot.CamelAutoConfiguration;
import org.apache.camel.test.spring.junit5.CamelSpringBootTest;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.annotation.DirtiesContext.ClassMode;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.asyncDispatch;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.request;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@EnableAutoConfiguration
@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
@CamelSpringBootTest
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = { CamelAutoConfiguration.class,
SpringBootPlatformHttpRestDSLTest.class, PlatformHttpAsyncRequestHandlingTest.TestConfiguration.class,
PlatformHttpComponentAutoConfiguration.class, SpringBootPlatformHttpAutoConfiguration.class, })
@AutoConfigureMockMvc
public class PlatformHttpAsyncRequestHandlingTest extends PlatformHttpBase {

@Autowired
private MockMvc mockMvc;

@Autowired
CamelContext camelContext;

private static final String postRouteId = "PlatformHttpAsyncRequestHandlingTest_mypost";

private static final String getRouteId = "PlatformHttpAsyncRequestHandlingTest_myget";

@Test
public void testGetAsync() throws Exception {
waitUntilRouteIsStarted(1, getGetRouteId());

MvcResult mvcResult = mockMvc.perform(get("/myget"))
.andExpect(status().isOk())
.andExpect(request().asyncStarted())
.andReturn();

mockMvc.perform(asyncDispatch(mvcResult))
.andExpect(status().isOk())
.andExpect(content().string("get"));
}

@Test
public void testPostAsync() throws Exception {
waitUntilRouteIsStarted(1, getPostRouteId());

MvcResult mvcResult = mockMvc.perform(post("/mypost").content("hello"))
.andExpect(status().isOk())
.andExpect(request().asyncStarted())
.andReturn();

mockMvc.perform(asyncDispatch(mvcResult))
.andExpect(status().isOk())
.andExpect(content().string("HELLO"));
}

// *************************************
// Config
// *************************************
@Configuration
public static class TestConfiguration {

@Bean
public RouteBuilder servletPlatformHttpRouteBuilder() {
return new RouteBuilder() {
@Override
public void configure() {
from("platform-http:/myget").id(getRouteId).setBody().constant("get");
from("platform-http:/mypost").id(postRouteId).transform().body(String.class, b -> b.toUpperCase());
}
};
}
}

@Override
protected String getPostRouteId() {
return postRouteId;
}

@Override
protected String getGetRouteId() {
return getRouteId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,46 @@
*/
package org.apache.camel.component.platform.http.springboot;

import org.junit.jupiter.api.Test;

import java.util.concurrent.TimeUnit;
import org.apache.camel.CamelContext;
import org.apache.camel.ServiceStatus;
import org.assertj.core.api.Assertions;
import org.awaitility.Awaitility;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.web.client.TestRestTemplate;

import static org.junit.jupiter.api.Assertions.assertEquals;

abstract class PlatformHttpBase {

@Autowired
private TestRestTemplate restTemplate;

@Autowired
CamelContext camelContext;

@Test
public void testGet() {
waitUntilRouteIsStarted(1, getGetRouteId());

Assertions.assertThat(restTemplate.getForEntity("/myget", String.class).getStatusCodeValue()).isEqualTo(200);
}

@Test
public void testPost() {
waitUntilRouteIsStarted(1, getPostRouteId());

Assertions.assertThat(restTemplate.postForEntity("/mypost", "test", String.class).getBody()).isEqualTo("TEST");
}

protected void waitUntilRouteIsStarted(int atMostSeconds, String routeId) {
Awaitility.await().atMost(atMostSeconds, TimeUnit.SECONDS).untilAsserted(() ->
assertEquals(ServiceStatus.Started, camelContext.getRouteController().getRouteStatus(routeId))
);
}

protected abstract String getPostRouteId();

protected abstract String getGetRouteId();
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,25 @@
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.spring.boot.CamelAutoConfiguration;
import org.apache.camel.test.spring.junit5.CamelSpringBootTest;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.annotation.DirtiesContext.ClassMode;

@SpringBootApplication
@DirtiesContext
@EnableAutoConfiguration
@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
@CamelSpringBootTest
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = { CamelAutoConfiguration.class,
SpringBootPlatformHttpRestDSLTest.class, SpringBootPlatformHttpRestDSLTest.TestConfiguration.class,
PlatformHttpComponentAutoConfiguration.class, SpringBootPlatformHttpAutoConfiguration.class, })
public class SpringBootPlatformHttpRestDSLTest extends PlatformHttpBase {

private static final String postRouteId = "SpringBootPlatformHttpRestDSLTest_mypost";

private static final String getRouteId = "SpringBootPlatformHttpRestDSLTest_myget";

// *************************************
// Config
// *************************************
Expand All @@ -44,13 +48,25 @@ public static class TestConfiguration {
public RouteBuilder springBootPlatformHttpRestDSLRouteBuilder() {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
rest().get("myget").to("direct:get").post("mypost").to("direct:post");
public void configure() {
rest()
.get("myget").id(getRouteId).to("direct:get")
.post("mypost").id(postRouteId).to("direct:post");

from("direct:post").transform().body(String.class, b -> b.toUpperCase());
from("direct:get").setBody().constant("get");
}
};
}
}

@Override
protected String getPostRouteId() {
return postRouteId;
}

@Override
protected String getGetRouteId() {
return getRouteId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,25 @@
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.spring.boot.CamelAutoConfiguration;
import org.apache.camel.test.spring.junit5.CamelSpringBootTest;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.annotation.DirtiesContext.ClassMode;

@SpringBootApplication
@DirtiesContext
@EnableAutoConfiguration
@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
@CamelSpringBootTest
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = { CamelAutoConfiguration.class,
SpringBootPlatformHttpTest.class, SpringBootPlatformHttpTest.TestConfiguration.class,
PlatformHttpComponentAutoConfiguration.class, SpringBootPlatformHttpAutoConfiguration.class })
public class SpringBootPlatformHttpTest extends PlatformHttpBase {

private static final String postRouteId = "SpringBootPlatformHttpTest_mypost";

private static final String getRouteId = "SpringBootPlatformHttpTest_myget";

// *************************************
// Config
// *************************************
Expand All @@ -44,11 +48,21 @@ public static class TestConfiguration {
public RouteBuilder servletPlatformHttpRouteBuilder() {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
from("platform-http:/myget").setBody().constant("get");
from("platform-http:/mypost").transform().body(String.class, b -> b.toUpperCase());
public void configure() {
from("platform-http:/myget").id(postRouteId).setBody().constant("get");
from("platform-http:/mypost").id(getRouteId).transform().body(String.class, b -> b.toUpperCase());
}
};
}
}

@Override
protected String getPostRouteId() {
return postRouteId;
}

@Override
protected String getGetRouteId() {
return getRouteId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#logging.level.org.springframework.web: DEBUG
#logging.level.org.springframework.boot.web: DEBUG

0 comments on commit ed5530a

Please sign in to comment.