-
Notifications
You must be signed in to change notification settings - Fork 639
Update to latest reactor-netty stack #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,13 +16,6 @@ | |
|
|
||
| package org.springframework.cloud.function.web; | ||
|
|
||
| import static org.springframework.http.MediaType.TEXT_PLAIN; | ||
| import static org.springframework.web.reactive.function.BodyExtractors.toFlux; | ||
| import static org.springframework.web.reactive.function.BodyInserters.fromPublisher; | ||
| import static org.springframework.web.reactive.function.RequestPredicates.POST; | ||
| import static org.springframework.web.reactive.function.RequestPredicates.contentType; | ||
|
|
||
| import java.util.concurrent.Executors; | ||
| import java.util.function.Function; | ||
|
|
||
| import org.reactivestreams.Publisher; | ||
|
|
@@ -35,22 +28,30 @@ | |
| import org.springframework.cloud.function.registry.FunctionRegistry; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
| import org.springframework.http.MediaType; | ||
| import org.springframework.http.server.reactive.HttpHandler; | ||
| import org.springframework.http.server.reactive.ReactorHttpHandlerAdapter; | ||
| import org.springframework.util.StringUtils; | ||
| import org.springframework.web.reactive.function.Request; | ||
| import org.springframework.web.reactive.function.Response; | ||
| import org.springframework.web.reactive.function.RouterFunction; | ||
| import org.springframework.web.reactive.function.RouterFunctions; | ||
| import org.springframework.web.reactive.function.server.RequestPredicates; | ||
| import org.springframework.web.reactive.function.server.RouterFunction; | ||
| import org.springframework.web.reactive.function.server.RouterFunctions; | ||
| import org.springframework.web.reactive.function.server.ServerRequest; | ||
| import org.springframework.web.reactive.function.server.ServerResponse; | ||
|
|
||
| import static org.springframework.web.reactive.function.BodyExtractors.toFlux; | ||
| import static org.springframework.web.reactive.function.BodyInserters.fromPublisher; | ||
|
|
||
| import reactor.core.publisher.Flux; | ||
| import reactor.ipc.netty.http.HttpServer; | ||
| import reactor.core.publisher.Mono; | ||
| import reactor.ipc.netty.NettyContext; | ||
| import reactor.ipc.netty.http.server.HttpServer; | ||
|
|
||
| /** | ||
| * @author Mark Fisher | ||
| */ | ||
| @Configuration | ||
| @EnableConfigurationProperties({ FunctionConfigurationProperties.class, WebConfigurationProperties.class }) | ||
| @EnableConfigurationProperties({ FunctionConfigurationProperties.class, | ||
| WebConfigurationProperties.class }) | ||
| public class RestConfiguration { | ||
|
|
||
| @Autowired | ||
|
|
@@ -69,10 +70,13 @@ public HttpHandler httpHandler(FunctionRegistry registry) { | |
| String name = functionProperties.getName(); | ||
| Function<Flux<String>, Flux<String>> function = (name.indexOf(',') == -1) | ||
| ? registry.lookupFunction(name) | ||
| : registry.composeFunction(StringUtils.commaDelimitedListToStringArray(name)); | ||
| : registry.composeFunction( | ||
| StringUtils.commaDelimitedListToStringArray(name)); | ||
| FunctionInvokingHandler handler = new FunctionInvokingHandler(function); | ||
| RouterFunction<Publisher<String>> route = RouterFunctions.route( | ||
| POST(webProperties.getPath()).and(contentType(TEXT_PLAIN)), handler::handleText); | ||
| RouterFunction<ServerResponse> route = RouterFunctions.route( | ||
| RequestPredicates.POST(webProperties.getPath()) | ||
| .and(RequestPredicates.contentType(MediaType.TEXT_PLAIN)), | ||
| handler::handleText); | ||
| return RouterFunctions.toHttpHandler(route); | ||
| } | ||
|
|
||
|
|
@@ -89,20 +93,21 @@ private FunctionInvokingHandler(Function<Flux<String>, Flux<String>> function) { | |
| this.function = function; | ||
| } | ||
|
|
||
| private Response<Publisher<String>> handleText(Request request) { | ||
| private Mono<ServerResponse> handleText(ServerRequest request) { | ||
| Flux<String> input = request.body(toFlux(String.class)); | ||
| Publisher<String> output = this.function.apply(input); | ||
| return Response.ok().body(fromPublisher(output, String.class)); | ||
| return ServerResponse.ok().body(fromPublisher(output, String.class)); | ||
| } | ||
| } | ||
|
|
||
| private static class LifecycleAwareHttpServer implements InitializingBean, DisposableBean { | ||
| private static class LifecycleAwareHttpServer | ||
| implements InitializingBean, DisposableBean { | ||
|
|
||
| private final HttpHandler handler; | ||
|
|
||
| private final int port; | ||
|
|
||
| private volatile HttpServer server; | ||
| private volatile NettyContext server; | ||
|
|
||
| private LifecycleAwareHttpServer(HttpHandler handler, int port) { | ||
| this.handler = handler; | ||
|
|
@@ -111,27 +116,34 @@ private LifecycleAwareHttpServer(HttpHandler handler, int port) { | |
|
|
||
| @Override | ||
| public void afterPropertiesSet() { | ||
| ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(this.handler); | ||
| ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter( | ||
| this.handler); | ||
| final HttpServer server = HttpServer.create("localhost", port); | ||
| this.server = server; | ||
| Executors.newSingleThreadExecutor().submit(new Runnable() { | ||
| Thread thread = new Thread(new Runnable() { | ||
|
|
||
| @Override | ||
| public void run() { | ||
| try { | ||
| server.startAndAwait(adapter); | ||
| } | ||
| catch (InterruptedException e) { | ||
| Thread.currentThread().interrupt(); | ||
| LifecycleAwareHttpServer.this.server = server.newHandler(adapter) | ||
| .block(); | ||
| while (LifecycleAwareHttpServer.this.server != null) { | ||
| try { | ||
| Thread.sleep(100L); | ||
| } | ||
| catch (InterruptedException e) { | ||
| Thread.currentThread().interrupt(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should this
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably throw an exception? |
||
| } | ||
| } | ||
| } | ||
| }); | ||
| thread.setDaemon(false); | ||
| thread.start(); | ||
| } | ||
|
|
||
| @Override | ||
| public void destroy() { | ||
| if (this.server != null) { | ||
| this.server.shutdown(); | ||
| this.server.dispose(); | ||
| this.server = null; | ||
| } | ||
| } | ||
| } | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we change the variable name here as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know how stable it is. I don't mind either way. Just wanted something that works.
I figure we'll migrate to spring boot stack eventually anyway.