開發管理用戶端程式
您可以開發管理用戶端程式來使用 WebSphere® Application Server 管理應用程式設計介面 (API) 和「Java™ 管理延伸 (JMX)」。
關於這項作業
產品管理 API 可供控制分散式系統的作業方面,且提供更新配置的能力。 如需 AdminClient 介面的相關資訊,請檢視應用程式設計介面文件。
請參閱 MBean 作業的範例。 如需 MBean 程式設計的相關資訊,請參閱 MBean Java API 文件。在這個資訊中心,按一下。
這個範例說明如何取得和使用產品所不提供的 NodeAgent MBean。 不過,如何取得及放置 NodeAgent MBean 的說明類似於您的產品所能使用的其他 MBean 的操作方式。
程序
結果
管理用戶端可以處理從 MBean 發出的事件通知。
避免困難: 如果用戶端程式透過 RMI 或 JSR160RMI 連接器來登錄通知接聽器,且 ORB 執行緒未停止執行,並因此阻止 Java 虛擬機器結束,請將 System.exit() 陳述式新增到用戶端程式中。
ORB 會啟動一個執行緒來處理對用戶端的通知傳播。
這個執行緒不會隨著用戶端主要執行緒而自動結束,除非主要執行緒有 System.exit() 陳述式。
請將 System.exit() 陳述式放在用戶端程式中,允許 ORB 執行緒和主要執行緒停止處理的位置。
例如,將 System.exit() 陳述式放在用戶端程式主要 try 區塊的 catch 或 finally 子句中。gotcha範例:管理用戶端程式
這個範例顯示如何連接至您的產品無法使用的節點代理程式伺服器。 不過,您可以變更主機和埠值來連接至伺服器。 請用您的伺服器來替換節點代理程式伺服器參照。 由於您的產品無法使用 NodeAgent MBean,請替換 queryNames 字串來搜尋另一個 MBean。
請將內容複製到名稱為 AdminClientExample.java 的檔案中。
將節點名稱和伺服器名稱變更為您的配置所適用的值之後,您可以依照利用 WebSphere Application Server 管理 Java API 來建立自訂 Java 管理用戶端程式中的指示來編譯及執行它。
import java.util.Date;
import java.util.Properties;
import java.util.Set;
import javax.management.InstanceNotFoundException;
import javax.management.MalformedObjectNameException;
import javax.management.Notification;
import javax.management.NotificationListener;
import javax.management.ObjectName;
import com.ibm.websphere.management.AdminClient;
import com.ibm.websphere.management.AdminClientFactory;
import com.ibm.websphere.management.exception.ConnectorException;
public class AdminClientExample implements NotificationListener
{
private AdminClient adminClient;
private ObjectName nodeAgent;
private long ntfyCount = 0;
public static void main(String[] args) {
AdminClientExample ace = new AdminClientExample();
// Create an AdminClient
ace.createAdminClient();
// Find a NodeAgent MBean
ace.getNodeAgentMBean("ellington");
// Invoke launchProcess
ace.invokeLaunchProcess("server1");
// Register for NodeAgent events
ace.registerNotificationListener();
// Run until interrupted
ace.countNotifications();
}
private void createAdminClient()
{
// Set up a Properties object for the JMX connector attributes
Properties connectProps = new Properties();
connectProps.setProperty(
AdminClient.CONNECTOR_TYPE, AdminClient.CONNECTOR_TYPE_SOAP);
connectProps.setProperty(AdminClient.CONNECTOR_HOST, "localhost");
connectProps.setProperty(AdminClient.CONNECTOR_PORT, "8879");
// Get an AdminClient based on the connector properties
try
{
adminClient = AdminClientFactory.createAdminClient(connectProps);
}
catch (ConnectorException e)
{
System.out.println("Exception creating admin client: " + e);
System.exit(-1);
}
System.out.println("Connected to DeploymentManager");
}
private void getNodeAgentMBean(String nodeName)
{
// Query for the ObjectName of the NodeAgent MBean on the given node
try
{
String query = "WebSphere:type=NodeAgent,node=" + nodeName + ",*";
ObjectName queryName = new ObjectName(query);
Set s = adminClient.queryNames(queryName, null);
if (!s.isEmpty())
nodeAgent = (ObjectName)s.iterator().next();
else
{
System.out.println("Node agent MBean was not found");
System.exit(-1);
}
}
catch (MalformedObjectNameException e)
{
System.out.println(e);
System.exit(-1);
}
catch (ConnectorException e)
{
System.out.println(e);
System.exit(-1);
}
System.out.println("Found NodeAgent MBean for node " + nodeName);
}
private void invokeLaunchProcess(String serverName)
{
// Use the launchProcess operation on the NodeAgent MBean to start
// the given server
String opName = "launchProcess";
String signature[] = { "java.lang.String" };
String params[] = { serverName };
boolean launched = false;
try
{
Boolean b = (Boolean)adminClient.invoke(
nodeAgent, opName, params, signature);
launched = b.booleanValue();
if (launched)
System.out.println(serverName + " was launched");
else
System.out.println(serverName + " was not launched");
}
catch (Exception e)
{
System.out.println("Exception invoking launchProcess: " + e);
}
}
private void registerNotificationListener()
{
// Register this object as a listener for notifications from the
// NodeAgent MBean. Don't use a filter and don't use a handback
// object.
try
{
adminClient.addNotificationListener(nodeAgent, this, null, null);
System.out.println("Registered for event notifications");
}
catch (InstanceNotFoundException e)
{
System.out.println(e);
e.printStackTrace();
}
catch (ConnectorException e)
{
System.out.println(e);
e.printStackTrace();
}
}
public void handleNotification(Notification ntfyObj, Object handback)
{
// Each notification that the NodeAgent MBean generates will result in
// this method being called
ntfyCount++;
System.out.println("***************************************************");
System.out.println("* Notification received at " + new Date().toString());
System.out.println("* type = " + ntfyObj.getType());
System.out.println("* message = " + ntfyObj.getMessage());
System.out.println("* source = " + ntfyObj.getSource());
System.out.println(
"* seqNum = " + Long.toString(ntfyObj.getSequenceNumber()));
System.out.println("* timeStamp = " + new Date(ntfyObj.getTimeStamp()));
System.out.println("* userData = " + ntfyObj.getUserData());
System.out.println("***************************************************");
}
private void countNotifications()
{
// Run until killed
try
{
while (true)
{
Thread.currentThread().sleep(60000);
System.out.println(ntfyCount + " notification have been received");
}
}
catch (InterruptedException e)
{
}
}
}
![[AIX Solaris HP-UX Linux Windows]](../images/dist.gif)
為了簡便,在伺服器所在的相同機器上執行用戶端程式;使用 localhost 作為主機名稱。
支援的配置:
作業主題