I have AWS instance with two servers. The first one is win.server with selenium hub and the second one is ubuntu machine as selenium node. Sometimes selenium node breaks and i'm looking for the best way to check availability of this node and reboot the machine if it was broken. Thanks in advance.
1 回答
1
You can check if the node is broken or disconnected using below code by checking node's 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;
}
}
if it gives "true" then you can use AWS API to reboot the server or can reboot it manually.
use this dependency in your pom.xml :
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-bundle</artifactId>
<version>1.7</version>
</dependency>
Hope it helps you:)
于 2018-07-16T14:32:07.860 回答