我有两台服务器的 AWS 实例。第一个是带有 selenium hub 的 win.server,第二个是作为 selenium 节点的 ubuntu 机器。有时 selenium 节点会中断,我正在寻找检查该节点可用性并在机器损坏时重新启动机器的最佳方法。提前致谢。
最佳答案
您可以通过检查节点的 session 使用以下代码检查节点是否损坏或断开连接:
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;
}
}
如果它给出“真”,那么您可以使用 AWS API 重新启动服务器或手动重新启动它。
在你的 pom.xml 中使用这个依赖:
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-bundle</artifactId>
<version>1.7</version>
</dependency>
希望对你有帮助:)
关于selenium - 如果节点损坏,如何使用 selenium 节点重新启动服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51355914/