Created
March 31, 2019 16:09
-
-
Save maslick/28a97e32e67c0a1ef2339c7f4afede03 to your computer and use it in GitHub Desktop.
raw Netty
This file contains 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
import org.springframework.http.MediaType; | |
import org.springframework.http.server.reactive.HttpHandler; | |
import org.springframework.http.server.reactive.ReactorHttpHandlerAdapter; | |
import org.springframework.web.reactive.function.server.RouterFunction; | |
import org.springframework.web.reactive.function.server.RouterFunctions; | |
import org.springframework.web.reactive.function.server.ServerResponse; | |
import reactor.core.publisher.Mono; | |
import reactor.netty.DisposableServer; | |
import reactor.netty.http.server.HttpServer; | |
import static org.springframework.web.reactive.function.server.RequestPredicates.GET; | |
import static org.springframework.web.reactive.function.server.RouterFunctions.route; | |
import static org.springframework.web.reactive.function.server.ServerResponse.ok; | |
public class CleanNetty { | |
public static void main(String[] args) { | |
RouterFunction<ServerResponse> myroute = route(GET("/pavel"), request -> { | |
Mono<Datta> data = Mono.justOrEmpty(new Datta("asd")); | |
return ok().contentType(MediaType.APPLICATION_JSON).body(data, Datta.class); | |
}); | |
HttpHandler handler = RouterFunctions.toHttpHandler(myroute); | |
HttpServer server = HttpServer.create().host("localhost").port(8080); | |
startServer(server.handle(new ReactorHttpHandlerAdapter(handler)).bindNow()); | |
} | |
private static void startServer(DisposableServer disposableServer) { | |
System.setProperty("io.netty.tryReflectionSetAccessible", "false"); | |
Thread awaitThread = new Thread("server") { | |
@Override | |
public void run() { | |
disposableServer.onDispose().block(); | |
} | |
}; | |
awaitThread.setContextClassLoader(CleanNetty.class.getClassLoader()); | |
awaitThread.setDaemon(false); | |
awaitThread.start(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment