有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

当我使用vert时使用java。要创建路由,地址已在使用中

在下面的代码中,我使用的是vert。使用x创建管线

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("people");
        });
        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();
    }
}

但是,当我再次尝试执行代码时,日志是

java.net.BindException: Address already in use

如果使用这个端口,我想停止占用该端口的进程,然后执行代码。有没有办法实现这个目标

我希望你能提供一个简单的例子


共 (2) 个答案

  1. # 1 楼答案

    你的代码非常好。请关闭所有java进程或重新启动计算机,然后重试。它应该很好用。只有一个导入丢失,我补充说

    package com.americanexpress.digitalpayments.pipe;
    
    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 {
    
    
    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();
    }
    
    @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("people");
        });
        vertx.createHttpServer().requestHandler(router::accept).listen(8181);
    }
    

    }

  2. # 2 楼答案

    您需要终止端口:

    对于ubuntu:

    sudo kill $(sudo lsof -t -i:8081)