2

我正在玩vertx.io,它看起来很棒。现在我建立了一个由三个verticles(三个简单的java main fat jars)组成的集群。vertx.io一个 verticle 暴露了一个 web 接口(一个糟糕的 api),另外两个只是通过的服务发现机制知道 web verticle 是向上还是向下。这是我的(相关部分)简单的“非网络”垂直:

public class FileReader extends AbstractVerticle {

  private ServiceDiscovery discovery;
  private Logger log = LogManager.getLogger(getClass());
  private Record record;

  @Override
  public void start(Future<Void> startFuture) throws Exception {
    record = EventBusService.createRecord(getServiceName(), getServiceAddress(), getClass());
    setUpRecord(record);
    discovery = ServiceDiscovery.create(vertx);
    discovery.publish(record, h -> {
        if (h.succeeded()) {
            log.info("Record published.");
        } else {
            log.info("Record not published.", h.cause());
        }
    });
    startFuture.complete();
  }
  ...
  @Override
  public void stop(Future<Void> stopFuture) throws Exception {
    log.info("Stopping verticle.");
    discovery.unpublish(record.getRegistration(), h -> {
        if (h.succeeded()) {
            log.info("Service unpublished.");
            stopFuture.complete();
        } else {
            log.error(h.cause());
            stopFuture.fail(h.cause());
        }
    });
  }
}

以下是我如何部署两个“非 Web”verticles 之一:

public class FileReaderApp {

private static Logger log = LogManager.getLogger(FileReaderApp.class);
private static String id;

  public static void main(String[] args) {
    ClusterManager cMgr = new HazelcastClusterManager();
    VertxOptions vOpt = new VertxOptions(new JsonObject());
    vOpt.setClusterManager(cMgr);
    Vertx.clusteredVertx(vOpt, ch -> {
        if (ch.succeeded()) {
            log.info("Deploying file reader.");
            Vertx vertx = ch.result();
            vertx.deployVerticle(new FileReader(), h -> {
                if (h.succeeded()) {
                    id = h.result();
                } else {
                    log.error(h.cause());
                }
            });
        } else {
            log.error(ch.cause());
        }
    });

    Runtime.getRuntime().addShutdownHook(new Thread() {
        public void run() {
            log.info("Undeploying " + id);
            Vertx.vertx().undeploy(id, h -> {
                if (h.succeeded()) {
                    log.info("undeployed.");
                } else {
                    log.error(h.cause());
                }
            });
        }
    });
  }
}

当“non-web”verticles 启动时,“web”verticles 会得到正确的通知。但是当“非网络”垂直关闭时,我敲了一个键盘Ctrl-C,我得到了这个错误,“网络”垂直仍然认为每个人都起来了:

2017-12-01 09:08:27 INFO  FileReader:31 - Undeploying 82a8f5c2-e6a2-4fc3-84ff-4bb095b5dc43
Exception in thread "Thread-3" java.lang.IllegalStateException: Shutdown in progress
at java.lang.ApplicationShutdownHooks.add(ApplicationShutdownHooks.java:66)
at java.lang.Runtime.addShutdownHook(Runtime.java:211)
at io.vertx.core.impl.FileResolver.setupCacheDir(FileResolver.java:310)
at io.vertx.core.impl.FileResolver.<init>(FileResolver.java:92)
at io.vertx.core.impl.VertxImpl.<init>(VertxImpl.java:185)
at io.vertx.core.impl.VertxImpl.<init>(VertxImpl.java:144)
at io.vertx.core.impl.VertxImpl.<init>(VertxImpl.java:140)
at io.vertx.core.impl.VertxFactoryImpl.vertx(VertxFactoryImpl.java:34)
at io.vertx.core.Vertx.vertx(Vertx.java:82)
at edu.foo.app.FileReaderApp$1.run(FileReaderApp.java:32)

I don't fully get what's going on. Application shutdown while it was undeploying verticle? How to solve this? What is the vertx.io approach?

4

1 回答 1

1

There are two problems

  1. You should undeploy the verticle using the clustered Vert.x instance, not just any instance
  2. undeploy is a non blocking operation so the shutdown hook thread must wait for completion.

Here's a modified version:

Runtime.getRuntime().addShutdownHook(new Thread() {
    public void run() {
        log.info("Undeploying " + id);
        CountDownLatch latch = new CountDownLatch(1);
        theClusteredVertxInstance.undeploy(id, h -> {
            if (h.succeeded()) {
                log.info("undeployed.");

            } else {
                log.error(h.cause());
            }
            latch.countDown();
        });
        try {
            latch.await(5, TimeUnit.SECONDS);
        } catch(Exception ignored) {
        }
    }
});
于 2017-12-01T10:09:48.867 回答