Java 为什么Mono.fromRunnable不执行

Java 为什么Mono.fromRunnable不执行,java,spring,spring-webflux,project-reactor,Java,Spring,Spring Webflux,Project Reactor,我是反应堆堆芯库的新手,我在网上看到了一些例子,读了几篇文章 但是,我很难使用Mono.fromRunnable()运行异步函数 这种没有Mono的方法按预期工作(向MS团队发布消息) void通知(卡片,URI){ HttpHeaders=新的HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); postForEntity(uri,新的HttpEntity(卡片,标题),Void.class); } 当我尝试使用

我是反应堆堆芯库的新手,我在网上看到了一些例子,读了几篇文章

但是,我很难使用Mono.fromRunnable()运行异步函数

这种没有Mono的方法按预期工作(向MS团队发布消息)

void通知(卡片,URI){
HttpHeaders=新的HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
postForEntity(uri,新的HttpEntity(卡片,标题),Void.class);
}
当我尝试使用Mono.fromRunnable时,什么都没有发生:

@Override
public Mono<Void> notify(Notification notification, Facts facts) {
    log.debug("Notify users by MS-Teams");
    Card card = cardBuilder.build(notification, facts);
    try {
        URI url = URI.create(uri);
        return doNotify(card, url);
    } catch (IllegalArgumentException ignore) {
        return Mono.empty();
    }
}

Mono<Void> doNotify(Card card, URI uri) {
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    return Mono.fromRunnable(() -> restTemplate.postForEntity(uri, new HttpEntity<Object>(card, headers), Void.class))
        .doOnError(e -> log.error("Failed to notify for card {}", card, e)).then().then(Mono.fromRunnable(this::updateSuccess));
}

private void updateSuccess() {
    System.out.println("Success");
}
@覆盖
公共通知(通知通知、事实){
log.debug(“由MS团队通知用户”);
Card Card=cardBuilder.build(通知、事实);
试一试{
uriurl=URI.create(URI);
返回doNotify(卡片、url);
}捕获(IllegalArgumentException忽略){
返回Mono.empty();
}
}
Mono doNotify(卡片,URI){
HttpHeaders=新的HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
返回Mono.fromRunnable(()->restTemplate.postForEntity(uri,新的HttpEntity(卡片,标题),Void.class))
.doError(e->log.error(“无法通知卡{}”,卡,e)).then().then(Mono.fromRunnable(this::updateSuccess));
}
私有void updateSuccess(){
System.out.println(“成功”);
}

请协助。

构建
Mono
应该表示“什么也没发生”。只有当有人试图使用mono产生的值时,代码才会被执行。那是故意的。谁调用了
notify
,他们如何使用返回值?@JoachimSauer编辑的返回单最终被忽略,从技术上讲,没有任何东西会消耗它。这就是有人应该订阅的问题。订阅之前什么都不会发生。mono的标签不适用于Java。这正是我在本视频中讨论的情况:D
@Override
public Mono<Void> notify(Notification notification, Facts facts) {
    log.debug("Notify users by MS-Teams");
    Card card = cardBuilder.build(notification, facts);
    try {
        URI url = URI.create(uri);
        return doNotify(card, url);
    } catch (IllegalArgumentException ignore) {
        return Mono.empty();
    }
}

Mono<Void> doNotify(Card card, URI uri) {
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    return Mono.fromRunnable(() -> restTemplate.postForEntity(uri, new HttpEntity<Object>(card, headers), Void.class))
        .doOnError(e -> log.error("Failed to notify for card {}", card, e)).then().then(Mono.fromRunnable(this::updateSuccess));
}

private void updateSuccess() {
    System.out.println("Success");
}