3.10. 服务器生命周期事件通知


您可以使用 JBoss EAP core-management 子系统JMX 为服务器生命周期事件设置通知。服务器运行时配置状态或服务器运行状态的更改将触发通知。

JBoss EAP 的服务器运行时配置状态是 STARTINGRUNNINGRELOAD_REQUIREDRESTART_REQUIREDSTOPPINGSTOPPED

运行 JBoss EAP 的服务器状态为 STARTING,NORMAL,ADMIN_ONLY,PRE_SUSPEND,SUSPENDING,SUSPENDED,STOPPING, 和 STOPPED

您可以将监听程序注册到 JBoss EAP core-management 子系统,以监控服务器生命周期事件。以下步骤演示了如何创建并注册将事件记录到文件中的示例监听程序。

先决条件

  • JBoss EAP 正在运行。

流程

  1. 创建监听程序。

    创建 org.wildfly.extension.core.management.client.ProcessStateListener 的实现,如下例所示。

    示例:Listener 类

    package org.simple.lifecycle.events.listener;
    
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    
    import org.wildfly.extension.core.management.client.ProcessStateListener;
    import org.wildfly.extension.core.management.client.ProcessStateListenerInitParameters;
    import org.wildfly.extension.core.management.client.RunningStateChangeEvent;
    import org.wildfly.extension.core.management.client.RuntimeConfigurationStateChangeEvent;
    
    public class SimpleListener implements ProcessStateListener {
    
        private File file;
        private FileWriter fileWriter;
        private ProcessStateListenerInitParameters parameters;
    
        public void init(ProcessStateListenerInitParameters parameters) {
            this.parameters = parameters;
            this.file = new File(parameters.getInitProperties().get("file"));
            try {
                fileWriter = new FileWriter(file, true);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        public void cleanup() {
            try {
                fileWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                fileWriter = null;
            }
        }
    
        public void runtimeConfigurationStateChanged(RuntimeConfigurationStateChangeEvent evt) {
            try {
                fileWriter.write(String.format("Runtime configuration state change for %s: %s to %s\n", parameters.getProcessType(), evt.getOldState(), evt.getNewState()));
                fileWriter.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        public void runningStateChanged(RunningStateChangeEvent evt) {
            try {
                fileWriter.write(String.format("Running state change for %s: %s to %s\n", parameters.getProcessType(), evt.getOldState(), evt.getNewState()));
                fileWriter.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    Copy to Clipboard Toggle word wrap

    注意

    在实施监听器时请注意以下几点:

    • 如果服务器重新加载,侦听器会在服务器尝试停止时停止侦听,并在服务器启动时重新加载监听程序。因此,实施必须确保在同一 JVM 内正确加载、初始化和删除它们。
    • 对监听器的通知阻止阻止对服务器状态的更改。实施必须确保它们不会阻止或死锁。
    • 每个监听器实例都以自己的线程执行,且不会保证顺序。
  2. 编译类并将其打包为 JAR。

    请注意,要编译,您需要依赖于 org.wildfly.core:wildfly-core-management-client Maven 模块。

  3. 将 JAR 添加为 JBoss EAP 模块。

    使用以下管理 CLI 命令,并提供 JAR 的模块名称和路径。

    module add --name=org.simple.lifecycle.events.listener --dependencies=org.wildfly.extension.core-management-client --resources=/path/to/simple-listener-0.0.1-SNAPSHOT.jar
    Copy to Clipboard Toggle word wrap
    重要

    使用 module 管理 CLI 命令仅作为技术预览提供和删除模块。此命令不适合在受管域中使用,或者在远程连接到管理 CLI 时。在生产环境中应该手动 添加和删除 模块。

    技术预览功能不包括在红帽生产服务级别协议(SLA)中,且其功能可能并不完善。因此,红帽不建议在生产环境中使用它们。这些技术预览功能可以使用户提早试用新的功能,并有机会在开发阶段提供反馈意见。

    如需有关技术预览功能支持范围的信息,请参阅红帽客户门户网站上的 技术预览功能支持范围

  4. 注册侦听器。

    使用以下管理 CLI 命令,将侦听器添加到 core-management 子系统:指定类、模块和文件位置,以记录服务器生命周期事件。

    /subsystem=core-management/process-state-listener=my-simple-listener:add(class=org.simple.lifecycle.events.listener.SimpleListener, module=org.simple.lifecycle.events.listener,properties={file=/path/to/my-listener-output.txt})
    Copy to Clipboard Toggle word wrap

现在,服务器生命周期事件将根据上面的 SimpleListener 类记录到 my-listener-output.txt 文件中。例如,在管理 CLI 中发出 :suspend 命令会将以下内容输出到 my-listener-output.txt 文件。

Running state change for STANDALONE_SERVER: normal to suspending
Running state change for STANDALONE_SERVER: suspending to suspended
Copy to Clipboard Toggle word wrap

这表明,running 状态从 normal 改为 suspending,然后从 挂起 挂起

3.10.2. 使用 JMX 通知监控服务器生命周期事件

您可以注册 JMX 通知监听程序,以监控服务器生命周期事件。以下步骤演示了如何创建并添加将事件记录到文件中的示例监听程序。

先决条件

  • JBoss EAP 正在运行。

流程

  1. 创建监听程序。

    创建 java.management.NotificationListener 的实现,如下例所示。

    示例:Listener 类

    import java.io.BufferedWriter;
    import java.io.IOException;
    import java.nio.charset.StandardCharsets;
    import java.nio.file.Files;
    import java.nio.file.Path;
    import java.nio.file.Paths;
    import java.nio.file.StandardOpenOption;
    
    import java.management.AttributeChangeNotification;
    import java.management.Notification;
    import java.management.NotificationListener;
    
    import org.jboss.logging.Logger;
    
    public class StateNotificationListener implements NotificationListener {
    
      public static final String RUNTIME_CONFIGURATION_FILENAME = "runtime-configuration-notifications.txt";
      public static final String RUNNING_FILENAME = "running-notifications.txt";
      private final Path targetFile;
    
      public StateNotificationListener() {
        this.targetFile = Paths.get("notifications/data").toAbsolutePath();
        init(targetFile);
      }
    
      protected Path getRuntimeConfigurationTargetFile() {
        return this.targetFile.resolve(RUNTIME_CONFIGURATION_FILENAME);
      }
    
      protected Path getRunningConfigurationTargetFile() {
        return this.targetFile.resolve(RUNNING_FILENAME);
      }
    
      protected final void init(Path targetFile) {
        try {
          Files.createDirectories(targetFile);
    
          if (!Files.exists(targetFile.resolve(RUNTIME_CONFIGURATION_FILENAME))) {
            Files.createFile(targetFile.resolve(RUNTIME_CONFIGURATION_FILENAME));
          }
    
          if (!Files.exists(targetFile.resolve(RUNNING_FILENAME))) {
            Files.createFile(targetFile.resolve(RUNNING_FILENAME));
          }
        } catch (IOException ex) {
            Logger.getLogger(StateNotificationListener.class).error("Problem handling JMX Notification", ex);
        }
      }
    
      @Override
      public void handleNotification(Notification notification, Object handback) {
        AttributeChangeNotification attributeChangeNotification = (AttributeChangeNotification) notification;
        if ("RuntimeConfigurationState".equals(attributeChangeNotification.getAttributeName())) {
          writeNotification(attributeChangeNotification, getRuntimeConfigurationTargetFile());
        } else {
          writeNotification(attributeChangeNotification, getRunningConfigurationTargetFile());
        }
      }
    
      private void writeNotification(AttributeChangeNotification notification, Path path) {
        try (BufferedWriter in = Files.newBufferedWriter(path, StandardCharsets.UTF_8, StandardOpenOption.APPEND)) {
          in.write(String.format("%s %s %s %s", notification.getType(), notification.getSequenceNumber(), notification.getSource().toString(), notification.getMessage()));
          in.newLine();
          in.flush();
        } catch (IOException ex) {
          Logger.getLogger(StateNotificationListener.class).error("Problem handling JMX Notification", ex);
        }
      }
    }
    Copy to Clipboard Toggle word wrap

  2. 注册通知监听程序。

    将通知监听程序添加到 MBeanServer

    示例:添加通知监听程序

    MBeanServer server = ManagementFactory.getPlatformMBeanServer();
    server.addNotificationListener(ObjectName.getInstance("jboss.root:type=state"), new StateNotificationListener(), null, null);
    Copy to Clipboard Toggle word wrap

  3. 打包并部署到 JBoss EAP.

服务器生命周期事件现在根据上面的 StateNotificationListener 类记录到文件中。例如,在管理 CLI 中发出 :suspend 命令会输出到 running-notifications.txt 文件。

jmx.attribute.change 5 jboss.root:type=state The attribute 'RunningState' has changed from 'normal' to 'suspending'
jmx.attribute.change 6 jboss.root:type=state The attribute 'RunningState' has changed from 'suspending' to 'suspended'
Copy to Clipboard Toggle word wrap

这表明,running 状态从 normal 改为 suspending,然后从 挂起 挂起

返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2025 Red Hat