2012-01-24 99 views
2

我在兩臺不同的物理機器上安裝了兩臺服務器節點IBM Websphere Application Server。任何人都可以用java代碼來幫助我檢查服務器實例是否正在運行,或者其中一臺服務器未啓動並正在運行?如何檢查服務器已啓動?

+1

是不是WebSphere ND提供了這個框? –

+0

我的目標是在服務器停機/掛起時得到通知。你能告訴我,ND是如何提供這些細節的? –

+0

只需要一個已知的頁面,並確保你找回來;微不足道的cron腳本或其他。 –

回答

5

要快速且便攜地執行此操作,可以檢查服務器是否提供頁面。

例如,您可以:

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服務器的工作。下面

+0

@ Andrea Colleoni:我相信'boolean isAlive = false;'應該是'boolean isAlive = true;'..對嗎? –

+0

對!抱歉... –

+0

這缺少一個hpCon.connect()調用來實際建立連接。 –

2

希望是你想要的...

或失敗:

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

好運!

+0

Fahim/Andrea我感謝你們兩位的及時答覆。 –

+0

歡迎您..... –

1

我認爲您可能需要的是使用WebSphere Thin Administrative Client,它公開了Java API並提供對WAS MBeans的訪問,使您可以查詢服務器/應用程序的狀態(以及許多其他管理和監視任務)。

首先,你要得到一個連接被(在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()}); 

根據需要,如查詢個人集羣成員的狀態,您可以調用進一步的操作。

此外,除了查詢,也可以register notifications讓你的程序可以在特定事件發生的通知,如集羣,服務器或應用程序狀態的變化。

+0

+1良好的使用mbeans –

1

我們使用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 
} 
+0

@ Vogel612代碼的答案是好的。 Chillax。 – Zizouz212

+0

@ Vogel612感謝您的反饋。我有同樣的問題,想分享代碼,我認爲這很清楚,但肯定可以更清楚。現在我試着解釋一下代碼。 – Weslor

相關問題