Mono zip from list of monos - spring-boot

I started a new project with Springboot webflux with reactive programming. As a newbie I am still exploring Mono and Flux and how to use with Spring.
I have a userIdList which calls dao to get Mono object from DB and puts response in the list.
List<Mono<UserData>> list;
userIdList.foreach(id -> {
list.add(userDao.findByUserId(id))
})
All I wanted to achieve is to pass this list as parameters to Mono.zip so that I will combine these monos and construct a response object.
Mono.zip(...List<Mono<UserData>>).flatMap(data->{
data.getT1();
data.getT2();
return <your_response_object>;
});
Is there any better way to combine the individual mono responses and construct the response. Thanks in advance. As a newbie, any suggestion is appreciated.

Related

Is it possible to stream large data (object) in spring

We construct a large object currently in java and send it as text/xml. It seems the client times out. Is it possible to convert this object in byte array or String and stream the data.
Will it help? We are using spring boot.
You should use TEXT_EVENT_STREAM_VALUE with Spring reactive, but it is expensive(too much CPU/Memory)
Ideally spring controlller should return
return quasarService.findByReceivedStatus(organizationId, receiverId)
.delayElements(Duration.ofSeconds(delay))
.cache()
.take(50)
.onBackpressureDrop()
.repeat(3) /// if repeat number not specified connection will keep long..
.parallel(50); //ParallelFlux

I am new to Reactive Programming trying UPSERT operation using Mono and concat with existed value in repository

Here, In PUT method if it is empty invoking POST using REST API if not request payload going to save in repository
I need to save like append the existed field value with updated one. please if you any idea on this help me out
public Mono updateEmployee(int id,Employee emp){
return repo.existsById(id)
.switchIfEmpty(service.create(emp))
.flatMap(repo.save(emp);
}

Reactive Elasticsearch and Paging

I am using a Reactive client for Elasticsearch. Both of them (ReactiveElasticsearchTemplate or ReactiveCrudRepository) have support for Flux/Mono and this is working fine for me.
My current problem is that using ReactiveElasticsearchTemplate:
return reactiveElasticsearchTemplate.search(
new NativeSearchQueryBuilder()
.withPageable(PageRequest.of(0, 10))
.build(),
UserDocument.class
)
It is returning slice, but I don't have any information about total elements, as method signature returns Flux<SearchHit<UserDocument>>. Ideally, I would like to get SearchHits or something similar, but I don't know how to achieve it.
Currently this information is not available in the reactive implementation there is an open issue for that. The only possibility at the moment is sending a count request first.

How to get reactive publisher data in a collection of set and set this in another reactive publisher object class

I have two document model class. One is RuleSet and another is Rule.
One RuleSet can have many rules. But When we are saving, We are saving Rules in a diffrent document and RuleSet in different document.
When rest api call /ruleSet uri to get all detail of ruleSet,
I want to call ruleSetRepository.findAll() which returns Flux
and
then I want to call ruleRepository.findAll() and collect all rule as List
then
ruleSet.SetRules(list of rules from ruleRepository).
I need a code example to achieve this.
Thanks in advance.
It can be done roughly like this
rulesRepository
.findAll()
.collectList()
.map(l -> {
RuleSet ruleSet = new RuleSet("myRuleSet");
ruleSet.setRules(l);
return rulesSetRepository.save(ruleSet);
})
The rulesRepository.findAll would return a flux and collectList would transform it to a Mono<List<>> which can be mapped to Mono returned by save using the list. This code should be customized to handle synchronization and error handling.

Spring RestTemplate readin JSON Document

I have an elementary question using Spring RestTemplate for reading a JSON document. I make a get call to a URL which returns a JSON document in this format:
{
{
..
},
{
..
}
}
I couldn't find any examples for fetching this kind of data.. Would be helpful if someone could help me out with this...
I was able to solve this problem in my case using the firebase-client apis. That exposes a map of values for the kind of json i was talking about. Still, I would like to know how to do this using RestTemplate..

Resources