About vertx, java.net.BindException: Address already in use

Now, I begin to learn vert.x.

At first, I tried to define a function that can work.

However, 当我第二次运行程序的时候, my current implementation gives me the following error:

三月 22, 2018 3:29:23 下午 io.vertx.core.http.impl.HttpServerImpl
严重: java.net.BindException: Address already in use

我希望第二次运行时,可以首先检测下端口,如果端口被占用,先关闭端口

I could not find an answer from the documentation.

Source code

package com.project.service;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import io.vertx.ext.web.Router;

import java.util.function.Consumer;

public class VerticleMain extends AbstractVerticle {


    @Override
    public void start() throws Exception {

        Router router = Router.router(vertx);

        router.route().handler(routingContext -> {
            routingContext.response()
                    .putHeader("content-type","text/html;charset=UTF-8")
                    .end("我的人设开始");
        });
        vertx.createHttpServer().requestHandler(router::accept).listen(8181);
    }

    public static void deployVertx() {
        String verticleId = VerticleMain.class.getName();
        VertxOptions options = new VertxOptions();
        Consumer<Vertx> runner = vertxStart -> {
            vertxStart.deployVerticle(verticleId);
        };
        Vertx vertx = Vertx.vertx(options);
        runner.accept(vertx);
    }

    public static void main(String[] args) {

        VerticleMain.deployVertx();
    }
}
阅读 3.7k
1 个回答

好像关闭端口占用,是要关闭占用端口的系统进程吧
你要先去查是哪个进程占用了该端口,然后在kill进程

其实你第一次运行完了关闭程序就可以了哇,何必搞的这么麻烦

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题