Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions spring-cloud-function-web/pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-cloud-function-web</artifactId>
Expand Down Expand Up @@ -31,7 +32,7 @@
<dependency>
<groupId>io.projectreactor.ipc</groupId>
<artifactId>reactor-netty</artifactId>
<version>0.5.1.RELEASE</version>
<version>0.6.0.BUILD-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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);
}

Expand All @@ -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;
Copy link
Contributor

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?

Copy link
Contributor Author

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.


private LifecycleAwareHttpServer(HttpHandler handler, int port) {
this.handler = handler;
Expand All @@ -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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this return?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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;
}
}
}
Expand Down