Java执行器检查TCP连接是否存活

6

我正在尝试使用Java中的执行器来识别主机是活着还是死了。在我的情况下,我有一个保存在列表中的多个主机。

我的目标是创建与主机数量相同的线程并对它们进行检查。当线程连接到主机时,主机不会关闭连接,并连续发送状态代码,如50(死亡)或51(存活)。

我的问题是线程只能连接一个主机。例如;

我有两个主机192.168.1.1和192.168.1.2。线程应该在后台检查它们两个,但我只能连接1.1。

连接

List <Host> hosts = LoadBalancer.getHostList();
ExecutorService executor = Executors.newFixedThreadPool(hosts.size());

executor.submit(()->{
    for (Host host:hosts) {
        try {
            connect(host,"message",1);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
});

同时,我已经在HOST.java中同步了setActive函数。

HOST.JAVA

public class Host {
    private String ip;
    private int port;
    private boolean isActive;

    public Host(String ip, int port) {
        this.ip = ip;
        this.port = port;
        this.isActive = true;
    }

    public synchronized boolean isActive() {
        return isActive;
    }

    public synchronized void setActive(boolean active) {
        isActive = active;
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }
}

Connect function

public static void connect(Host host, String message, int mode) throws Exception {
EventLoopGroup group = new NioEventLoopGroup();
try {
    Bootstrap clientBootstrap = new Bootstrap();

    clientBootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 500);

    clientBootstrap.group(group);
    clientBootstrap.channel(NioSocketChannel.class);
    clientBootstrap.remoteAddress(new InetSocketAddress(host.getIp(), host.getPort()));

    clientBootstrap.handler(new ChannelInitializer<SocketChannel>() {
        protected void initChannel(SocketChannel socketChannel) {

            //TODO, TIMEOUT BILGISI ILE DOLDUR BURAYI
            //socketChannel.pipeline().addLast(new ReadTimeoutHandler(1));
            //socketChannel.pipeline().addLast("idleStateHandler", new IdleStateHandler(1, 1, 2));

            socketChannel.pipeline().addLast(new ClientHandler(host, message, mode));
        }
    });

    ChannelFuture channelFuture = clientBootstrap.connect().sync();
    channelFuture.channel().closeFuture().sync();
} catch (Exception e) {
    System.err.println("Connection timed out --> " + e);
    host.setActive(false); //connection kurulamadı demektir. Bir sonraki mesaj geldiğinde bu hostun açılıp açılmadığı denenecek.
} finally {
    group.shutdownGracefully().sync();
}

}


是的,你说得对。我忘记在我的问题顶部写连接方法了。我刚刚添加了。谢谢。 - Berkin
我会编辑我的问题。 - Berkin
您正在向一个线程池提交单个可运行对象,其中您在循环中检查所有主机。因此,您没有正确使用该线程池(在我看来,为此任务拥有太多线程的线程池是一种浪费)。 - gusto2
是的,我猜到了。我在这个领域还很新,所以犯了一些错误。 - Berkin
非常感谢您的快速回复! - GhostCat
1个回答

3

This:

executor.submit(()->{
     for (Host host:hosts) {
        try {
            connect(host,"message",1);
            } catch (Exception e) {
            e.printStackTrace();
        }
    }
});

这个问题会导致所有主机都在单个线程中连接。您希望它显示为:


for (Host host: hosts) {
    executor.submit(()->{
        try {
            connect(host,"message",1);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
});

不错!我忽略了那个,但它确实非常明显;-) - GhostCat

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接