project-reactor

What kind of “EventBus” to use in Spring? Built-in, Reactor, Akka?

ε祈祈猫儿з 提交于 2019-12-03 00:53:39
问题 We're going to start a new Spring 4 application in a few weeks. And we'd like to use some event-driven architecture. This year I read here and there about "Reactor" and while looking for it on the web, I stumbled upon "Akka". So for now we have 3 choices: Spring's ApplicationEvent : http://docs.spring.io/spring/docs/4.0.0.RELEASE/javadoc-api/org/springframework/context/ApplicationEvent.html Reactor : https://github.com/reactor/reactor#reactor Akka : http://akka.io/ I couldn't find a real

Web Reactive Programming - What are the advantages from the HTTP client point of view?

寵の児 提交于 2019-12-02 23:11:33
Lets suppose these two scenarios of a controller that generates some random numbers with a delay: 1) Reactive Spring 5 reactive application: @GetMapping("/randomNumbers") public Flux<Double> getReactiveRandomNumbers() { return generateRandomNumbers(10, 500); } /** * Non-blocking randon number generator * @param amount - # of numbers to generate * @param delay - delay between each number generation in milliseconds * @return */ public Flux<Double> generateRandomNumbers(int amount, int delay){ return Flux.range(1, amount) .delayMillis(delay) .map(i -> Math.random()); } 2) Traditional Spring MVC

How to check if Mono is empty?

只谈情不闲聊 提交于 2019-12-02 22:54:39
I'm developing a app with Spring Boot 2.0 and Kotlin using the WebFlux framework. I want to check if a user id exits before save a transaction. I'm stucked in a simple thing like validate if a Mono is empty. fun createTransaction(serverRequest: ServerRequest) : Mono<ServerResponse> { val transaction = serverRequest.body(BodyExtractors.toMono(Transaction::class.java)) transaction.flatMap { val user = userRepository.findById(it.userId) // If it's empty, return badRequest() } return transaction.flatMap { transactionRepository.save(it).then(created(URI.create("/transaction/" + it.id)).build()) } }

Benefits of having HTTP endpoints return Flux/Mono instances instead of DTOs [closed]

余生颓废 提交于 2019-12-02 17:33:15
I've watched Spring Tips: Functional Reactive Endpoints with Spring Framework 5.0 and read a little about spring reactor but I can't quite understand it. What are the benefits of having endpoints return Flux / Mono instances (jacksonified) instead of straight up dto objects (jacksonified), given that I've got netty and spring reactor active? I initially assumed that reactive streams would, in http request/response context, work more like websockets wherein the server pushes the data to the receiver with an open channel but this doesn't seem to be the case. Also what does netty actually do

Comparison of Java reactive frameworks [closed]

瘦欲@ 提交于 2019-12-02 17:07:56
I see many frameworks/libraries that claim that they can help build reactive applications in Java, such as: Akka, Vert.x, RxJava, Reactor, QBit, etc. They seem to have different approaches, features, pros, cons, etc. I could not find detailled comparisons. There is documentation about each of these framerwork, but it is not enough for me to understand the differences. What are the differences between the major Java reactive frameworks? And what are the application requirements that can drive the choice of a Java reactive framework? Thank you for your time. I'm working on RxJava and I did some

Correct way of throwing exceptions with Reactor

社会主义新天地 提交于 2019-12-02 16:47:05
I'm new to project Reactor and reactive programming in general. I'm currently working on a piece of code similar to this: Mono.just(userId) .map(repo::findById) .map(user-> { if(user == null){ throw new UserNotFoundException(); } return user; }) // ... other mappings This example is probably silly and there are surely better ways of implementing this case, but the point is: Is it wrong to use a throw new exception in a map block or should I replace this with a return Mono.error(new UserNotFoundException()) ? Is there any actual difference in these two ways of doing? Oleh Dokuka There are a

Akka or Reactor [closed]

拟墨画扇 提交于 2019-12-02 13:48:57
I am in the process of starting a new project (java-based). I need to build it as a modular, distributed and resilient architecture. Therefore I would like to have the business processes to communicate among themselves, be interoperable, but also independent. I am looking right now at two frameworks that, besides their difference in age, express 2 different views: Akka ( http://akka.io ) Reactor ( https://github.com/reactor/reactor ) What I should consider when choosing one of the above frameworks? As far as I understand till now, Akka is still somehow coupled (in a way that I have to 'choose'

Failed to subscribe thenMany item

这一生的挚爱 提交于 2019-12-02 09:37:54
问题 When I use Mono.thenMany , the Flux data is lost, why? @Test fun thenManyLostFluxDataTest() { Mono.empty<Int>() .thenMany<Int> { Flux.fromIterable(listOf(1, 2)) } .subscribe { println(it) } // why not output item 1, 2 } If I change to use blockLast() to do the subscribe, the test method run forever. So fearful: @Test fun thenManyRunForeverTest() { Mono.empty<Int>() .thenMany<Int> { Flux.fromIterable(listOf(1, 2)) } .blockLast() // why run forever } Now I use another way to do what the

Issue with Project Reactor's or() operator usage

强颜欢笑 提交于 2019-12-02 04:55:22
I would like to chain Mono s and emit the first non-empty of them. I thought the or() operator was designed for this purpose. Here is my chain of Mono s: first one is empty and second one should emit "hello". @Test void orTest() { Mono<String> chain = Mono.<String>empty().or(Mono.just("hello")); StepVerifier.create( chain ) .expectNext("hello") .verifyComplete(); } However, I get the following failure: java.lang.AssertionError: expectation "expectNext(hello)" failed (expected: onNext(hello); actual: onComplete()) Can someone please help? What I am getting wrong here? You misunderstand or() -

Spring WebFlux: Only one connection receive subscriber allowed

拟墨画扇 提交于 2019-12-01 20:24:21
I am writing a simple app with Spring 5 Webflux and Kotlin. I am trying to implement PUT endpoint in a following way: PUT("/confs/{id}", { val id = it.pathVariable("id") ServerResponse.ok().body(service.save(it.bodyToMono(Item::class.java)), Item::class.java) }) The trick on save is that I try to read a city name from item, resolve geo coordinates, overwrite them in original item and then save to Mongo using Spring Data Mongo Reactive repo. fun save(item: Mono<Item>): Mono<Item> { val geo = item.flatMap { val city = it.location?.city ?: "Somewhere" geoService.resolveGeoFromCity(city) } val