开发管理客户机程序
可以开发使用 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示例:管理客户机程序
此示例说明如何连接至您的产品不能使用的 Node Agent 服务器。但是,可以通过更改主机和端口值来连接至服务器。使用您的服务器替换 Node Agent 服务器引用。由于您的产品不能使用 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 用作主机名。
支持的配置:
任务主题