配置并连接到Spring JMX

时间:2012-07-02 09:07:49

标签: java spring jmx

我正在尝试为我的独立Spring应用程序配置JMX控制台。

我已经这样配置了:

申请背景:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"


    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

    <!-- Must for auto wiring 
    <context:annotation-config />
    -->

    <context:component-scan base-package="com.fixgw.beans">
    </context:component-scan>


    <bean id="FeedListenerBean" class="com.fixgw.beans.FeedListenerBean">
    </bean>

    <bean id="TriggerBean" class="com.fixgw.test.TriggerBean">
    </bean>


    <bean id="mbeanServer" class="org.springframework.jmx.support.MBeanServerFactoryBean" >
    <property name="locateExistingServerIfPossible" value="true" />

    </bean>


    <bean id="exporter" class="org.springframework.jmx.export.MBeanExporter">
        <property name="beans">
            <map>
                <entry key="bean:name=TriggerBean1" value-ref="TriggerBean" />
            </map>
        </property>
        <property name="server" ref="mbeanServer" />
    </bean>
</beans>

还有一个Trigger bean,我希望它的公共方法在JMX中公开:

public class TriggerBean implements IJmxTriggerBean
{
    static Logger logger = Logger.getLogger(TriggerBean.class);

    FeedListenerBean fe = null;

    public void start()
    {
        try
        {
            // init();
            PropertyConfigurator.configure(FixGWConstants.LOG4J_PATH);
            ApplicationContext context = new ClassPathXmlApplicationContext(FixGWConstants.APPLICATION_CONTEXT_XML);
            fe = (FeedListenerBean) context.getBean("FeedListenerBean");
            Thread t=new Thread()
            {
                public void run()
                {
                    while (true)
                    {
                        System.out.println("a");
                        try
                        {
                            Thread.sleep(1000);
                        }
                        catch (InterruptedException e)
                        {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }
            };
            t.run();
            doTheListen();

        }
        catch (Throwable t)
        {
            logger.error(t);
            System.out.println(t);
        }

    }

    public void doTheListen()
    {
        fe.listen();
    }

}


package com.finbird.fixgw.test;

public interface IJmxTriggerBean
{
    public void doTheListen();
}
  1. 这足以配置吗?
  2. 现在我应该连接哪个本地地址:端口才能访问控制台?
  3. 感谢, 射线

1 个答案:

答案 0 :(得分:1)

您拥有JMX服务器,但您需要一个HTMLAdapter才能通过浏览器查看这些bean。例如来自this article

  <bean id="htmlAdaptor" class="com.sun.jdmk.comm.HtmlAdaptorServer" init-method="start" />

  <bean id="exporter" class="org.springframework.jmx.export.MBeanExporter">
    <property name="beans">
     <map>
       <entry key="adaptor:name=htmlAdaptor" value-ref="htmlAdaptor" />
       <entry key="bean:name=calculatorConfigBean" value-ref="calculatorConfig" />
     </map>
    </property>
    <property name="server" ref="mbeanServer" />
  </bean>

请注意,我在这里假设HTML /浏览器访问。有关使用代码配置的适配器的Oracle教程,请参阅here