mono - Mono.switchIfEmpty with Mono.then evaluation order
问题描述
I build the reactive pipeline like this on netty server with spring web-flux:
someService.getSettings(key) //Mono<Settings>
.filter(Settings::isEnabled)
.switchIfEmpty(Mono.error(
new Exception("Setting is disabled.")
)).then(/*Mono representing API fetch*/ someApi.fetch(param));
As per my understanding, whenever the Settings::isEnabled
would return false, the pipeline would be empty and I will get a Mono representing error. However, this behavior does not happens. My someApi.fetch(param)
call always fires without the filter Mono being completed. So even if Settings::isEnabled
is false or true, someApi.fetch(param)
is always fired without completion of Mono.filter
.
How do I make sure that error is thrown if filter returns empty Mono and the fetch call happens only when I have checked that the setting is not disabled?
解决方案
这仍然是 java:这个调用someApi.fetch(param)
不是在 lambda 内部完成的,所以它不能偷懒。执行到达该then
行的那一刻,您的fetch
方法被调用。运营商是量身定制的Flux.defer
,可以包装该呼叫并使其变得懒惰。
推荐阅读
- reactjs - redux/react-testing-library 的初始状态对于下一个测试用例保持不变
- mongodb - 如何保持 Azure blob 存储和 Mongo db 之间的一致性
- sql-server-2008 - 如何在 SSMS 中连接到 Azure SQL DB
- javascript - 仅匹配正则表达式的一部分
- c - C 代码适用于本地设备,但不适用于 leetcode
- python - 将字符串附加到列表/字符串返回'None'或'AttributeError:'str'对象在python中没有属性'append''
- php - PHP:将带有单独行的数组作为值插入数据库
- c# - .NET Core 3.1 Windows 服务的 EventLog 源名称
- java - 按员工编号对字符串数组进行排序
- flask - Flask- Session (server-side cache) - CSRF attacks