Java 执行器检查 TCP 连接是否有效

标签 java multithreading

我试图通过在 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 函数

主机.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;
    }
}

连接函数

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

最佳答案

这个:

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

关于Java 执行器检查 TCP 连接是否有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52715011/

相关文章:

java - 自动化截图

java - 损坏的 Eclipse 构建路径?

java - 使用多个线程处理单个 HTTP 请求

c# - 嵌套并行 For 中的死锁风险

c - C 中多线程(pthreads)对数组元素求和

java - 使用 Spring Boot 创建 RESTful Web 服务

java - 填充二维数组时出现 NullPointerException

java - JScrollPane 内的 JPanel JFrame 内的 JPanel 内

java - 如何在Java中正确使用MongoDB多线程?

multithreading - BackgroundWorker 中的异步错误处理