Using the sample HttpClient/HttpServer examples in github, I am trying to print a response from the sample HttpServer when the HttpClient subscribes rather than blocks. Here is the sample HttpServer used:
public class Server {
public static void main(String[] args) {
DisposableServer server =
HttpServer.create()
.host("10.0.0.19")
.port(61005)
.route(routes ->
routes.get("/hello",
(request, response) -> response.sendString(Mono.just("Hello World!")))
.post("/echo",
(request, response) -> response.sendString(Mono.just("Hello World!"))))
.bindNow();
server.onDispose()
.block();
}
}
Here are the maven dependencies used by the HttpClient and HttpServer:
<dependencies>
<dependency>
<groupId>io.projectreactor.netty</groupId>
<artifactId>reactor-netty</artifactId>
<version>0.9.7.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>
</dependencies>
If the HttpClient blocks, the request and response work correctly as in this blocking code:
public class Client {
private static final Logger log = Logger.getLogger(Client.class.getSimpleName());
public static void main(String[] args) {
String responseStr = HttpClient.create()
.tcpConfiguration(tcpClient -> tcpClient.host("10.0.0.19"))
.port(61005)
.post()
.uri("/echo")
.send(ByteBufFlux.fromString(Mono.just("Hello")))
.responseContent()
.aggregate()
.asString()
.block();
System.out.println(responseStr);
}
}
But is the HttpClient subcribes with onSuccess, onError and onCompletion callbacks, there is no response, that is, neither onSuccess, onError or onCompletion are executed:
public class Client {
private static final Logger log = Logger.getLogger(Client.class.getSimpleName());
public static void main(String[] args) {
Consumer<String> onSuccess = (String response) -> {
log.info("response in onSuccess: "+response);
};
Consumer<Throwable> onError = (Throwable ex) -> {
ex.getMessage();
};
Runnable onCompletion = () -> {
System.out.println("Message Completed");
};
HttpClient.create()
.tcpConfiguration(tcpClient -> tcpClient.host("10.0.0.19"))
.port(61005)
.post()
.uri("/echo")
.send(ByteBufFlux.fromString(Mono.just("Hello")))
.responseContent()
.aggregate()
.asString()
.subscribe(onSuccess, onError, onCompletion);
}
}
I have not been able to find an example that uses subscribe(). In the above example of the HttpClient using subscribe(), the HttpServer does not appear to be returning a response. Any incite into why this appears to be happening would be helpful.