gpt4 book ai didi

java - 注册到 mbean 服务器的 Mbeans 没有出现在 jconsole 中

转载 作者:搜寻专家 更新时间:2023-10-31 20:22:58 26 4
gpt4 key购买 nike

我使用 MBeanServerFactory.createMBeanServer 创建一个 mbean 服务器并向其注册 mbean。我可以在 jconsole 中找到 mbean 服务器,但是当我连接到它时,我看不到已注册的 mbean。这是代码:

public static void main(String[] args) throws Exception
{
MBeanServer mbeanServer = MBeanServerFactory.createMBeanServer("example");
ObjectName objectName = new ObjectName("example:type=simpleMbean");
Simple simple = new Simple (1, 0);
mbeanServer.registerMBean(simple, objectName);
while (true)
{
}
}

如果我使用 platformMBeanServer 并将我的 mbean 注册到它,而不是创建 mbean 服务器,我可以在 jconsole 中看到 mbean。知道在执行 createMBeanServer 时我还需要做什么吗?

最佳答案

我昨天遇到了这个问题,但设法解决了这个问题。我花了一些时间这样做,所以我认为这篇文章有助于节省其他人的时间。

首先,您可以按照帖子中的主要方法在 Java 代码中注册 bean。但我认为如果使用 Spring 会简单得多。

查看此链接了解更多信息; http://static.springsource.org/spring/docs/2.0.x/reference/jmx.html

您需要避免一些循环漏洞。不要在您的应用程序中启动两个 MBean 服务器。

我使用了这个配置文件:

<bean id="mbeanExporter" class="org.springframework.jmx.export.MBeanExporter" lazy-init="false">
<property name="beans">
<map>
<entry key="bean:name=beanName" value-ref="dataSource"/>
</map>
</property>
<property name="registrationBehaviorName" value="REGISTRATION_REPLACE_EXISTING"/>
</bean>

使用此配置将 bean 名称附加到 MBeanExporter。确保 'lazy-init' 设置为 false。请注意我在网络应用程序中使用此配置。 Web应用程序部署在tomcat内部。所以tomcat已经有了MBean server。因此您无需明确提供。如果您以独立模式运行它,则需要启动 MBean 服务器并进行相应配置。

另请注意,您需要在 tomcat 的 catalina.bat 文件中添加以下属性。 设置 CATALINA_OPTS=-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=8088 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false -Dcom. sun.management.jmxremote.hostname="localhost"

本例中的 Jmx 连接器端口是 8088,主机名是“localhost”启动 tomcat 后,您需要启动 jconsole(我不会在这里告诉您如何启动它)然后单击“RemoteProcess”并键入“localhost:8088”并连接到 tomcat 的 MBean 服务器。然后转到 jconsole 中的 MBean 选项卡,您应该会在那里看到您的 MBean。

关于java - 注册到 mbean 服务器的 Mbeans 没有出现在 jconsole 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7424009/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com