Java 编写此webflux代码最有效的方法是什么?

Java 编写此webflux代码最有效的方法是什么?,java,spring-webflux,Java,Spring Webflux,我有这个密码。在方法A中,我必须调用b.subcribe()。没有它,方法B将无法执行。什么是不使用subscibe的有效方法来调用它。我觉得subcribe在这里不合适 public static void main(String[] args) { Test t = new Test(); Mono<Integer> a = t.A(); System.out.println(System.currentTimeMillis()/

我有这个密码。在方法A中,我必须调用b.subcribe()。没有它,方法B将无法执行。什么是不使用subscibe的有效方法来调用它。我觉得subcribe在这里不合适

public static void main(String[] args) {
        Test t = new Test();
        Mono<Integer> a = t.A();

        System.out.println(System.currentTimeMillis()/1000);
        a.subscribe(a1-> System.out.println(a1));
        System.out.println(System.currentTimeMillis()/1000);

    }

     Mono<Integer> A(){
         Mono<Integer> integerMono = Mono.fromCallable(() -> {

             System.out.println("Hello");
             Thread.sleep(2000);
             return 1;
         });


         Mono<Integer> b = integerMono.flatMap(this::B);

         b.subscribe();

         return integerMono;
     }

    Mono<Integer> B(int a ){
        return Mono.fromCallable(() -> {
            System.out.println("B method");
            Thread.sleep(1000);
            return a * 100;
        });

    }
publicstaticvoidmain(字符串[]args){
测试t=新测试();
单声道a=t.a();
System.out.println(System.currentTimeMillis()/1000);
a、 订阅(a1->System.out.println(a1));
System.out.println(System.currentTimeMillis()/1000);
}
单声道A(){
Mono integerMono=Mono.fromCallable(()->{
System.out.println(“你好”);
《睡眠》(2000年);
返回1;
});
monob=integerMono.flatMap(this::b);
b、 订阅();
返回整数mono;
}
单声道B(INTA){
返回Mono.fromCallable(()->{
System.out.println(“B方法”);
睡眠(1000);
返回a*100;
});
}
我找到了另一种避免订阅的方法。我不确定这是最好的方式

Mono<Integer> A(){
         Mono<Integer> integerMono = Mono.fromCallable(() -> {

             System.out.println("Hello");
             Thread.sleep(2000);
             return 1;
         });

         
         return integerMono.flatMap(
                 a -> {
                     return this.B(a).map(x->a);
                 }
         );
     }
monoa(){
Mono integerMono=Mono.fromCallable(()->{
System.out.println(“你好”);
《睡眠》(2000年);
返回1;
});
返回integerMono.flatMap(
a->{
返回此.B(a).map(x->a);
}
);
}

你的代码什么都不做,所以当你说“尽可能高效”时,你不可能回答

除非您是最终消费者,否则您不应订阅。你睡得到处都是,这意味着你正在阻止线程,这也不应该在webflux中进行

所以如果你想要一个实际的解决方案,你应该给我们一个实际的问题

public static void main(String[] args) throws InterruptedException {
    Test t = new Test();
    Mono<Integer> a = t.A();

    System.out.println(System.currentTimeMillis()/1000);
    a.subscribe(a1-> System.out.println(a1));
    System.out.println(System.currentTimeMillis()/1000);
}

public static class Test {

     Mono<Integer> A(){
         Mono<Integer> integerMono = Mono.fromCallable(() -> {

             System.out.println("Hello");
             Thread.sleep(2000);
             return 1;
         });

         // Here just return
         return integerMono.flatMap(this::B);
     }

    Mono<Integer> B(int a ){
        return Mono.fromCallable(() -> {
            System.out.println("B method");
            Thread.sleep(1000);
            return a * 100;
        });
    }
}
publicstaticvoidmain(String[]args)抛出InterruptedException{
测试t=新测试();
单声道a=t.a();
System.out.println(System.currentTimeMillis()/1000);
a、 订阅(a1->System.out.println(a1));
System.out.println(System.currentTimeMillis()/1000);
}
公共静态类测试{
单声道A(){
Mono integerMono=Mono.fromCallable(()->{
System.out.println(“你好”);
《睡眠》(2000年);
返回1;
});
//来,回来吧
返回integerMono.flatMap(this::B);
}
单声道B(INTA){
返回Mono.fromCallable(()->{
System.out.println(“B方法”);
睡眠(1000);
返回a*100;
});
}
}

没有什么比这更有效的了。。。你实际上是在调用一个函数,而函数又反过来调用一个函数。

只需删除行
b。subscribe()
方法b将永远不会被调用,然后
b。然后(integerMono)
方法a将被调用两次,使用b.then(interMono)是非常糟糕的