Spring Graphql Client code executing a query to receive a flux - spring

Anyone know who to use the Spring graphql Client to receive a Flux of objects ? https://docs.spring.io/spring-graphql/docs/current/reference/html/#client.httpgraphqlclient
What expression would you code for the client to execute a query to receive a flux of objects
Trying to solve a problem and expecting an answer

Related

Calling Multiple apis synchronously, spring boot webflux

In my project we are using spring boot webflux, and I have this scenario where i have to call multiple api's from within a particular microservice synchronously.
e.g.
for(String api:apiNames){
//call api's
}
As per my understanding, webClient works asynchronously, until and unless someone subscribe to it it wont release the response.
in my current scenario i have to make use of webclient and I have to call each api only after successfull execution of previous api.
Note:- api response can be anything success/failure
Please help me in implementing this synchronous call
You can use the #block() method to make it synchronous.
Example on a mono (https://projectreactor.io/docs/core/release/api/reactor/core/publisher/Mono.html#block--)
For example on the (asynchronous) webflux webclient:
return webClient.get()
.uri(uri)
.accept(MediaType.APPLICATION_JSON)
.retrieve()
.bodyToMono(String.class)
.block();
The above example will wait with executing any next line of code untill it has received a response.
After which you can just make another call to another api in the same way.
Edit: As pointed out in the comments I would like to add a warning that using #block() is not efficient and you should try and chain more reactive calls and try to avoid making your application non reactive.
A nice post with more details can be found here: https://stackoverflow.com/a/57365926/1326867
Although, the question uses the word "synchronously", the description rather seems to suggest that sequentiality is what is needed, meaning executing each request one after the other.
If that's the requirement, it can be implemented the following way with Reactor:
Flux.fromIterable(apiNames)
.concatMap(apiName -> webClient...) // concatMap ensures sequential execution
However, if the application is a blocking Spring application, then Nick Hol's answer is also a correct one.

Elastic search high/low rest client vs spring rest template

I am in a dilemma over to use spring's rest template or elasticsearch's own high/low rest client while searching in es . Does es client provide any advantage like HTTP connection pooling , performance while compared to spring rest template . Which of the two take less time in getting response from the server . Can some one please explain this ?
The biggest advantage of using Spring Data Elasticsearch is that you don't have to bother about the things like converting your requests/request bodies/responses from your POJO domain classes to and from the JSON needed by Elasticsearch. You just use the methods defined in the ElasticsearchOperations class which is implemented by the *Template classes.
Or going one abstraction layer up, use the Repository interfaces the all the Spring Data modules provide to store and search/retrieve your data.
Firstly, This is a very broad question. Not sure if it suits the SO guidelines.
But my two cents:
High Level Client uses Low Level client which does provide connection pooling
High Level client manages the marshalling and unmarshalling of the Elastisearch query body and response, so it might be easier to work using the APIs.
On the other hand, if you are familiar with the Elasticsearch querying by providing the JSON body then you might find it a bit difficult to translate between the JSON body and the Java classes used for creating the query (i.e when you are using Kibana console or other REST API tools)
I generally overcome this by logging the query generated by the Java API so that I can use it with Kibana console or other REST API tools.
Regarding which one is efficient- the library will not matter that much to affect the response times.
If you want to use Spring Reactive features and make use of WebClient, ES Libraries do provide support for Async search.
Update:
Please check the answer by Wim Van den Brande below. He has mentioned a very valid point of using Transport Client which has been deprecated over REST API.
So it would be interesting to see how RestTemplate or Spring Data ElasticSearch will update their API to replace TransportClient.
One important remark and caveat with regards to the usage of Spring Data Elasticsearch. Currently, Spring Data Elasticsearch doesn't support the communication by the High Level REST Client API. They are using the transport client. Please note, the TransportClient is deprecated as of Elasticsearch 7.0.0 and is expected to be removed in Elasticsearch 8.0!!!
FYI, this statement has been confirmed already by another post: Elasticsearch Rest Client with Spring Data Elasticsearch

Mongo DB connections are not getting closed,While using reactive spring boot infinite streams

I am using Reactive Spring Boot (Flux) for infinite streams and #Tailable in a Reactive Mongo Repository to enable server side events. For each request to the Flux controller, it is creating a brand new MongoDB connection, although I am using the reactive Mongo client to create the connection. Because of this after 100 connections the application is throwing a MongoDB connection timeout exception. Below is my code.
REST Controller
Reactive Mongo Repository :
Please advise, if anyone knows how to fix this!

How does reactive java access data from couchbase?

I'm working with spring 5 and reactive programming with couchbase. Can anyone explain in detail how does reactive java pull in data from couchbase? How does couchbase provide reactive support? Thanks in advance.
The Couchbase Java client is implemented with RxJava 1, at least through the 2.x versions.
If you look at, for example, a document get operation, you can turn the operation into an observable stream by inserting a call to async. That is, bucket.async().get(id) returns a type of Observable<JsonDocument>.

Example of RabbitMQ with RPC in Spring Integration

After make a search about different ways to implement it, im stuck.
What im looking for is to realize this example (https://www.rabbitmq.com/tutorials/tutorial-six-spring-amqp.html) with Spring Integration.
I had found interesting post as this (Spring integration with Rabbit AMQP for "Client Sends Message -> Server Receives & returns msg on return queue --> Client get correlated msg") but didn't help me with what i need.
My case mill be a system where a client call the "convertSendAndReceive" method and a server (basede on Spring Integration) will response.
Thanks
According to your explanation it sounds like Outbound Gateway on the Client side and Inbound Gateway on the Server side pair is what you need.
Spring Integration AMQP support provides those implementations for you with built-in correlation functionality: https://docs.spring.io/spring-integration/docs/5.0.0.RELEASE/reference/html/amqp.html

Resources