简体   繁体   English

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

[英]Java Executors Check TCP Connection Alive

I am trying to recognize that hosts are alive or dead with using executor in Java. 我试图通过在Java中使用executor来识别主机是活着还是死了。 In my case, I have severeal hosts which kept in a list. 在我的情况下,我有严格的主机保留在列表中。

My goal is to create threads with the number of hosts and checking them. 我的目标是创建具有多个主机并检查它们的线程。 When thread connect with the host, host doesnt close the connection, and sending a situation code such as 50 (dead) or 51(alive) continiously. 当线程与主机连接时,主机不会关闭连接,并且连续发送诸如50(死)或51(活动)的情况代码。

My problem is threads can only connect on host. 我的问题是线程只能在主机上连接。 For example; 例如;

I have two host 192.168.1.1 and 192.168.1.2. 我有两个主机192.168.1.1和192.168.1.2。 Threads should check both of them in the background but I can only connect in 1.1 线程应该在后台检查它们,但我只能在1.1中连接

CONNECTION 连接

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

Also I have syncronized setActive function inside of HOST.java 我还在HOST.java中同步了setActive函数

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

} }

This: 这个:

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

results in all hosts being connected to in a single Thread. 导致所有主机连接到单个线程中。 You want it to read something like 你想要它读取类似的东西

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM