[英]Spring cloud stream merge responses from two different functions
I'm trying to use spring cloud stream to solve the following problem:我正在尝试使用 spring 云 stream 来解决以下问题:
I have a class that calls two separated functions (Function A and B), both of those functions must work in parallel if the Function A finishes it must call the Function C, the same happens if Function B finish but this will call Function D, then I need to wait for Function C and Function D to finish and merge both responses in a single response, and then return this merged response object to the starting class that must be waiting to get that response.我有一个 class 调用两个单独的函数(函数 A 和 B),如果 886982359588 A 完成,这两个函数必须并行工作,它必须调用 Function C,如果 Function B 完成,也会发生同样的情况,但这将调用 888698882359然后我需要等待 Function C 和 Function D 完成并将两个响应合并为一个响应,然后将此合并响应 object 返回到必须等待获得该响应的起始 class。
The problems I have are:我遇到的问题是:
streamBridge.send
which to start Function A and Function B at the same time.streamBridge.send
同时启动 Function A 和 Function B。 I'm using我正在使用
spring-cloud-stream-3.1.3
spring-cloud-stream-binder-rabbit
Required spring-cloud-stream-binder-rabbit
必需I cannot use Kafka
Required我不能使用
Kafka
必需
Sample code示例代码
ServiceClass服务等级
@Service
@RequiredArgsConstructor
public class ServiceClass {
@NonNull
private final StreamBridge streamBridge;
@Override
protected MergedResponse execute(Input input) {
var send1 = streamBridge.send("functionA-in-0", input);
var send2 = streamBridge.send("functionB-in-0", input);
//TODO: Wait for Function E response object
}
}
Function A Function 一
@Slf4j
@Configuration
public class FunctionAClass{
@Bean
public Function<Input, OutputFunctionA> functionA() {
return input -> {
//TODO: Invoke Function C to pass OutputFunctionA object
return OutputFunctionA.builder.build();
};
}
}
I don't mind using Supplier
or Consumer
instead of Function
.我不介意使用
Supplier
或Consumer
而不是Function
。
Edit Hi, @Oleg Zhurakousky thanks for your help, to answer your question my problem is: I have to create a REST endpoint that consume N
different third-party REST endpoints (two at first, async is a must as it will be too slow to process every request sequentially) I don't need all the data from them, just a few fields to build a common object. I'm planning to used streamBridge
to start the first two functions that will to build the required request for each third-party, then a function to invoke each of their endpoints, then a function to build a common object with each response, and finally a function that collects the commons objects and send them in the response of my service.编辑你好,@Oleg Zhurakousky 感谢你的帮助,回答你的问题我的问题是:我必须创建一个 REST 端点,它使用
N
个不同的第三方 REST 端点(首先是两个,异步是必须的,因为它太慢了顺序处理每个请求)我不需要他们的所有数据,只需要几个字段来构建一个通用的 object。我打算使用streamBridge
启动前两个函数,这些函数将为每个三分之一构建所需的请求-party,然后是一个 function 来调用他们的每个端点,然后是一个 function 来为每个响应构建一个公共的 object,最后是一个 function 来收集公共对象并将它们发送到我的服务的响应中。 Let me know if you have another question.如果您还有其他问题,请告诉我。
Regards.问候。
Couple of points.几点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.