是使用套接字檢查服務器的好方法嗎? 這樣使用套接字檢查服務器的好方法是?
public boolean hostAvailabilityCheck(String serverAddress, Integer serverPort) {
try {
Socket s= new Socket(serverAddress, serverPort);
s.close();
} catch (IOException exception) {
return false;
}
return true;
}
,或者這是更好
public boolean isServerAvailable(String serverAddress, Integer serverPort) {
boolean isAlive = true;
try {
URL url = new URL("http:/" + serverAddress + serverPort);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.connect();
httpURLConnection.disconnect();
} catch (Exception e) {
isAlive = false;
}
return isAlive;
}
我試着去查看是否有Solr的服務器已啓動將數據推林不知道這是很好的!我希望 org.apache.solr.client.solrj.response 類SolrPingResponse做同樣的事情。謝謝
那麼,如果你可以連接到服務器,它最有可能啓動並運行。 –