project-reactor

Readable debug logging for http requests with spring webclient

对着背影说爱祢 提交于 2019-12-06 01:31:02
问题 I'm using Spring reactive WebClient for sending requests to a http server. Inorder to view the underlying request & response that's being sent, I enabled debug logging for reactor.ipc.netty package. The headers for the outgoing requests can be viewed normally. Tho I'm sending & receiving plain text over http, the log contains the request & responses in the below format (is it hex?) I'm not sure how to view the logged data in a easy to understand way. Better yet log the request & response in a

Java application: Sequence workflow pattern

情到浓时终转凉″ 提交于 2019-12-05 22:06:26
I have a spring web application. When a user calls save endpoint, system should execute many external calls to save the state in multiple microservices. However, those steps depend on each other. In other words I have a sequence of steps to perform. sequence pattern Just calling a set of steps one by one is not a big deal, I can just create class for each step and call them one by one doing appropriate modifications between the steps. However, each of the steps can fail and if it happens it should be properly reported to the user. Here is a pseudo-code for a straight forward solution: var

How to limit the number of active Spring WebClient calls

白昼怎懂夜的黑 提交于 2019-12-05 19:15:58
I have a requirement where I read a bunch of rows (thousands) from a SQL DB using Spring Batch and call a REST Service to enrich content before writing them on a Kafka topic. When using the Spring Reactive webClient, how do I limit the number of active non-blocking service calls? Should I somehow introduce a Flux in the loop after I read data using Spring Batch? (I understand the usage of delayElements and that it serves a different purpose, as when a single Get Service Call brings in lot of data and you want the server to slow down -- here though, my use case is a bit different in that I have

compose() vs. transform() vs. as() vs. map() in Flux and Mono

折月煮酒 提交于 2019-12-05 17:55:35
问题 Recently, I decided to try spring 5 with projectreactor.io (io.projectreactor:3.1.1). Does anyone know what the best case of using this functions? What cons and pros of using each of them and where they should be used? Good examples will be helpful. 回答1: You have two broadly different categories of operators here: Operators that work on the Flux itself transform and compose are for code mutualization When you compose chains of operators regularly and you have common operator usage patterns in

how to wait for all requests to complete with Spring 5 WebClient?

泄露秘密 提交于 2019-12-05 17:23:21
I have a simple Java program that sends multiple requests with Spring WebClient. Each returns a mono, and I am using response.subscribe() to check the result. However, my main thread of execution finishes before all requests are processed, unless I add a long Thread.sleep(). With CompletableFutures you can use: CompletableFuture.allOf(futures).join(); Is there a way to wait for all Mono's to complete ? As explained in the Project Reactor documentation , nothing happens until you subscribe to a Publisher . That operation returns an instance of Disposable , meaning that operation may still be

Exception Handling Reactive

我只是一个虾纸丫 提交于 2019-12-05 14:15:06
How does one use a Mono.error(<Throwable>) but attach information from the body returned from a request? Is there a reactive object that extends Throwable that takes a Mono/Flux object, so the error being thrown will wait for the body to be accounted for? Or is there a way to add some sort of 'flag' onto an existing Mono object to make it fail instantly (to circumvent the requirement to be Throwable ) Example scenario below: import org.springframework.web.reactive.function.client.WebClient; private someMethod() { webClient.get().retrieve().onStatus(HttpStatus::isError, this::errorHandler) }

How to return 404 with Spring WebFlux

折月煮酒 提交于 2019-12-05 11:20:57
I'm having a controller like this one (in Kotlin): @RestController @RequestMapping("/") class CustomerController (private val service: CustomerService) { @GetMapping("/{id}") fun findById(@PathVariable id: String, @RequestHeader(value = IF_NONE_MATCH) versionHeader: String?): Mono<HttpEntity<KundeResource>> = return service.findById(id) .switchIfEmpty(Mono.error(NotFoundException())) .map { // ETag stuff ... ok().eTag("...").body(...) } } Im wondering whether there is a better approach than throwing an exception which is annotated with @ResponseStatus(code = NOT_FOUND) I would like use

Spring 5 Web Reactive - Hot Publishing - How to use EmitterProcessor to bridge a MessageListener to an event stream

ぃ、小莉子 提交于 2019-12-05 11:08:59
A sample project is located here: https://github.com/codependent/spring5-playground I would like to bridge a message received from a JMS queue into a Reactive Controller that would publish the messages as an event stream. I don't want the messages to be replayed, that is, if a message arrives and there isn't any subscriber I don't want them to be sent later when any subsbribes, so I am using an EmitterProcessor: @Component public class AlertEmitterProcessor { private Logger logger = LoggerFactory.getLogger(getClass()); private EmitterProcessor<Alert> processor; public AlertEmitterProcessor(){

Is it correct to convert a CompletableFuture<Stream<T>> to a Publisher<T>?

我怕爱的太早我们不能终老 提交于 2019-12-05 10:46:39
问题 To allow multiple iterations on the resulting stream from a CompletableFuture<Stream<String>> I am considering one of the following approaches: Convert the resulting future to CompletableFuture<List<String>> through: teams.thenApply(st -> st.collect(toList())) Convert the resulting future to Flux<String> with cache: Flux.fromStream(teams::join).cache(); Flux<T> is the implementation of Publisher<T> in project reactor. Use case: I would like to get a sequence with the premier league teams

How can I return a Flux<Order> when my response is wrapped in a json pagination object with Spring 5?

早过忘川 提交于 2019-12-04 18:44:54
I've got a web service that I'm trying to consume with the new Spring 5 WebClient. Working example # GET /orders/ [ { orderId: 1, ... }, { orderId: 1, ... } ] And the java code for the call // Java Flux<Order> ordersStream = webClient.get() .uri("/orders/") .exchange() .flatMap(response -> response.bodyToFlux(Order.class)); Problem The response from the web service is paginated and therefore doesn't contain the list of items directly as in the example above. It looks like this # GET /orders/ { "error": null, "metadata": { "total": 998, "limit": 1000, "offset": 0 }, "data": [ { orderId: 1, ...