简体   繁体   中英

How to check server is up?

我在两个不同的物理机器上有两个服务器节点IBM Websphere Application Server的集群。任何人都可以帮助我使用java代码检查我的服务器实例是否正在运行,或者其中一个服务器是否未启动并运行?

To do it quickly and portably, you can check for a page if it's served by the server.

For example you can:

boolean isAlive = true;
try {
  URL hp = new URL("http://yourserver/TestPage.html"); 
  URLConnection hpCon = hp.openConnection(); 
  // add more checks...
} catch (Exception e) {
  isAlive = false;
}

This not much sophisticated method will work with every http server.

Hope below is what you want...

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());

or for failure:

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 ");
}

Good Luck!!!

We get the specialized connection for the URL we want using openConnection() . It will return a subclass of the abstract class URLConnection , depending on the URL's public protocol, for example a HttpURLConnection . Then with the method connect() opens the communication link

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
}

I think what you may be looking for is the use of the WebSphere Thin Administrative Client , which exposes Java APIs and provides access to the WAS MBeans that allow you to query the status of your servers/applications (along with many other management and monitoring tasks).

First, you'll want to obtain a connection to WAS (the AdminClient ) as follows:

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);

Next, you'll want to query for the relevant MBeans, and then perform the relevant operations. In your case, you may be interested in the ClusterMgr and/or J2EEApplication MBeans. Here is an example that queries for the Cluster's state:

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()});

You can invoke further operations as desired, such as querying the individual cluster member's status.

Also, in addition to querying, you can also register notifications so that your program can be notified when certain events happen, such as a change in state of clusters, servers, or applications.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM