[英]Spring Flux and the Async annotation
我有一个Spring Flux应用程序,在某个时候我需要在后台执行一些繁重的任务,调用者(一个HTTP请求)不需要等待该任务完成。
没有反应堆,我可能只是使用Async注释,在另一个线程上执行该方法。 对于Reactor,我不确定是否应该继续使用该方法,或者不确定是否已有内置机制可以实现这一目标。
例如,给定一个接受Resource对象的Controller :
@PostMapping("/create")
public Mono<Resource> create(@Valid @RequestBody Resource r) {
processor.run(r); // the caller should not wait for the resource to be processed
return repository.save(r);
}
和一个处理器类:
@Async
void run(Resource r) {
WebClient webClient = WebClient.create("http://localhost:8080");
Mono<String> result = webClient.get()
.retrieve()
.bodyToMono(String.class);
String response = result.block(); //block for now
}
/create
的HTTP调用程序无需等待run
方法完成即可。
如果您正在寻找即弃模式的实现,则可以订阅发布者
@PostMapping("/create")
public Mono<Resource> create(@Valid @RequestBody Resource r) {
run(r).subscribe();
return repository.save(r);
}
Mono<Void> run(Resource r) {
WebClient webClient = WebClient.create("http://localhost:8080");
return webClient.get()
.retrieve()
.bodyToMono(String.class)
.then();
}
如果您的发布者执行阻止操作,则应使用弹性或并行调度程序在其他线程上进行订阅。
我进行了一些测试,我认为即使将subscribe()
用作触发方式,也忘记了要等待请求完成,然后再将答案返回给Webbrowser或REST客户端(至少在我的简单测试中,它看起来像这样)。 因此,您必须执行类似@Async的操作,并创建另一个线程:
@PostMapping("/create")
public Mono<Resource> create(@Valid @RequestBody Resource r) {
return processor.run(r)
.subscribeOn(Schedulers.elastic()) // put eveything above this line on another thread
.doOnNext(string -> repository.save(r)); // persist "r", not changing it, though
}
和一个处理器类:
Mono<String> run(Resource r) {
WebClient webClient = WebClient.create("http://localhost:8080");
return webClient.get()
.retrieve()
.bodyToMono(String.class);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.