Reputation: 345
I have got a cluster of two server nodes IBM Websphere Application Server on two different physical machine.Can anybody help me with java code to check whether my server instance is running or when one of the server is not up and running?
Upvotes: 2
Views: 17206
Reputation: 22440
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
}
Upvotes: 1
Reputation: 6021
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.
Upvotes: 5
Reputation: 7324
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.
Upvotes: 1
Reputation: 31637
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!!!
Upvotes: 2