TcpClient Only Returns First Result in Spring WebFlux Controller - spring-webflux

I have an HTTP service exposing a GET endpoint that connects to a simple echo server via TCP. The HTTP service is running on Netty.
#RestController
public class OurTcpClient {
private Connection connection;
#GetMapping("echo1")
public Mono<String> echo(#RequestParam("value") final String value) {
this.connection.outbound()
.sendString(Mono.just(String.format("%04d", value.length()) + value)) // prepend length
.then()
.subscribe();
return this.connection.inbound()
.receive()
.asString()
.next();
}
#PostConstruct
public void init() {
this.connection = TcpClient.create()
.host("localhost")
.port(10002)
.wiretap(true)
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000)
.option(ChannelOption.SO_KEEPALIVE, true)
.connectNow();
}
}
My expectation is that I can query the service at, for example, http://localhost:8081/echo1?value=hi, as many times as I like, and receive "hi" back in each response. This works for the first request. The second request hangs indefinitely. If I then cancel the second request and attempt another, I get the following error:
{
"timestamp": "2020-04-13T18:56:40.221+0000",
"path": "/echo1",
"status": 500,
"error": "Internal Server Error",
"message": "Only one connection receive subscriber allowed."
}
Any help would be greatly appreciated.

In the example you use next()
return this.connection.inbound()
.receive()
.asString()
.next();
According to the Flux#next javadoc Emit only the first item emitted by this Flux, into a new Mono., then the subscription will be canceled.
In the context of Reactor Netty when you use next, timeout, take etc. operators that cancel the subscription this means that the connection will be closed.

Related

Abort / Stop SignalR connection from server with a message and status code

I'm creating a SignalR server and I added a couple of rules that the clients should follow when they want to connect to server.
The rules (also call them 'validators') are, for example, that a certain header should be present when the client request to connect.
My question is: how can "reject" a connection with a proper "status code" and "message" and kick out the user?
I didn't find any helpful thread around.
Thanks for reading.
I Checked the hub class and found:
and if the connection is assciated with httprequest,you could use Context.GetHttpContext() method to get the httpcontext,
So I tried as below:
public override async Task OnConnectedAsync()
{
var errormessage = "the connection was disconnected due to Some reason";
var header = Context.GetHttpContext().Request.Headers;
if (header.ContainsKey("Origin"))
{
await Clients.Caller.SendAsync("Disconnect", errormessage);
Context.Abort();
.......
}
}
The Result:

Common processing/validation of response body in WebClient

I have a REST-like service I POST requests to using WebFlux WebClient. The service returns response in a common JSON format, something like:
{
"status": "OK",
"data": []
}
Now for each WebClient invocation for each endpoint I would like to perform common validation to check if status == "OK". Do I need to invoke the validation separately for each endpoint, e.g.
myClient.post().uri("/myEndpoint1")
//..
.retrieve()
.bodyToMono(MyResponse.class)
.map(this::validateResponse)
//..
Or is there a way to add some common processing while creating the WebClient. I tried using a filter
this.myClient = WebClient.builder()
.clientConnector(new ReactorClientHttpConnector(HttpClient.create().wiretap()))
.filter(ExchangeFilterFunction.ofResponseProcessor(this::validateMyResponseAsFilter))
.baseUrl(mybaseUrl)
.build();
where validateMyResponseAsFilter is
private Mono<ClientResponse> validateMyResponseAsFilter(ClientResponse resp) {
return resp.bodyToMono(MyResponse.class)
.flatMap(myResponse -> "OK".equals(myResponse.getStatus()) ? Mono.just(resp) : Mono.error(new RuntimeException()));
}
but this results in
org.springframework.web.reactive.function.UnsupportedMediaTypeException: Content type 'application/octet-stream' not supported for bodyType=my.package.MyResponse
Turned out that the service I connected to did not return Content-Type header. After fixing the service, the code works correctly.

Quarkus AMQP send message to queue after request business logic

Once I receive a HTTP Get/Post I have to persist and object and then send a message to a queue where other services are listening to start doing other complex work
My current issue is that I can't just call a method with an #Outgoing("channel") annotation, I tried that and just keeps on executing the method without calling
Is there a way to call a method to send a JSON payload to a queue using the Quarkus framework?
PS: Im also trying to use rabbitMQ and switched back to ActiveMQ
Ive followed the Quarkus tuturial on reactive messaging and tried to register something on in implemented resource, but no luck
#Path("/part")
class PartService : PanacheRepository<PartDao>, Logging {
#GET
#Produces(MediaType.APPLICATION_JSON)
#Transactional
fun fetchParts(): List<PartDao> {
val partDao = PartDao(label = "Test", status = PartStatus.INBANK, creatorId = "ghost-007")
partDao.persist()
if (partDao.isPersistent) {
// Send a message to a queue -> PoC
send(partDao)
}
return findAll().list()
}
#Outgoing("part-persisted")
#Transactional
fun send(partDao: PartDao): CompletionStage<AmqpMessage<*>> {
val future = CompletableFuture<AmqpMessage<*>>()
val message = "hello from sender"
// Debug proposes
println("Sending (data): $message")
logger.debug(partDao.toString())
future.complete(AmqpMessage(message))
return future
}
}
Expected:
Register message "hello from sender" in queue after doing:
curl http://localhost/part
Actual results:
send method just keeps on executing
If I understand correctly, you want to call a method that would put something into a stream.
To my knowledge, you have to use an Emitter to do it, see e.g. https://github.com/michalszynkiewicz/devoxxpl-demo/blob/master/search/src/main/java/com/example/search/SearchEndpoint.java#L23
See https://smallrye.io/smallrye-reactive-messaging/#_stream documentation.

