project-reactor

How to perform assertion on all remaining elements in StepVerifier?

我是研究僧i 提交于 2019-12-07 21:40:12
问题 StepVerifier has an assertNext method that allows performing an assertion on a value of next element. StepVerifier.create(dataFlux) .assertNext(v -> checkValue(v) .verifyComplete(); What is a good way to perform assertion for every remaining element (e.g. to check that every element is positive)? I'd expect something like assertEveryNextElement method. 回答1: My first attempt to perform assertion on all elements of a flux was StepVerifier.create(dataFlux) .recordWith(ArrayList::new)

How to set and handle timeout in Spring WebClient?

故事扮演 提交于 2019-12-07 17:10:28
Spring docs says it is required to configure http client for WebClient manually to set timeouts: https://docs.spring.io/spring/docs/current/spring-framework-reference/web-reactive.html#webflux-client-builder-reactor-timeout . But since WebClient returns reactive Mono, it's possible (api-wise) to apply .timeout method. Does it have the same effect? Moreover, when one uses .timeout method, Reactor's TimeoutException is expected. Will the same error appear in the stream if configuration is done manually i.e. will doOnError(TimeoutException.class, ...) work? My findings Setting a timeout in a http

Java application: Sequence workflow pattern

左心房为你撑大大i 提交于 2019-12-07 15:00:37
问题 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

How to convert Reactor Flux<String> to InputStream

梦想与她 提交于 2019-12-07 07:38:11
问题 Given that I have a Flux<String> of unknown size, how can I convert it into InputStream that other library is expecting? For example with WebClient I can achieve that using this approach WebClient.get('example.com').exchange.flatMap { it.bodyToMono(InputStreamResource::class.java) }.map { it.inputStream } but I can't figure out how to do the same when I have Flux<String> as an input? 回答1: There are probably many ways to do this. One possibility is to use PipedInputStream and PipedOutputStream

How to return 404 with Spring WebFlux

≯℡__Kan透↙ 提交于 2019-12-07 06:36:39
问题 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

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

别来无恙 提交于 2019-12-07 04:49:51
问题 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 =

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

∥☆過路亽.° 提交于 2019-12-06 13:27:47
问题 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/

How to perform assertion on all remaining elements in StepVerifier?

时光怂恿深爱的人放手 提交于 2019-12-06 11:01:21
StepVerifier has an assertNext method that allows performing an assertion on a value of next element. StepVerifier.create(dataFlux) .assertNext(v -> checkValue(v) .verifyComplete(); What is a good way to perform assertion for every remaining element (e.g. to check that every element is positive)? I'd expect something like assertEveryNextElement method. My first attempt to perform assertion on all elements of a flux was StepVerifier.create(dataFlux) .recordWith(ArrayList::new) .thenConsumeWhile(x -> true) // Predicate on elements .consumeRecordedWith(matches -> matches.forEach(v -> checkValue(v

Reactor Flux<MyObject> to Mono<List<MyObject>>

荒凉一梦 提交于 2019-12-06 05:25:51
How can I convert Flux<MyObject> directly to Mono<List<MyObject>> ? I am looking for equivalent of Single<List<MyObject>> single = observable.toList() from RxJava. With blocking operator I can do it like this: val just: Mono<List<MyObject>> = Mono.just(flux.toIterable().toList()) But it is executed at the time of declaration which doesn't seam to be right. Flux has the method collectList() doing just the same like toList() in Rx. val just: Mono<List<MyObject>> = flux.collectList() 来源: https://stackoverflow.com/questions/44039497/reactor-fluxmyobject-to-monolistmyobject

How to correctly use slf4j MDC in spring-webflux WebFilter

孤人 提交于 2019-12-06 03:32:23
问题 I referenced with the blog post Contextual Logging with Reactor Context and MDC but I don't know how to access reactor context in WebFilter. @Component public class RequestIdFilter implements WebFilter { @Override public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) { List<String> myHeader = exchange.getRequest().getHeaders().get("X-My-Header"); if (myHeader != null && !myHeader.isEmpty()) { MDC.put("myHeader", myHeader.get(0)); } return chain.filter(exchange); } } 回答1: