Java 返回 Mono<Void> 后的方法调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51515306/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Method call after returning Mono<Void>
提问by kostepanych
I want to call the method when previous returned Mono<Void>
:
我想在以前返回时调用该方法Mono<Void>
:
@Override
public Mono<Void> sendEmail(EmailDto emailDto) {
return mailReactiveClient.sendEmail(message ->
createMessage(emailDto, emailDto.getBody(), message))
.doOnNext(saveNotificationLog(emailDto)); //it's not work
}
private void saveNotificationLog(EmailDto emailDto) {
notificationLogReactiveRepository.save(NotificationLog.builder()
...
.build());
}
Method sendEmail
returns Mono<Void>
.
方法sendEmail
返回Mono<Void>
。
So how to call saveNotificationLog
?
那么如何调用saveNotificationLog
呢?
UPD:Tring to make my question simplier:
UPD:试图让我的问题更简单:
@Override
public Mono<Void> sendEmail(EmailDto emailDto) {
return mailReactiveClient.sendEmail(message ->
createMessage(emailDto, emailDto.getBody(), message))
.doOnNext(System.out.print("Hello world!");
}
How to call doOnNext
or similar method after sendEmail
return Mono<Void>
?
返回doOnNext
后如何调用或类似方法?sendEmail
Mono<Void>
采纳答案by Brian Clozel
doOnNext
, and in general all doOn*
reactor methods are side-effect methods. You're not supposed to call them to do I/O work or chain operations, but rather log things and not do anything that would affect the state of the application.
doOnNext
,并且通常所有doOn*
反应器方法都是副作用方法。您不应该调用它们来执行 I/O 工作或链式操作,而是应该记录事情而不是做任何会影响应用程序状态的事情。
In your code sample, notificationLogReactiveRepository.save
returns Mono<Void>
. The saveNotificationLog
returns void
and does not subscribe to the publisher returned by notificationLogReactiveRepository.save
. This means the notification will not be saved, because nothing happens until you subscribe.
在您的代码示例中,notificationLogReactiveRepository.save
返回Mono<Void>
. 该saveNotificationLog
收益void
和不订阅由返回的出版商notificationLogReactiveRepository.save
。这意味着不会保存通知,因为在您订阅之前不会发生任何事情。
In this case, it seems you're trying to chain operations - then
operators are just made for that. Your code should look like this:
在这种情况下,您似乎正在尝试链接操作 -then
运算符就是为此而生的。您的代码应如下所示:
@Override
public Mono<Void> sendEmail(EmailDto emailDto) {
return mailReactiveClient.sendEmail(message ->
createMessage(emailDto, emailDto.getBody(), message))
.then(saveNotificationLog(emailDto));
}
private Mono<Void> saveNotificationLog(EmailDto emailDto) {
return notificationLogReactiveRepository.save(NotificationLog.builder()
...
.build());
}
回答by wargre
The Mono will not emit data, so doOnNext
will not be triggered. You should use the doOnSuccess
instead.
Mono 不会发出数据,因此doOnNext
不会被触发。你应该使用doOnSuccess
代替。
Also, your Mono need to be consumed. Without the code, we don't know if it is or not.
此外,您的 Mono 需要消耗。没有代码,我们不知道它是不是。
Some example here: I added subscribe()
to consume the mono. Depending on the use of your Mono, you will have to do or not the same thing.
这里有一些例子:我添加subscribe()
了消耗单声道。根据 Mono 的使用情况,您将不得不做或不做同样的事情。
This print nothing:
这不打印:
Mono<String> m=Mono.just("test");
Mono<Void> v=m.then();
v.doOnNext(x->System.out.println("OK")).subscribe();
This print "OK":
这个打印“OK”:
Mono<String> m=Mono.just("test");
Mono<Void> v=m.then();
v.doOnSuccess(x->System.out.println("OK")).subscribe();
回答by Michel Dambros Figueiredo
Try it this way:
试试这个方法:
Mono.empty().then()