Is there a helper method to extract origin host from request (to build a link)

I receive request via a router :
#Bean
public RouterFunction<ServerResponse> routerFunction() {
return nest(path(API_PATH), route(GET("/"), indexHandler::getIndex));
}
handle by a method :
public Mono<ServerResponse> getIndex(ServerRequest request) {
...
}
I need to extract the url use to request the service, I have different cases, sometimes request are direct to service, sometimes request go through proxy (and add X-Forwarded-Path,X-Forwarded-Path headers).
Is there a helper method, to extract this details from ServerRequest object ?

Not getting response with Http Async Client

I am stuck with this weird situation where sometimes my HTTP requests don't go out or I don't get a HTTP response to my request sporadically. My application makes several (100s) http requests to other 3rd party service periodically most of which work absolutely fine.
I use the CloseableHttpAsyncClient (Version 4.0) with a custom HttpRequestIntercerptor and HttpResponseInterceptor. These were mainly added for debugging purpose with the RequestInterceptor is the last interceptor in the chain and the ResponseInterceptor is the first one. The idea was to log each http request at the last stage before it sends the actual request and to log each http response when it is first received.
I have the following pattern to setup the async client:
HttpAsyncClientBuilder asyncClientBuilder = HttpAsyncClientBuilder.create();
asyncClientBuilder.addInterceptorLast(new MyHttpRequestInterceptor());
asyncClientBuilder.addInterceptorFirst(new MyHttpResponseInterceptor());
IOReactorConfig reactorConfig = IOReactorConfig.DEFAULT;
reactorConfig.setConnectTimeout(5 * 60 * 1000); // 5 mins
reactorConfig.setSoTimeout(5 * 60 * 1000); // 5 mins
asyncClientBuilder.setDefaultIOReactorConfig(reactorConfig);
System.setProperty("http.maxConnections", "100");
this.asyncHttpClient = asyncClientBuilder.useSystemProperties().build();
this.asyncHttpClient.start();
To make the request I do:
HttpGet httpGet = new HttpGet("some url");
asyncHttpClient.execute(httpGet, new AsyncHTTPResponseHandler(requestMetadata));
Here is my AsyncHTTPResponseHandler class:
class AsyncHTTPResponseHandler implements FutureCallback<HttpResponse> {
// local copy of the request for reference while processing the response.
private RequestMetadata requestMetadata;
public AsyncHTTPResponseHandler(final RequestMetadata requestMetadata) {
this.setRequestMetadata(requestMetadata);
Thread.currentThread().setUncaughtExceptionHandler(new HttpUncaughtExceptionHandler(requestMetadata));
}
#Override
public void cancelled() {
logger.error("AsyncHTTPResponseHandler#Http request id: {} cancelled",
requestMetadata.getRequestId()));
}
#Override
public void completed(HttpResponse response) {
logger.debug("Received HTTP Response for request id: {}",
requestMetadata.getRequestId());
//handleHttpResponse(requestMetadata, response);
}
#Override
public void failed(Exception e) {
logger.error("AsyncHTTPResponseHandler#Error in Http request id: " + requestMetadata.getRequestId(), e);
}
}
Based on this setup, I see the following cases based on my interceptors logs:
1. My application http request triggers an asyncclient HttpRequest and I get the HttpResponse -- Success.
2. My application http request triggers an asyncclient HttpRequest (the interceptor logs it) and I don't get the HttpResponse for this request --- Don't know why?
3. My application http request does not trigger an asyncclient HttpRequest (the interceptor does not log it) and I don't get the HttpResponse for this request --- Don't know why?
Any tips or suggestions on what I can do fix this or debug this problem further?
Thanks!!
So, thought I will share my findings and solution here.
We were experiencing symptoms similar to this bug: https://issues.apache.org/jira/browse/HTTPASYNC-79
If you enable DEBUG logging for "org.apache.http.impl.nio" package, then you can see the exchanges. Note: The logs will be very verbose.
The issue was resolved by upgrading the HttpAsyncClient library from 4.0 to 4.0.2. I have also enabled socket and Connection timeouts. You should see timeout exceptions in the log files with this.
Here is how my HttpAsyncClient instance looks now:
HttpAsyncClientBuilder asyncClientBuilder = HttpAsyncClientBuilder.create();
asyncClientBuilder.addInterceptorLast(new MyHttpRequestInterceptor());
asyncClientBuilder.addInterceptorFirst(new MyHttpResponseInterceptor());
// reactor config
IOReactorConfig reactorConfig = IOReactorConfig.custom()
.setConnectTimeout(TIMEOUT_5_MINS_IN_MILLIS)
.setSoTimeout(TIMEOUT_5_MINS_IN_MILLIS).build();
asyncClientBuilder.setDefaultIOReactorConfig(reactorConfig);
// request config
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(TIMEOUT_5_MINS_IN_MILLIS)
.setConnectionRequestTimeout(TIMEOUT_5_MINS_IN_MILLIS)
.setSocketTimeout(TIMEOUT_5_MINS_IN_MILLIS).build();
asyncClientBuilder.setDefaultRequestConfig(requestConfig);
// connection config
ConnectionConfig connectionConfig = ConnectionConfig.custom()
.setMalformedInputAction(CodingErrorAction.IGNORE)
.setUnmappableInputAction(CodingErrorAction.IGNORE)
.build();
asyncClientBuilder.setDefaultConnectionConfig(connectionConfig);
System.setProperty("http.maxConnections", "100");
System.setProperty("http.conn-manager.timeout", "300000"); // 5 mins
this.asyncHttpClient = asyncClientBuilder.useSystemProperties().build();