我对 webflux 比较陌生,我想找到解决方案,以避免在有条件时出现嵌套的 flatMap:
我有 3 个简单的实体:
商品、品牌、类别。
Item基本包含:brandId和categoryId
public Mono<Item> patch(String itemId, PatchSpecs specs) {
return itemRepository.findById(itemId)
.switchIfEmpty(Mono.error(..)))
.flatMap(item -> {
final String brandId = specs.getBrandId();
if (brandId != null) {
return brandService.getById(brandId)
.switchIfEmpty(Mono.error(..)))
.flatMap(brand -> {
final String categoryId = specs.getCategoryId();
if (categoryId != null) {
return categoryService.getById(categoryId)... -> return updateAndSave(..)
}
return updateAndSave(specs, item, brand, null);
});
}
else {
final String categoryId = specs.getCategoryId();
if (categoryId != null) {
return categoryService.getById(categoryId)... -> return updateAndSave(..)
}
return …Run Code Online (Sandbox Code Playgroud) 我正在使用 Spring Webflux 并试图理解 Monos 链的超时概念。
例如,有一系列 Mono 调用:
myService.firstOperation()
.then(myService.secondOperation())
...
.then(myService.nOperation())
.timeout(3000L)
Run Code Online (Sandbox Code Playgroud)
如何应用超时:
1) 对于一般操作(操作总时间)
2)对于ech操作(每个操作的时间不应超过超时时间)
3) 仅针对最后一次操作(nOperation)
?
我几乎可以肯定超时适用于最后一个发布者。如果是这样,如何将超时应用于操作的总和?
java spring reactive-programming project-reactor spring-webflux
我想要一个类似于 RxSwift 的组合运算符take(_ n: Int),我写了这个,它似乎有效:
first(_ n)执行public extension Publisher where Failure == Never {
/// Publishes the first `n` elements of a stream, then finishes.
func first(_ numberOfElements: Int) -> AnyPublisher<Output, Failure> {
collect(numberOfElements) // "Buffer"
.first() // "release and complete (finish)"
// Publisher<[Output]> -> Publisher<Output>
.map { $0.publisher }.switchToLatest()
.eraseToAnyPublisher()
}
}
Run Code Online (Sandbox Code Playgroud)
或者你能想出更好的替代解决方案吗?
(take但我将其命名为first,以反映合并本机运算符first- “发布流的第一个元素,然后完成。” ) 运算符在编写测试时非常方便,我希望发布者仅在n元素之后完成。
我有 2 个事件流: 1. 鼠标拖放事件流(拖动开始...拖动结束...拖动开始...拖动结束) 2.按键事件流('a' ... ' b' .... 'c' .... 'd')
我需要合并到一个仅包含第二个流中的事件(因此仅包含按键)的流中,但它需要过滤掉拖动开始和拖动结束之间发生的所有按键(最后一个除外)。
所以如果来源是这样的:
... Start ............... End .............. Start .............. End
Run Code Online (Sandbox Code Playgroud)
和
...........'a'...'b'...'c'.......'d'...'e'..........'f'....'g'.......
Run Code Online (Sandbox Code Playgroud)
结果应该是这样的:
...........................'c'...'d'...'e'..........................'g'
Run Code Online (Sandbox Code Playgroud)
在 C# 中使用 Rx.net 可以实现类似的功能吗?
如果代码中发生某些情况,我想取消 kotlin 流程。
假设我有一个方法如下
fun test(): Flow<String> = flow {
val lst = listOf("A", "B", "C")
while(true) {
lst.forEach { emit(it) }
//If some condition occurs, need to return from here, else continue
//How to stop flow here
}
}
Run Code Online (Sandbox Code Playgroud)
并称其为
test().collect { println(it)}
Run Code Online (Sandbox Code Playgroud)
问题是,如何停止流在某些条件下(来自流生成器或流生成器之外)产生任何内容?
asynchronous reactive-programming kotlin rx-java kotlin-flow
Spring Boot应用:
a@RestController接收以下有效负载:
{
"cartoon": "The Little Mermaid",
"characterNames": ["Ariel", "Prince Eric", "Sebastian", "Flounder"]
}
Run Code Online (Sandbox Code Playgroud)
我需要按以下方式处理它:
转换控制器接收到的数据:将角色名称替换为上一步从“卡通人物”微服务接收到的适当 ID。
{
"cartoon": "The Little Mermaid",
"characterIds": [1, 2, 3, 4]
}
使用转换后的数据向“cartoon-db”微服务发送 HTTP POST 请求。
我遇到的问题:
Reactive Programming我需要使用(非阻塞\异步处理)和Spring WebFlux(Mono| Flux)的范例来实现所有这些步骤Spring Reactive WebClient- 但我对该堆栈的经验为零,试图尽可能多地阅读它,再加上谷歌搜索很多但是仍然有很多未解答的问题,例如:
Q1 . 我已经配置了响应式 webClient,向“卡通人物”微服务发送请求:
public Mono<Integer> getCartoonCharacterIdbyName(String characterName) {
return WebClient.builder().baseUrl("http://cartoon-characters").build()
.get()
.uri("/character/{characterName}", characterName)
.retrieve()
.bodyToMono(Integer.class);
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我有一个卡通人物名称列表,对于每个角色我需要调用getCartoonCharacterIdbyName(String name)方法,我不确定串联调用它的正确选项,相信正确的选项:并行执行。
写了如下方法: …
reactive-programming spring-boot reactive spring-webflux spring-webclient
与等待运行 Reactor Mono 实例完成的问题类似,但我想在另一个 Mono 中获得理想的结果。这是我的代码。我尝试了具体化解决方案,但没有成功。
@GetMapping("/bounced")
public Mono<Map<String, Object>> bounced(
@RequestHeader("X-B3-Traceid") String traceId,
@RequestHeader(HttpHeaders.AUTHORIZATION) String authorization
) {
final Mono<Map<String, Object>> sample = webClient.get()
.uri("http://sample:8080/")
.header(HttpHeaders.AUTHORIZATION, authorization)
.retrieve()
.bodyToMono(new ParameterizedTypeReference<>() {
});
final Mono<Map<String, Object>> httpGet = webClient.get()
.uri("http://httpbin.org/get")
.retrieve()
.bodyToMono(new ParameterizedTypeReference<>() {
});
final Mono<Map<String, Object>> anything = webClient.get()
.uri("http://httpbin.org/anything/foo")
.retrieve()
.bodyToMono(new ParameterizedTypeReference<>() {
});
/*
Tried this and it does start it up, but it triggers another "download" in the return block.
Mono.when(anything, sample, httpGet)
.subscribe(); …Run Code Online (Sandbox Code Playgroud) 我目前正在开发一个涉及一些反应式编程的项目。
我有 4 个不同的反应性存储库,分别从中得到 4 个不同的Mono<List<SomeType>>回报。目标是将它们组合成一个Mono<List<GeneralType>>,以便将其合并到自定义响应中以在ResponseEntity.ok(). 我已经创建了一个GeneralType并成功转换了一个Mono<List<SomeType>>,但是没有取得进一步的进展。
所有存储库都有相似的签名:
public Mono<List<SomeType>> findAllByUserId(UUID userId)
Run Code Online (Sandbox Code Playgroud)
我的回复中的字段将所有不同的列表合并为一个:
private Mono<List<GeneralType>> items;
Run Code Online (Sandbox Code Playgroud)
到目前为止我的方法是什么样的:
public Mono<List<GeneralType>> combineMonos(UUID userId) {
Mono<List<GeneralType>> combo1 = reactiveRepository.findAllByUserId(userId)
.map(list -> list.stream()
.map(GeneralType::new)
.collect(Collectors.toList()));
return combo1; // works just fine
}
Run Code Online (Sandbox Code Playgroud)
所有其他列表都有几乎相同的方法,但是将它们组合成一个 Mono<List> 是一个问题。
我尝试过以下方法:
return Flux.merge(combo1.flatMapMany(Flux::fromIterable), combo2.flatMapMany(Flux::fromIterable)).collectList();
Run Code Online (Sandbox Code Playgroud)
但这样一来,IDE 就会敦促将返回类型更改为Flux<Object>. 另外,有些列表可能为空,所以我不确定zip()这里是否有一个选项。我读过,如果至少有一个结果为空,它将返回所有内容为空。
所以问题是如何在不到处都是block()的情况下以有效的方式完成这一点?
假设我们有以下组件脚本
let x = 0;
function increase() {
x += 1;
}
$: console.log(x)
$: if (x == 4) {
x = 0;
}
Run Code Online (Sandbox Code Playgroud)
https://svelte.dev/repl/2bca979673114afea9fc6b37434653a3?version=3.29.0
直觉上,我期望持续调用increase, 是将值0,1,2,3,4,1,2,3,4,...记录到控制台。但这种情况并非如此。相反,第二个反应式语句首先运行,并且4从不记录;这是0。切换反应语句的顺序没有任何效果。
如何使日志记录语句在另一个语句之前执行?它首先运行第二个的原因是什么?
我在玩反应堆
public @NotNull Mono<ServerResponse> findXXXXSse(final ServerRequest request) {
return request.bodyToMono(XXXXSearch.class)
.doOnNext(this::validate)
.flatMap(this::findXXXXSse)
.switchIfEmpty(this.emptyBodyException());
}
Run Code Online (Sandbox Code Playgroud)
我想知道使用.doOnNext(this::validate)是否正确。从我的角度来看,我不确定在findXXXXSse?之前调用 validate 。
我错了吗?
java ×4
spring ×2
asynchronous ×1
combine ×1
ios ×1
kotlin ×1
kotlin-flow ×1
reactive ×1
rx-java ×1
rx.net ×1
spring-boot ×1
svelte ×1
swift ×1
timing ×1