vertx-sync icon indicating copy to clipboard operation
vertx-sync copied to clipboard

Method would Invoke twice If Miss One Annatation @Suspendable in the Invoke Chain

Open mashirofang opened this issue 5 years ago • 0 comments

hello ! I had some problem while using this fantastic SYNC library in my program. The interface is very simple , but in my program I had some very deep invoke of methods just like code below . We got a fiberHandler and inside call test(), then inside call test2(), then inside call test3() and then inside call test4() .


import co.paralleluniverse.fibers.Suspendable;
import io.vertx.core.Vertx;
import io.vertx.core.eventbus.EventBus;
import io.vertx.core.eventbus.Message;
import io.vertx.ext.sync.SyncVerticle;

import static io.vertx.ext.sync.Sync.awaitResult;
import static io.vertx.ext.sync.Sync.fiberHandler;

public class FiberTest extends SyncVerticle {

  FiberTest(Vertx vertx) {
    this.vertx = vertx;
  }

  public static final String ADDRESS = "some-address";

  public static void main(String[] args) {
    Vertx vertx = Vertx.vertx();
    vertx.deployVerticle(new FiberTest(vertx));
  }

  @Suspendable
  public String test() {
    System.out.println("method:test1");
    return test2();
  }

  public String test2() {
    System.out.println("method:test2");
    return test3();
  }

  @Suspendable
  public String test3() {
    System.out.println("method:test3");
    Message<String> reply = awaitResult(h -> vertx.eventBus().send(ADDRESS, "blah", h));
    return reply.body();
  }

  @Suspendable
  @Override
  public void start() throws Exception {
    EventBus eb = vertx.eventBus();
    eb.consumer(FiberTest.ADDRESS).handler(event -> event.reply("hello"));
    vertx
        .createHttpServer()
        .requestHandler(
            fiberHandler(
                req -> {
                  String test = test();
                  System.out.println(test);
                  req.response().end("blah");
                }))
        .listen(8080, "localhost");
  }
}

And I found that I must Annatation All the method of the call chain . If I miss the @Suspendable on test2() , some method would invoke twice like this .

method:test1
method:test2
method:test3
method:test1
method:test2
hello

Do I had a way that just Annatation the last method which indeed would Suspend?

Thank!

mashirofang avatar Nov 05 '19 09:11 mashirofang