有 Java 编程相关的问题?

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

java Web服务如何捕获ConnectException?

我有两个服务器(主服务器和辅助服务器)和相应的客户端。两者都使用相同的WSDL契约(因此我无法更改操作),因为我希望此服务器具有容错性。基本上,我所做的验证主服务器是否已启动并正在运行的工作是与辅助服务器ping。当主服务器没有应答时,我知道该服务器已关闭。我的主要问题是,当我尝试ping它发送的断开连接的服务器时:

javax.xml.ws.WebServiceException: java.net.ConnectException: Connection refused

如果从未在函数体中抛出此异常,如何捕获它?或者是否有其他方法来验证服务器是否已启动

代码如下:

辅助服务器

private void isAlive(){
        String alive = null;
        try {
            Client client = new Client(uddiURL, wsName);
            while(true){                
                try {
                    alive = client.ping("alive");
                } catch (ConnectException e1) {
                    System.out.println("Primary server is down");
                }
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if(alive.equals("alive")){
                    System.out.println("Still up");
                }
            }
        } catch (BrokerClientException e) {
            System.err.print("Failed to create BrokerClient");
            e.printStackTrace();
            return ;
        }
    }

主服务器

public String ping(String message){
        return message;
}

共 (0) 个答案