project-reactor

Kotlin to achieve multithread request hedging?

核能气质少年 提交于 2020-01-24 18:55:31
问题 Spring's reactor has an interesting feature : Hedging . It means spawning many requests and get the first returned result , and automatically clean other contexts. Josh Long recently has been actively promoting this feature. Googling Spring reactor hedging shows relative results. If anybody is curious , here is the sample code . In short , Flux.first() simplifies all the underlaying hassles , which is very impressive. I wonder how this can be achieved with Kotlin's coroutine and multithread ,

Reactive Spring does not support ServerHttpRequest as parameter in REST endpoint tests?

孤街浪徒 提交于 2020-01-24 15:36:05
问题 The question is very similar to this one. Except the fact that I use: org.springframework.http.server.ServerHttpRequest not HttpServletRequest. The exception is got in test code. Real calls works. Code: @RunWith(SpringRunner.class) @SpringBootTest(classes = SecurityTests.SecurityTestsApplication.class) @TestPropertySource(properties = {""}) @AutoConfigureWebTestClient public class SecurityTests { @Test public void myTest() { //send request to myUrl and got 500 } } @RestController

ReactiveSecurityContextHolder.getContext() is empty but @AuthenticationPrincipal works

本秂侑毒 提交于 2020-01-23 17:40:06
问题 I've been using ReactiveAuthenticationManager in Spring Security + Webflux. It is customised to return an instance of UsernamePasswordAuthenticationToken which from what I can tell is an what I should be receiving when I call ReactiveSecurityContextHolder.getContext().map(ctx -> ctx.getAuthentication()).block() . As as far as I can tell I am unable to access the authentication context through both: SecurityContextHolder.getContext().getAuthentication(); or ReactiveSecurityContextHolder

what does Mono.defer() do?

大城市里の小女人 提交于 2020-01-20 02:20:27
问题 I've come across Mono.defer() in some Spring webflux code I looked up the method in the docs but don't understand the explanation: "Create a Mono provider that will supply a target Mono to subscribe to for each Subscriber downstream" please could I have an explanation and an example. Is there a place with a bunch of Reactor example code (their unit tests?) that I might reference. thanks 回答1: It is a bit of an oversimplification but conceptually Reactor sources are either lazy or eager. More

Flux.any correct use

╄→尐↘猪︶ㄣ 提交于 2020-01-16 14:19:39
问题 I have a Flux.fromIterable(list of ids) . I want to find out if any of the record is null .So I am trying to use Flux.any but I see that I does not even print anything inside any and directly goes to doOnEach as a result output is false. How can we fix this? The solution should not be limited to null check.It can be any boolean condition. Mono<Boolean> isAnyNull =Flux.fromIterable(request.getIds()) .switchIfEmpty(Mono.error(new SomeException("No elements"))) .flatMap(id->{ return FooRepo.find

How to create a Mono from a completableFuture

被刻印的时光 ゝ 提交于 2020-01-15 12:18:07
问题 I am trying to wrap CompletableFuture inside a Reactor Mono type in order to simplify my transform operations. Project Reactor is more convenient in general! I am working inside an AWS Lambda function and I am invoking AWS services such as S3, SQS, etc... using the new AWS Java SDK 2.x version. This new SDK allows to make asynchronous calls to AWS services and returns CompleteableFuture objects. For example: S3AsyncClient s3AsyncClient = S3AsyncClient.builder().build(); Mono.fromFuture

How to create a Mono from a completableFuture

穿精又带淫゛_ 提交于 2020-01-15 12:18:05
问题 I am trying to wrap CompletableFuture inside a Reactor Mono type in order to simplify my transform operations. Project Reactor is more convenient in general! I am working inside an AWS Lambda function and I am invoking AWS services such as S3, SQS, etc... using the new AWS Java SDK 2.x version. This new SDK allows to make asynchronous calls to AWS services and returns CompleteableFuture objects. For example: S3AsyncClient s3AsyncClient = S3AsyncClient.builder().build(); Mono.fromFuture

How to handle errors in Spring reactor Mono or Flux?

点点圈 提交于 2020-01-15 09:36:28
问题 I have below code retuning Mono<Foo>: try { return userRepository.findById(id) // step 1 .flatMap(user -> barRepository.findByUserId( user.getId()) // step 2 .map(bar-> Foo.builder().msg("Already exists").build()) // step 3 .switchIfEmpty(barRepository.save(Bar.builder().userId(user.getId()).build()) // step 4 .map(bar-> Foo.builder().msg("Created").build()) // step 5 )) .doOnError(throwable -> Mono.just(handleError(throwable))); } catch(Exception e) { log.error("from catch block"); return

The most efficient way to split a Flux to multiple Fluxes in Reactor 3

徘徊边缘 提交于 2020-01-15 07:30:12
问题 In Reactor 3, what's the most efficient way to split a heterogeneous flux to multiple fluxes by pattern matching? (And subsequent operations on each flux may be very different) For example, Source Flux: a->b->c->a->b->c || vv A Flux: a->a->a B Flux: b->b->b C Flux: c->c->c I'm new to reactive programming, and the only solution I come up with is share() + filter() , like val shared = flux.share(); shared.filter(x -> x.tag=='a').subscribe(a -> consumeA(a)); shared.filter(x -> x.tag=='b')

Using both publishOn and subscribeOn on a flux results in nothing happening

て烟熏妆下的殇ゞ 提交于 2020-01-12 08:05:44
问题 Whenever i use both subscribeOn and publishOn nothing is printed. If I use only one it will print. If I use subscribeOn(Schedulers.immediate()) or elastic it works. Any ideea why that is? It was my understanding that publishOn affects on what thread it gets published and subscribe on on what thread the subscriber runs. Could you please point me in the right direction? fun test() { val testPublisher = EmitterProcessor.create<String>().connect() testPublisher .publishOn(Schedulers.elastic())