- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个 spring web 应用程序。当用户调用保存端点时,系统应执行许多外部调用以将状态保存在多个微服务中。但是,这些步骤相互依赖。换句话说,我要执行一系列步骤。 sequence pattern
只是一个接一个地调用一组步骤没什么大不了的,我可以为每个步骤创建类并一个接一个地调用它们,在步骤之间进行适当的修改。
但是,每个步骤都可能失败,如果发生这种情况,应该正确地报告给用户。这是一个直接解决方案的伪代码:
var response = new Response()
try {
var result1 = step1.execute(args1)
var args2 = process(result1, args1)
var result2 = step2.execute(args2)
...
catch(Step1Exception e) {
response.setIsPartialSuccess(true);
response.setPartialResults(e.getDetails())
}
catch(Step2Exception e) {
response.setIsPartialSuccess(true);
response.setPartialResults(e.getDetails())
}
return response;
每个步骤都可以处理项目列表。有些步骤会一次发送所有项目(要么都失败,要么都没有),有些步骤会一个接一个地发送(一半可以失败,一半可以通过)。 StepException 将包含该信息,即什么通过了,什么失败了。
如您所见,它并不是真正可维护的。在这里使用 Spring Batch 就太过分了,因为我不是在读写东西,我不需要任何多线程、作业细节或检查点。但是,想法很相似,我想创建一些构建 block 并控制流程。
目前我正在尝试弄清楚 Spring Reactor 是否可以在这里提供帮助(是的,我知道它用于不同的目的),因为它具有带有一些错误处理的流/管道。想象一下我可以这样写:
var context = new Context(response, args1);
Mono.just(context)
.map(step1::execute)
.onErrorReturn(e -> context.withError(e))
//I assume if error happened before
//steps below are not executed
.map(step2::execute)
.onErrorReturn(e -> context.withError(e))
.block()
return context;
You can think of data processed by a reactive application as moving through an assembly line. Reactor is both the conveyor belt and the workstations. The raw material pours from a source (the original Publisher) and ends up as a finished product ready to be pushed to the consumer (or Subscriber).
The raw material can go through various transformations and other intermediary steps or be part of a larger assembly line that aggregates intermediate pieces together. If there is a glitch or clogging at one point (perhaps boxing the products takes a disproportionately long time), the afflicted workstation can signal upstream to limit the flow of raw material.
换句话说,我正在寻找一个与上述类似的框架。我现在不需要任何异步处理或重试,但它们将来可能会有用。如果有比 reactor 更好的东西满足我的需要,请告诉我。
最佳答案
即使您现在不需要非阻塞异步调用,Reactor 仍然非常适合这种情况,因为它擅长编排这种处理管道。我认为 Java 8 Stream
也可以满足要求,但在这方面功能稍差。
为了清晰起见,扩展方法引用,加上我的一些猜测,您的代码在 Reactor 中看起来像这样:
var response = Mono.just(initialArgs)
.flatMap(args1 -> Mono.fromCallable(() -> step1.execute(args1))
.map(result1 -> process(result1, args1) //args1 still in scope inside flatMap
)
.flatMap(args2 -> Mono.fromCallable(() -> step2.execute(args2))
//alternatively to last flatMap, with caveat:
//.map(args2 -> step2.execute(args2))
.map(endResult -> new Response(endResult))
.onErrorResume(error -> {
Response errorResponse = new Response();
errorResponse.setIsPartialSuccess(true);
errorResponse.setPartialResults(error.getDetails());
return Mono.just(errorResponse);
})
.block();
此特定链中使用的运算符不会更改线程,因此这将全部在调用最后一个 block()
方法的线程中执行。
任何步骤的错误都会停止整个处理并传播到最后(block()
将抛出异常)。
请注意,一些运算符(主要是那些具有时间概念的运算符)会更改线程,此时 stepX.execute
被阻塞会成为一个问题,因为这会阻塞本应共享的线程由整个 Reactor 代码(不仅是特定的处理管道)组成,而且资源有限。
关于Java 应用程序 : Sequence workflow pattern,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52846654/
这个问题在这里已经有了答案: Difference between / and /* in servlet mapping url pattern (5 个回答) 4年前关闭。 web.xml 中的/
Scala 具有支持模式匹配中析取的语言功能(“模式替代”): x match { case _: String | _: Int => case _ => } 但是,如果审查满足 P
解释我的问题: 类别:玩具 特质 1:说话像男性 特质2:说话像女性 我能否在运行时更改 Toy 的行为(特征),以便有时同一个对象说话像男性,有时同一个对象说话像女性? 我想在运行时改变说话行为。
我已经能够找到很好的资源,这些资源告诉我 Java API 中的 MouseAdapter 没有使用适配器模式。问题是:MouseAdapter 是否实现了某种模式? 我知道它的作用:它为 Mouse
我有兴趣了解有关模式识别的更多信息。我知道这是一个广泛的领域,所以我将列出一些我想学习处理的特定类型的问题: 在看似随机的字节集中查找模式。 识别图像中的已知形状(例如圆形和正方形)。 注意给定位置流
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 8 年前。 Improve this ques
所以,问题很简单:在 awk 中,if (var ~/pattern/) 是否与 if (var ~ "pattern") 相同? 我已经对 csv 进行了一些基本测试,两者似乎都产生了相同的结果..
我的问题是 this 的 Scala (Java) 变体Python 上的查询。 特别是,我有一个字符串 val myStr = "Shall we meet at, let's say, 8:45
我最近一直在研究正则表达式并注意到了这一点。 Pattern pNoEmbed = Pattern.compile("[ a-z]+", Pattern.CASE_INSENSITIVE); Patt
在研究大型应用程序的 C++ 源代码时,我发现了这种模式(该示例的语法可能很粗略,但基本细节都在那里): class A : X friend B; B *parent; ...
有人可以举一个“中介者模式”在现实世界中有用的用例吗? 最佳答案 Mediator是一种添加第三方对象以控制一组(2 个或更多)对象之间交互的方法。 您能找到的最简单的示例是 Chat Room例如,
尝试编译以下代码片段时: type 'a frame = Empty | Frame of string * 'a * 'a frame let rec searchFrame f s = match
目标 我的目标是获得一个 servlet 过滤器来处理对主页的请求,然后再将它们转发到 index.jsp。 问题 我无法让过滤器接收来自“/”的请求。它的 URL 模式是 / 相反,对该模式的请求最
这个问题已经有答案了: Difference between / and /* in servlet mapping url pattern (5 个回答) 已关闭 6 年前。 我已经设置了一个具有此
第 6 章(代码重用模式)中有以下示例: // the parent constructor function Parent(name) { this.name = name || 'Adam
Pattern类中的pattern()方法和toString()方法有什么区别? 文档说: public String pattern() Returns the regular expression
我有脚本 here并且 ng-pattern 工作正常,因为 scope.subnet 仅在输入匹配模式后才显示在输出中。但是如果 ng-pattern 不匹配,ng-show 不会显示任何错误
我想知道为什么当提供相同的正则表达式和相同的字符串时,java regex pattern.matcher() 和 pattern.matches() 的结果会不同 String str = "hel
This SO answer引用“患有模式综合症的小男孩”。虽然我可以通过上下文推断出一些含义,但我并不完全理解。 “有模式综合症的小男孩”的良好定义是什么? 最佳答案 它只是意味着寻找将模式注入(i
我有以下微服务架构的用例。 我的问题是,在当前情况下,我有 3 个微服务和一个 APIGateway。 最后,网关必须在聚合(组合)来自 3 个服务的数据之前进行大量查询。因为这 3 个微服务只提供基
我是一名优秀的程序员,十分优秀!