3.10. 服务器生命周期事件通知
您可以使用 JBoss EAP core-management 子系统 或 JMX 为服务器生命周期事件设置通知。服务器运行时配置状态或服务器运行状态的更改将触发通知。
JBoss EAP 的服务器运行时配置状态是 STARTING、RUNNING、RELOAD_REQUIRED、RESTART_REQUIRED、STOPPING 和 STOPPED。
运行 JBoss EAP 的服务器状态为 STARTING,NORMAL,ADMIN_ONLY,PRE_SUSPEND,SUSPENDING,SUSPENDED,STOPPING, 和 STOPPED。
3.10.1. 使用核心管理子系统监控服务器生命周期事件 复制链接链接已复制到粘贴板!
您可以将监听程序注册到 JBoss EAP core-management 子系统,以监控服务器生命周期事件。以下步骤演示了如何创建并注册将事件记录到文件中的示例监听程序。
先决条件
- JBoss EAP 正在运行。
流程
创建监听程序。
创建
org.wildfly.extension.core.management.client.ProcessStateListener的实现,如下例所示。示例:Listener 类
Copy to Clipboard Copied! Toggle word wrap Toggle overflow 注意在实施监听器时请注意以下几点:
- 如果服务器重新加载,侦听器会在服务器尝试停止时停止侦听,并在服务器启动时重新加载监听程序。因此,实施必须确保在同一 JVM 内正确加载、初始化和删除它们。
- 对监听器的通知阻止阻止对服务器状态的更改。实施必须确保它们不会阻止或死锁。
- 每个监听器实例都以自己的线程执行,且不会保证顺序。
编译类并将其打包为 JAR。
请注意,要编译,您需要依赖于
org.wildfly.core:wildfly-core-management-clientMaven 模块。将 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
module add --name=org.simple.lifecycle.events.listener --dependencies=org.wildfly.extension.core-management-client --resources=/path/to/simple-listener-0.0.1-SNAPSHOT.jarCopy to Clipboard Copied! Toggle word wrap Toggle overflow 重要使用
module管理 CLI 命令仅作为技术预览提供和删除模块。此命令不适合在受管域中使用,或者在远程连接到管理 CLI 时。在生产环境中应该手动 添加和删除 模块。技术预览功能不包括在红帽生产服务级别协议(SLA)中,且其功能可能并不完善。因此,红帽不建议在生产环境中使用它们。这些技术预览功能可以使用户提早试用新的功能,并有机会在开发阶段提供反馈意见。
如需有关技术预览功能支持范围的信息,请参阅红帽客户门户网站上的 技术预览功能支持范围。
注册侦听器。
使用以下管理 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})/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 Copied! Toggle word wrap Toggle overflow
现在,服务器生命周期事件将根据上面的 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
Running state change for STANDALONE_SERVER: normal to suspending
Running state change for STANDALONE_SERVER: suspending to suspended
这表明,running 状态从 normal 改为 suspending,然后从 挂起 挂起。
3.10.2. 使用 JMX 通知监控服务器生命周期事件 复制链接链接已复制到粘贴板!
您可以注册 JMX 通知监听程序,以监控服务器生命周期事件。以下步骤演示了如何创建并添加将事件记录到文件中的示例监听程序。
先决条件
- JBoss EAP 正在运行。
流程
创建监听程序。
创建
java.management.NotificationListener的实现,如下例所示。示例:Listener 类
Copy to Clipboard Copied! Toggle word wrap Toggle overflow 注册通知监听程序。
将通知监听程序添加到
MBeanServer。示例:添加通知监听程序
MBeanServer server = ManagementFactory.getPlatformMBeanServer(); server.addNotificationListener(ObjectName.getInstance("jboss.root:type=state"), new StateNotificationListener(), null, null);MBeanServer server = ManagementFactory.getPlatformMBeanServer(); server.addNotificationListener(ObjectName.getInstance("jboss.root:type=state"), new StateNotificationListener(), null, null);Copy to Clipboard Copied! Toggle word wrap Toggle overflow - 打包并部署到 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'
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'
这表明,running 状态从 normal 改为 suspending,然后从 挂起 挂起。