开发管理客户机程序

可以开发使用 WebSphere® Application Server 管理应用程序编程接口 (API) 和 Java™ 管理扩展 (JMX) 的管理客户机程序。

关于此任务

产品管理 API 控制分布式系统的操作方面以及提供更新配置的能力。有关 AdminClient 接口的信息,请查看应用程序编程接口文档。

请参阅 MBean 操作的示例。有关 MBean 编程的信息,请参阅 MBean Java API 文档。 在此信息中心中,单击引用 > 编程接口 > Mbean 接口

过程

  1. 创建 AdminClient 实例。

    管理客户机程序需要对 AdminService 对象调用方法,该对象在 Deployment Manager 或基本安装中的应用程序服务器中运行。

    AdminClient 类通过受支持的某个 Java 管理扩展 (JMX) 连接器为远程 AdminService 对象提供代理。
    • 以下示例显示如何为简单对象访问协议 (SOAP) 连接器创建 AdminClient 实例:
      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");
      connectProps.setProperty(AdminClient.USERNAME, "test2");
      connectProps.setProperty(AdminClient.PASSWORD, "user24test");
      AdminClient adminClient = null;
      try
      {
             adminClient = AdminClientFactory.createAdminClient(connectProps);
      }
      catch (ConnectorException e)
      {
             System.out.println("Exception creating admin client: " + e);
      }
      1. 设置属性对象。

        该示例使用访问服务器所需的属性来设置属性对象。在这种情况下,使用 SOAP 连接器来连接到服务器;对于连接器类型,使用值:AdminClient.CONNECTOR_TYPE_SOAP。

      2. [AIX Solaris HP-UX Linux Windows][IBM i]为简便起见,在服务器所在的机器上运行客户机程序;将 localhost 用作主机名。

        要访问远程主机而不是本地主机,请对该主机使用可解析的网络名称。

      3. 设置服务器 SOAP 连接器正在侦听的端口号。

        在单服务器安装中,应用程序服务器 SOAP 连接器的缺省端口号为 8880。WebSphere Application Server Network Deployment 安装中,Deployment Manager SOAP 连接器的缺省端口号为 8879。

      4. 在设置连接属性后,使用 AdminClientFactory 类和属性对象来创建连接至所选服务器的 AdminClient 对象。

        根据各种考虑因素(如您期望的协议和安全环境),您可能需要设置其他属性。例如,如果为应用程序客户机程序启用安全性,那么包括 javax.net.ssl.* 属性。 有关 AdminClient 接口(javax.net.ssl.* 属性)和其他创建示例的更详细信息,请参阅应用程序编程接口文档中的 AdminClient 接口。

    • 以下示例显示如何为远程方法调用 (RMI) 连接器创建 AdminClient 实例。为了打印方便,这些命令被拆分为多行。
      Properties connectProps = new Properties();
      connectProps.setProperty(AdminClient.CONNECTOR_TYPE, AdminClient.CONNECTOR_TYPE_RMI);
      connectProps.setProperty(AdminClient.CONNECTOR_HOST, "localhost");
      connectProps.setProperty(AdminClient.CONNECTOR_PORT, "2809");
      connectProps.setProperty(AdminClient.USERNAME, "test2");
      connectProps.setProperty(AdminClient.PASSWORD, "user24test");
      System.setProperty("com.ibm.CORBA.ConfigURL", 
       "file:C:/AA/cf010839.26/profiles/AppSrv02/properties/sas.client.props");
      System.setProperty("com.ibm.SSL.ConfigURL", 
       "file:C:/AA/cf010839.26/profiles/AppSrv02/properties/ssl.client.props");
      AdminClient adminClient = null;
      try
      {
             adminClient = AdminClientFactory.createAdminClient(connectProps);
      }
      catch (ConnectorException e)
      {
             System.out.println("Exception creating admin client: " + e);
      }
      支持的配置 支持的配置: 当在应用程序服务器上运行的应用程序代码(例如,servlet 和 JavaServer Pages (JSP) 文件)内使用 createAdminClient 方法时,必须将 CACHE_DISABLED 属性设置为 true。例如:
      connectProps.setProperty(AdminClient.CACHE_DISABLED, "true");
      sptcfg
      1. 设置属性对象。

        该示例使用访问服务器所需的属性来设置属性对象。在这种情况下,使用远程方法调用连接器连接到服务器;对于连接器类型,使用值:AdminClient.CONNECTOR_TYPE_RMI。

      2. [AIX Solaris HP-UX Linux Windows][IBM i]为简便起见,在服务器所在的机器上运行客户机程序;将 localhost 用作主机名。

        要访问远程主机而不是本地主机,请对该主机使用可解析的网络名称。

      3. 设置服务器 RMI 连接器正在侦听的端口号。

        在单服务器安装中,应用程序服务器 RMI 连接器的缺省端口号为 2809。在 WebSphere Application Server Network Deployment 安装中,Deployment Manager RMI 连接器的缺省端口号为 9809。

      4. 在设置连接属性后,使用 AdminClientFactory 类和属性对象来创建连接至所选服务器的 AdminClient 对象。

        根据各种考虑因素(如您期望的协议和安全环境),您可能需要设置其他属性。例如,如果对应用程序客户机程序启用安全性,那么需要设置系统属性以指向 ssl.client.props 文件和 sas.client.props 文件。如果在本地机器上运行,那么可以指向实际位置。如果在远程机器上运行,那么可以从服务器机器中复制这些属性文件并将其放置到任何位置,然后指定放置这些文件的位置路径。

        可以在 sas.client.props 文件中指定用户名和密码,当您这样做时,请指定 com.ibm.CORBA.loginSource=properties。 如果您要在客户机程序中设置用户名和密码,请在 sas.client.props 文件中指定 com.ibm.CORBA.loginSource=none

  2. 查找 MBean。

    在获取 AdminClient 实例时,可使用它来访问管理服务器和应用程序服务器中的受管资源。每个受管资源都向可以用来访问资源的 AdminService 注册一个 MBean。MBean 由标识 MBean 的 ObjectName 实例表示。ObjectName 实例由后跟一个或多个关键属性的无序集的域名组成。域名的语法如下所示:

    [domainName]:property=value[,property=value]*
    对于 WebSphere Application Server,域名为 WebSphere,为管理定义的关键属性如下:
    表 1. 关键属性描述. 关键属性包括类型、名称、单元、节点以及进程。
    关键属性 描述
    type MBean 的类型。例如:服务器、跟踪服务和 Java 虚拟机 (JVM)。
    name MBean 的各个实例的名称标识。
    cell 运行 MBean 的单元的名称。
    node 运行 MBean 的节点的名称。
    process MBean 运行的进程的名称。

    WebSphere Application Server 中的某些 MBean 使用其他关键属性。可在 WebSphere Application Server 过程中向 MBean 服务器注册没有关键属性的 MBean。但是,这类 MBean 不能参与产品添加的分布式增强功能,例如请求路由、分布式事件通知等。

    如果您知道 ObjectName 实例的一组完整关键属性,那么可使用它来查找它标识的 MBean。但是,在不必知道所有关键属性的情况下查找 MBean 通常更为可行更加便利。使用通配符 (*) 来表示不需要匹配的任何关键属性。下表提供对象名的一些示例,这些对象名带有与单个或多个 MBean 相匹配的通配符关键属性。
    表 2. 具有通配符关键属性的示例对象名称. 包括星号 (*) 以指定通配符关键属性。
    对象名 描述
    *:type=Server,* 类型为服务器的所有 MBean
    *:node=Node1,type=Server,* Node1 上类型为服务器的所有 MBean
    *:type=JVM,process=server1,node=Node1,* 节点 Node1 上名为 server1 的服务器中的 JVM MBean
    *:process=server1,* 名为 server1 中的所有服务器中的所有 MBean
    *:process=server1,node=Node1,* Node1 上名为 server1 的服务器中的所有 MBean

    要查找 MBean,可以使用与关键属性相匹配的对象名来查询该 MBean。以下示例显示如何为节点 MyNode 的 Node Agent 查找 MBean:

    String nodeName = "MyNode";
    String query = "WebSphere:type=NodeAgent,node=" + nodeName + ",*";
    ObjectName queryName = new ObjectName(query);
    ObjectName nodeAgent = null;
    Set s = adminClient.queryNames(queryName, null);
    if (!s.isEmpty())
        nodeAgent = (ObjectName)s.iterator().next();
    else
        System.out.println("Node agent MBean was not found");
    1. 使用查询字符串(用于指定类型和节点的关键属性)来构建 ObjectName 实例。

      通过使用通配符来表示余下关键属性,此模式将与节点 MyNode 上类型为 Node Agent 的所有 MBean 的对象名相匹配。因为每个节点只存在一个 Node Agent,所以此信息足以标识您想要的 MBean。

    2. 将此 ObjectName 实例提供给 AdminClient 接口的 queryNames 方法。

      AdminClient 接口执行对 AdminService 接口的远程调用以获取与查询相匹配的一组 MBean 对象名。此方法的第二个空参数是查询表达式 (QueryExp) 对象,可将其用作基于 MBean(与第一个参数中的 ObjectName 模式相匹配)的另一查询。

    3. 使用设置迭代器来获取第一个(在这种情况下也是唯一一个)元素。

      该元素是 Node Agent 的 MBean ObjectName 实例。

  3. 使用 MBean。

    特定 MBean 可执行的操作取决于该 MBean 的管理接口。MBean 可声明:

    • 可获取或设置的属性
    • 可调用的操作
    • 可对其注册侦听器的通知
    对于 WebSphere Application Server 提供的 MBean,可以在 MBean API 文档中找到有关他们支持的接口的信息。 以下示例调用先前找到的 Node Agent MBean 上的其中一个可用操作。以下示例将启动 MyServer 应用程序服务器:
    String opName = "launchProcess";
    String signature[] = { "java.lang.String" };
    String params[] = { "MyServer" };
    try
    {
         adminClient.invoke(nodeAgent, opName, params, signature);
    }
    catch (Exception e)
    {
         System.out.println("Exception invoking launchProcess: " + e);
    }
    AdminClient.invoke 方法是用于在任何 MBean 上调用任何操作的通用方法。参数包括:
    • 目标 MBean 的对象名 nodeAgent
    • 操作的名称 opName
    • 包含操作参数的对象数组 params
    • 包含操作特征符的字符串数组 signature
    示例中的 launchProcess 操作有一个参数,它是用于标识要启动的服务器的字符串。

    调用方法返回对象实例,调用代码可使用它来转换为被调用操作的返回类型。launchProcess 操作将声明为空,以便可以忽略此示例中的返回值。

  4. 注册事件。

    除了管理资源之外,JMX API 还支持特定管理事件的应用程序监视。某些事件产生通知,例如,当服务器启动时。管理应用程序可以注册为这些通知的侦听器。WebSphere Application Server 提供了 JMX 通知模型的完整实现,并且提供了其他功能,所以您可以在分布式环境中接收通知。要获取从产品 MBean 发出的通知的完整列表,请参阅 MBean API 文档中的 com.ibm.websphere.management.NotificationConstants 类。

    以下示例显示对象如何使用 ObjectName Node Agent 向通过 MBean 发出的事件通知注册对象。

    adminClient.addNotificationListener(nodeAgent, this, null, null);

    在此示例中,第一个参数是 Node Agent MBean 的 ObjectName。第二个参数标识侦听器对象,它必须实现 NotificationListener 接口。在这种情况下,调用对象为侦听器。第三个参数是过滤器,可使用它来指示要接收的通知。将此参数保留为 null 时,将从此 MBean 接收所有通知。最后一个参数为 handback 对象,可使用它将 JMX API 设置为发出通知时返回给您。

    如果 MBean 在单元中的另一个服务器上,即使管理客户机程序可能连接至 Deployment Manager 服务器,也可以接收到它的通知。所有通知流向上游服务器。例如,来自应用程序服务器的通知先流向本地 Node Agent,然后流向 Deployment Manager。

    Application Server 提供的另一个增强功能就是使用一个调用注册为多个 MBean 的通知侦听器。此注册将通过 AdminClient 接口的 addNotificationListenerExtended 方法完成,它是标准 JMX addNotificationListener 方法的扩展。此扩展方法甚至允许您向当前不活动的 MBean 注册。此注册在以下情况下很重要,在这些情况中,您想要监视资源中的一些事件,这些事件可在管理客户机程序的有效时间内停止并重新启动。

  5. 处理事件。

    对象通过 handleNotification 方法接收 JMX 事件通知,此方法是通过 NotificationListener 接口定义的,任何事件接收器都必须实现该方法。以下示例是 handleNotification 方法的实现,它将报告接收到的通知:

    public void handleNotification(Notification n, Object handback)
    {
         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("***************************************************");
    }

结果

管理客户机可以处理从 MBean 发出的事件通知。

避免故障 避免故障: 如果客户机程序通过 RMI 或 JSR160RMI 连接器来注册通知侦听器,并且 ORB 线程未停止运行,并因此而阻止了 Java 虚拟机退出,请将 System.exit() 语句添加到客户机程序。ORB 将启动一个线程来处理对客户机的通知传播。除非客户机主线程具有 System.exit() 语句,否则此线程不会自动随该主线程一起退出。请将 System.exit() 语句放置在客户机程序中允许 ORB 线程和主线程停止处理的某个位置。例如,将 System.exit() 语句放置在客户机程序的主 try 块的 catchfinally 子句中。gotcha

示例:管理客户机程序

请将内容复制到名为 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)
        {
        }
    }
    
}

指示主题类型的图标 任务主题



时间戳记图标 最近一次更新时间: last_date
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=cord&product=was-nd-mp&topic=tjmx_develop
文件名:tjmx_develop.html