简体   繁体   中英

In a Reactor Netty HttpClient/HttpServer configuration, I am not able to get the retrieve the request object on a post to the Server

A Reactor-Netty HttpClient is sending a request, HttpClientRequest, to an HttpServer. On the server side, I am not able to get the request object out of the HttpServerRequest. I can retreive the FluxByteBuf from the HttpServerRequest, but not the ByteBuf object in the Flux object. Typically, subscribing to the Flux would allow me to get the request object, but that is not working here. The response back from the server is being received by the client successfully. Does anyone know why subscribing to a Flux/Mono would not work on the server side of a Reactor-Netty client/server?

The client code is:

public class Client {

    private static final Logger log = Logger.getLogger(Client.class.getSimpleName());

    public static void main(String[] args) throws InterruptedException {

        Consumer<byte[]> onSuccess = (byte[] response) -> {
            ElectionResponse electionResponse = SerializationUtils.deserialize(response);
            log.info("response in onSuccess: "+electionResponse);

        };
        Consumer<Throwable> onError = (Throwable ex) -> {
            ex.getMessage();
        };

        Runnable onCompletion = () -> {
            System.out.println("Message Completed");

        };
        ElectionRequest electionRequest = new ElectionRequest("aRequest");
        byte[] requestBytes = SerializationUtils.serialize(electionRequest);
        ByteBuf requestByteBuf = Unpooled.copiedBuffer(requestBytes);

        HttpClient.create()
                .tcpConfiguration(tcpClient -> tcpClient.host("10.0.0.19"))
                .port(61005)
                .post()
                .uri("/echo")
                .send(Mono.just(requestByteBuf))
                .responseContent()
                .aggregate()
                .asByteArray()
                .subscribe(onSuccess, onError, onCompletion);

        try {
            Thread.sleep(15000);
        } catch(InterruptedException ie) {
            ie.printStackTrace();
        }
    }
}

The server code is:

public class Server {

    private static final Logger log = Logger.getLogger(Server.class.getSimpleName());

    public static void main(String[] args) {

        ElectionResponse electionResponse = new ElectionResponse("aResponse");
        byte[] responseArray = SerializationUtils.serialize(electionResponse);

        Consumer<ByteBuf> onSuccess = (ByteBuf request) -> {
            System.out.println("onSuccess: Request received!");
        };
        Consumer<Throwable> onError = (Throwable ex) -> {
            ex.getMessage();
        };
        Runnable onCompletion = () -> {
            System.out.println("Message Completed");

        };

        DisposableServer server =
            HttpServer.create()
                      .host("10.0.0.19")
                      .port(61005)
                      .route(routes ->
                          routes
                            .post("/echo",
                                (request, response) -> {
                                    request.receive().retain().next().subscribe(onSuccess, onError, onCompletion);
                                    return response.send(Mono.just(Unpooled.copiedBuffer(responseArray).retain()));
                                                       }
                                    ))
                        .bindNow();

        server.onDispose()
                .block();
    }
}

The environment and maven dependencies are as follows:

Apache Maven 3.6.1
Maven home: /usr/share/maven
Java version: 11.0.6, vendor: Oracle Corporation, runtime: /home/linuxlp/opt/graalvm/graalvm-svm-linux-20.1.0-ea+28
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "5.3.0-51-generic", arch: "amd64", family: "unix"

    <dependencies>
        <dependency>
            <groupId>io.projectreactor.netty</groupId>
            <artifactId>reactor-netty</artifactId>
            <version>0.9.0.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-core</artifactId>
            <version>3.3.5.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.reactivestreams</groupId>
            <artifactId>reactive-streams</artifactId>
            <version>1.0.3</version>
        </dependency>

What do you think about composing the request/response like this

public class Server {

    private static final Logger log = Logger.getLogger(Server.class.getSimpleName());

    public static void main(String[] args) {

        Consumer<byte[]> onSuccess = (byte[] request) -> {
            System.out.println("onSuccess: Request received!");
        };
        Consumer<Throwable> onError = (Throwable ex) -> {
            ex.getMessage();
        };
        Runnable onCompletion = () -> {
            System.out.println("Message Completed");

        };

        DisposableServer server =
                HttpServer.create()
                        .host("10.0.0.19")
                        .port(61005)
                        .route(routes ->
                                routes.post("/echo",
                                        (request, response) ->
                                                response.send(request.receive()
                                                                     .aggregate()
                                                                     .asByteArray()
                                                                     .doOnNext(onSuccess)
                                                                     .doOnError(onError)
                                                                     .doOnTerminate(onCompletion)
                                                                     .flatMap(bytes -> {
                                                                         ElectionRequest electionRequest = (ElectionRequest) SerializationUtils.deserialize(bytes);
                                                                         ElectionResponse electionResponse = new ElectionResponse(electionRequest.getStr());
                                                                         return Mono.just(Unpooled.copiedBuffer(SerializationUtils.serialize(electionResponse)));
                                                                     }))))
                        .bindNow();

        server.onDispose()
                .block();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM