我有两个服务器的AWS实例。第一个是带有硒中心的win.server,第二个是作为硒节点的ubuntu机器。有时硒节点发生故障,我正在寻找最好的方法来检查此节点的可用性并在机器损坏时重新启动计算机。预先感谢。
答案 0 :(得分:1)
您可以使用以下代码通过检查节点的会话来检查节点是否断开或断开连接:
import javax.ws.rs.core.MediaType;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
public class TestJerseyClient {
public static void main(String[] args) {
String nodeURL = "http://10.11.208.114:5555/wd/hub/sessions"; // replace your IP and port here
System.out.println(isNodeDisconnected(nodeURL));
}
/** It will check if any node is disconnected from hub in Selenium Grid
* @param nodeURL
* @return node connection status
*/
private static boolean isNodeDisconnected(String nodeURL) {
boolean isNodeDisconnected= false;
try {
Client client = Client.create();
WebResource webResource = client.resource(nodeURL);
ClientResponse response = webResource.accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
String output = response.getEntity(String.class);
System.out.println(output);
} catch (Exception e) {
if (e.getMessage().contains("java.net.ConnectException")) {
isNodeDisconnected= true;
}
System.out.println("The node is disconneted and needs to be connected again !!!!!!!!!");
}
return isNodeDisconnected;
}
}
如果它给出“ true”,那么您可以使用AWS API重新启动服务器或手动重新启动。
在pom.xml中使用此依赖项:
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-bundle</artifactId>
<version>1.7</version>
</dependency>
希望它对您有帮助:)