BAEL-4107: Spring MVC Async vs Spring WebFlux

This commit is contained in:
maryarm 2020-08-15 14:20:40 +02:00
parent 932ca7da9e
commit e36678d920
7 changed files with 120 additions and 1 deletions

View File

@ -1,2 +1,3 @@
# Files #
*.log
*.ipr

View File

@ -0,0 +1,11 @@
package com.baeldung.spring.asyncvsflux;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class AsyncVsWebFluxApp {
public static void main(String[] args) {
SpringApplication.run(AsyncVsWebFluxApp.class, args);
}
}

View File

@ -0,0 +1,17 @@
package com.baeldung.spring.asyncvsflux;
import java.time.Duration;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;
@RestController
public class WebFluxController {
@GetMapping("/flux_result")
public Mono<String> getResult(ServerHttpRequest request) {
return Mono.defer(() -> Mono.just("Result is ready!"))
.delaySubscription(Duration.ofMillis(500));
}
}

View File

@ -0,0 +1,20 @@
package com.baeldung.spring.asyncvsflux;
import java.time.Duration;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilterChain;
import reactor.core.publisher.Mono;
@Component
public class WebFluxFilter implements org.springframework.web.server.WebFilter {
@Override
public Mono<Void> filter(ServerWebExchange serverWebExchange, WebFilterChain webFilterChain) {
return Mono
.delay(Duration.ofMillis(200))
.then(
webFilterChain.filter(serverWebExchange)
);
}
}

View File

@ -0,0 +1,23 @@
package com.baeldung.asyncvsflux;
import java.util.concurrent.CompletableFuture;
import javax.servlet.http.HttpServletRequest;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class AsyncController {
@GetMapping("/async_result")
@Async
public CompletableFuture<String> getResultAsyc(HttpServletRequest request) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
return CompletableFuture.completedFuture("Result is ready!");
}
}

View File

@ -0,0 +1,34 @@
package com.baeldung.asyncvsflux;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import org.springframework.stereotype.Component;
@Component
public class AsyncFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
filterChain.doFilter(servletRequest, servletResponse);
}
@Override
public void destroy() {
}
}

View File

@ -0,0 +1,13 @@
package com.baeldung.asyncvsflux;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
@SpringBootApplication
@EnableAsync
public class AsyncVsWebFluxApp {
public static void main(String[] args) {
SpringApplication.run(AsyncVsWebFluxApp.class, args);
}
}