rayman
rayman

Reputation: 21596

Configure and connect to Spring JMX

I am trying to configure JMX console for my standalone Spring application.

I have configured it this way:

Application context:

<?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>

And a Trigger bean which I want it's public method to be exposed in the 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. Is that enough for configuration?
  2. Now to which local address:port should I connect in order to access the console?

thanks, ray

Upvotes: 0

Views: 1688

Answers (1)

Brian Agnew
Brian Agnew

Reputation: 272277

You have the JMX server, but you need an HTMLAdapter to view these beans via a browser. e.g. from 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>

Note that I'm assuming HTML/browser access here. See here for an Oracle tutorial with an adapter configured in code.

Upvotes: 1

Related Questions