[英]How to check server is up?
我在两个不同的物理机器上有两个服务器节点IBM Websphere Application Server的集群。任何人都可以帮助我使用java代码检查我的服务器实例是否正在运行,或者其中一个服务器是否未启动并运行?
要快速,便携地执行此操作,您可以检查页面是否由服务器提供。
例如,你可以:
boolean isAlive = true;
try {
URL hp = new URL("http://yourserver/TestPage.html");
URLConnection hpCon = hp.openConnection();
// add more checks...
} catch (Exception e) {
isAlive = false;
}
这种不太复杂的方法适用于每个http服务器。
希望下面是你想要的......
URL url = new URL( "http://google.com/" );
HttpURLConnection httpConn = (HttpURLConnection)url.openConnection();
httpConn.setInstanceFollowRedirects( false );
httpConn.setRequestMethod( "HEAD" );
httpConn.connect();
System.out.println( "google.com : " + httpConn.getResponseCode());
或失败:
URL url = new URL( "http://google.com:666/" );
HttpURLConnection httpConn = (HttpURLConnection)url.openConnection();
httpConn.setInstanceFollowRedirects( false );
httpConn.setRequestMethod( "HEAD" );
try{
httpConn.connect();
System.out.println( "google.com : " + httpConn.getResponseCode());
}catch(java.net.ConnectException e){
System.out.println( "google.com:666 is down ");
}
祝好运!!!
我们使用openConnection()
获得了我们想要的URL的专用连接。 它将返回抽象类URLConnection
的子类,具体取决于URL的公共协议,例如HttpURLConnection
。 然后使用方法connect()
打开通信链接
private String server = "http://testserver:9086";
try {
URLConnection hpCon = new URL(SERVER).openConnection();
hpCon.connect();
} catch (Exception e) {
// Anything you want to do when there is a failure
}
我认为您可能正在寻找的是使用WebSphere Thin Administrative Client ,它公开Java API并提供对WAS MBean的访问,允许您查询服务器/应用程序的状态(以及许多其他管理和监视任务) )。
首先,您需要获得与WAS( AdminClient
)的连接,如下所示:
Properties clientProps = new Properties();
clientProps.setProperty(AdminClient.CONNECTOR_TYPE, AdminClient.CONNECTOR_TYPE_SOAP);
clientProps.setProperty(AdminClient.CONNECTOR_HOST, dmgrHostname);
clientProps.setProperty(AdminClient.CONNECTOR_PORT, dmgrSoapConnectorPort);
if (dmgrIsSecure) {
clientProps.setProperty(AdminClient.CONNECTOR_SECURITY_ENABLED, "true");
clientProps.setProperty(AdminClient.USERNAME, wasUsername);
clientProps.setProperty(AdminClient.PASSWORD, wasUserPassword);
}
AdminClient adminClient = AdminClientFactory.createAdminClient(clientProps);
接下来,您将要查询相关的MBean,然后执行相关操作。 在您的情况下,您可能对ClusterMgr和/或J2EEApplication MBean感兴趣。 以下是查询群集状态的示例:
AdminClient adminClient = getAdminClient(target);
ObjectName clusterMgr =
(ObjectName)adminClient.queryNames(
ObjectName.getInstance("WebSphere:*,type=ClusterMgr"), null).iterator().next();
String state = adminClient.invoke(clusterMgr, "getClusterState",
new Object[] {clusterName}, new String[] {String.class.getName()});
您可以根据需要调用进一步的操作,例如查询单个集群成员的状态。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.