如果启动了新实例,则终止程序的旧实例

时间:2019-05-21 16:32:33

标签: java server java-threads

方案:程序启动服务器并侦听另一个程序以连接到该服务器;如果有另一个程序连接,请终止旧程序中的服务器,并在新程序中启动同一服务器,然后重复该循环。

第一次运行该程序后,我得到:

thread started
attempting connection
server started

然后,再次运行该程序后,第一个实例显示为:

thread started
attempting connection
server started
Another instance was started, this instance has been shut down
Exception in thread "Thread-0" java.lang.NullPointerException
    at me.aj.phoenix.util.JustOneServer.startServer(JustOneServer.java:37)
    at me.aj.phoenix.util.JustOneServer.run(JustOneServer.java:28)

,第二个实例显示为:

attempting connection
Another instance was running and has been closed.
server started
java.net.BindException: Address already in use: NET_Bind
    at java.base/java.net.PlainSocketImpl.bind0(Native Method)
    at java.base/java.net.PlainSocketImpl.socketBind(PlainSocketImpl.java:132)
    at java.base/java.net.AbstractPlainSocketImpl.bind(AbstractPlainSocketImpl.java:436)
    at java.base/java.net.ServerSocket.bind(ServerSocket.java:381)
    at java.base/java.net.ServerSocket.<init>(ServerSocket.java:243)
    at java.base/java.net.ServerSocket.<init>(ServerSocket.java:187)
    at me.aj.phoenix.util.JustOneServer.startServer(JustOneServer.java:34)
    at me.aj.phoenix.util.JustOneServer.run(JustOneServer.java:26)

本质上,我试图做的是每当启动新版本的程序时,关闭较旧的程序并在较新的程序上启动侦听器

代码如下:

public class TestProgram extends Thread {
    public final int port = 9665;
    ServerSocket serverSocket = null;
    Socket clientSocket = null;

    @Override
    public void run() {
        System.out.println("thread started");
        try {
            this.check();
            this.startServer();
        } catch (IOException ioe) {
            this.startServer();
        }
    }
    public void startServer() {
        try {
            System.out.println("server started");
            serverSocket = new ServerSocket(port, 1);
            clientSocket = serverSocket.accept();
            Logger.print("Another instance was started, this instance has been shut down");
            System.exit(0);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void check() throws UnknownHostException, IOException {
        System.out.println("attempting connection");
        Socket socket = new Socket("localhost", port);
        if(socket.isConnected()) {
            Logger.print("Another instance was running and has been closed.");
        }
    }
}

第二个程序没有等待第一个程序侦听服务器从端口上解除绑定,因此我不确定如何解决它。

1 个答案:

答案 0 :(得分:0)

主要问题是startServer方法中的逻辑应无限循环运行,以解决可能随时断开的套接字的弱特性,更重要的是要尝试连接到正在关闭的先前实例所绑定的端口。

        while (true) 
            runServer(port);

其他问题包括在出现问题时关闭资源,以及将来自客户端Socket的异常与来自ServerSocket的异常嵌套在一起;因此,只要为ServerSocket引发异常,就不会被解释为将终止startServer方法执行的异常。

    void runServer(int port) {
        try (ServerSocket serverSocket = new ServerSocket(port, 1)) {
            logger.debug("Server started!");
            try (Socket clientSocket = serverSocket.accept()) {
                logger.debug("Signal to shutdown received. Shutting down.");
                System.exit(0);
            }
        } catch (IOException e) {
            logger.debug("The other application is still shutting down...");
        }
    }

以下是此示例的完整工作示例:

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ServerReplacer {

    private static Logger logger = LoggerFactory.getLogger(ServerReplacer.class);

    public static void runReplacerService(int port) {
        searchOlderInstances(port);

        logger.debug("Server starting...");
        while (true) 
            runServer(port);
    }

    private static void searchOlderInstances(int port) {
        logger.debug("Attempting to find older instance...");
        try (Socket socket = new Socket("127.0.0.1", port)) {
            logOtherInstanceFound(socket.isConnected());
        } catch (Exception e) {
            logOtherInstanceFound(false);
        }
    }

    private static void logOtherInstanceFound(boolean otherInstanceFound) {
        logger.debug(otherInstanceFound ? 
            "FOUND ANOTHER INSTANCE RUNNING! It has been signaled to shut down." : 
            "No older instance found.");
    }

    private static void runServer(int port) {
        try (ServerSocket serverSocket = new ServerSocket(port, 1)) {
            logger.debug("Server started!");
            try (Socket clientSocket = serverSocket.accept()) {
                logger.debug("Signal to shutdown received. Shutting down.");
                System.exit(0);
            }
        } catch (IOException e) {
            logger.debug("The other application is still shutting down...");
        }
    }

    public static void main(String[] args) {
        runReplacerService(9665);
    }
}

希望这会有所帮助。