Reputation: 1209
I'm trying to use WebClient to make massive Http POST request.
I put the logging level on reactor.ipc.netty
to DEBUG to see the request being sent.
Here is a functioning code :
@Service
public class HttpService implements IHttpService {
private static final String URL = "http://blablabla.com/bla";
private static final Logger logger = LogManager.getLogger();
@Autowired
WebClient webClient;
@Override
public void push(Data data) {
String body = constructString(data);
Mono<ClientResponse> res = webClient.post()
.uri(URL + getLogType(data))
.contentLength(body.length())
.contentType(MediaType.APPLICATION_JSON)
.syncBody(body)
.exchange();
ClientResponse resp = res.block();
logger.debug("Status : " + resp.statusCode());
logger.debug("Body : " + resp.bodyToMono(String.class));
}
}
It produces that kind of log :
2018-05-16 15:54:14.642 DEBUG 19144 --- [ctor-http-nio-4] r.i.n.channel.ChannelOperationsHandler : [id: 0x439f7819, L:/127.0.0.1:56556 - R:blablabla.com/127.0.0.1:8069] Writing object DefaultHttpRequest(decodeResult: success, version: HTTP/1.1)
POST /bla HTTP/1.1
user-agent: ReactorNetty/0.7.7.RELEASE
host: blablabla.com/bla:8069
accept: */*
accept-encoding: gzip
Content-Length: 494
Content-Type: application/json
But when I delete ClientResponse resp = res.block();
I don't see the log anymore... So I don't even know if the request is processed or not.
How can I process the response as soon as I get it ?
I tried with res.doOnSuccess(clientResponse -> logger.debug("Code : " + clientResponse.statusCode()));
but with no success...
Upvotes: 2
Views: 8178
Reputation: 59221
Because Flux
and Mono
are Reactive Streams types, they are also lazy: nothing happens until you subscribe
to it. There are several methods that can achieve that goal, and subscribe
or block
are amongst those.
Usually, Spring WebFlux applications don't subscribe
directly and actually return those reactive types; without more context in your case, I can't really say what would be the right approach here.
Check out the Reactor project reference documentation on Flux
and Mono
, this should help you understand the core principles behind this.
Upvotes: 5