您可以使用 Health Center API 来编写自己的代码,以对从 Java™ 或 Node.js 应用程序中收集的 Health Center 数据进行操作。
无法将 API 与 Bluemix™ 应用程序一起使用。
开始之前
Health Center 提供 Node.js API 和 Java API。
Node.js API 在 healthcenter npm 模块中提供。该模块还包含最新的 Health Center 代理。有关该模块的更多信息,请参阅安装 Health Center。
在两个版本中提供 Java API:
- 插件版本,可以在 Eclipse 中使用。必须具有 Eclipse V3.4 或更高版本的实例。
- 普通 .jar 文件,可以与现有 Java 应用程序一起使用。
必须具有 IBM SDK Java Technology Edition V6 或更高版本。
关于此任务
通常使用 Health Center 客户机 GUI 来查看和分析受监控应用程序的数据。然而,您可能希望只查看一小部分数据,或者可能希望通过一种目前无法使用 GUI 来实现的方法对数据进行操作。通过使用 Health Center API,您可以编写应用程序,以根据您的需求对 Health Center 数据进行操作。
要使用 Node.js API,请阅读 API 文档中的指示信息,该文档在 healthcenter npm 模块中和 Health Center npm 模块页面 (
https://www.npmjs.com/package/healthcenter)上提供。
本主题的剩余部分将描述如何使用 Java API。
您可以通过以下方式使用 Java API:
- 仅事件方式
- 与非事件方式相比,该方式最多可减少使用 75% 内存。
- 在该方式中,您的监控应用程序将创建和注册侦听器,这些侦听器会响应受监控运行时环境中的事件,如垃圾回收周期。然后,您的监控应用程序会连接到 Health Center 代理以获取数据。如果要存储数据,那么必须将代码添加到您的监控应用程序。
注: 当您使用仅事件方式时,性能建议不可用。
- 非事件方式
- Health Center 客户机的实例嵌入在您的监控应用程序中,可存储监控数据。您的应用程序使用该实例来连接到 Health Center 代理。
过程
- 可选: 要使用 Java API 的插件版本,必须将 Health Center 安装到 Eclipse 中。
- 如果 Eclipse 中还没有 Health Center,请遵循 Eclipse 文档中的指示信息安装新软件,为软件站点指定以下 URL:
http://public.dhe.ibm.com/software/websphere/runtimes/tools/healthcenter/
- 在 Eclipse 中创建 Java 应用程序。 如果需要有关该步骤的更多信息,请使用 Eclipse 文档。
- 可选: 要将 API 与现有 Java 应用程序一起使用,请完成以下步骤:
- 从 Health Center 获取 API 包。 使用以下链接,但请注意,仅当从产品中查看本文档时,此链接才有效:monitoringapi.jar。
- 通过指定 -Xbootclasspath/p 命令行参数,在您运行应用程序时将 monitoringapi.jar 文件添加到引导程序类路径。 例如:
java -Xbootclasspath/p:./monitoringapi.jar helloworld
- 向 Java 应用程序添加 Health Center 代码,以连接到 Health Center 代理:
- 创建 ConnectionProperties 对象,可以选择指定主机名和端口号。 该对象用于存储将应用程序中的 Health Center 客户机连接到 Health Center 代理所必需的信息。有关更多信息,请参阅 ConnectionProperties 类。
- 将 ConnectionProperties 对象传递至某个 HealthCenterFactory.connect(ConnectionProperties
props ... ) 方法中,以返回 HealthCenter 对象。 该对象代表与受监控应用程序的连接。
例如:
- 您可以使用 HealthCenterFactory.connect(ConnectionProperties
props, boolean autoScan) 方法,将 autoScan 参数设置为 true,以连接到第一个可用代理。
- 您可以使用 HealthCenter.connect(ConnectionProperties
props java.lang.String agentName) 方法连接到通过 agentName 参数指定的代理。
您可以使用 scanForAgents(ConnectionProperties
props, int numberOfAgents) 方法来获取代理名称的列表。当您通过 MQTT 代理程序进行连接时,该选项将很有用,因为多个代理可以使用同一个代理程序。
- 您还可以通过使用 HealthCenterFactory.connect(java.io.File filename) 方法连接到保存的文件,而非正在运行的应用程序。
有关更多信息,请参阅
HealthCenterFactory 类。
代码示例:
...
// Create the connection object:
ConnectionProperties conn1 = new ConnectionProperties("localhost", 1973);
// Connect to the Health Center agent by using the connection object:
HealthCenter hcObject = HealthCenterFactory.connect(conn1, true);
...
- 向 Java 应用程序添加 Health Center 代码,以从代理中获取数据:
- 如果要使用仅事件方式,请启用此方式,然后根据需要创建并注册侦听器。您必须为每种类型数据(如 CPU 或垃圾回收)创建不同的侦听器实现。然后,向相应的数据对象注册侦听器,并从该数据对象中获取受监控的数据。例如:
...
// Switch on event-only mode:
hcObject.setEventOnlyMode(true);
// Define and create a garbage collection listener.
// The listener receives a garbage collection event object after an event:
class myGCEventListener implements GCEventListener {
@Override
public void gcEvent(GCEvent event) {
System.out.println("GC event detected");
System.out.println("event.gcEventTime " + event.gcEventTime);
System.out.println("event.gcPauseTime " + event.gcPauseTime);
System.out.println("event.heapSize " + event.heapSize);
System.out.println("event.usedHeapAfterGC " + event.usedHeapAfterGC);
System.out.println("event.reason " + event.reason);
System.out.println("event.type " + event.type);
}
}
myGCEventListener gclistener = new myGCEventListener();
// Register the listener with a garbage collection data object:
GCData gcData = hcObject.getGCData();
gcData.addGCListener(gclistener);
// Extract data from the data object:
System.out.println("GC Mode is " + gcData.getGCMode().toString());
...
- 如果要使用非事件方式,请使用 HealthCenter 对象获取表示您感兴趣的数据类型的数据对象,然后从该数据对象中检索数据。例如:
...
// Get the garbage collection data and print it:
GCData gcData = hcObject.getGCData();
System.out.println("GC Mode is " + gcData.getGCMode().toString());
...
您还可以使用 HealthCenter 对象来限制从运行中应用程序返回的数据量。
有关能够检索的数据的更多信息以及关联的方法和类,请参阅 Health Center Java API 文档。