Search code examples
springjmxtrans

Embedded JMXTrans Spring Configuration


I wanted to try out basic getting started example with embedded-jmxtrans. So I add the below code

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jmxtrans="http://www.jmxtrans.org/schema/embedded"
       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.jmxtrans.org/schema/embedded http://www.jmxtrans.org/schema/embedded/jmxtrans-1.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:annotation-config/>

    <jmxtrans:jmxtrans>
        <jmxtrans:configuration>classpath:jvmmonitoring/jmxtrans/jmxtrans.json</jmxtrans:configuration>
        <jmxtrans:configuration>classpath:org/jmxtrans/embedded/config/tomcat-6.json</jmxtrans:configuration>
        <jmxtrans:configuration>classpath:org/jmxtrans/embedded/config/jmxtrans-internals.json</jmxtrans:configuration>
        <jmxtrans:configuration>classpath:org/jmxtrans/embedded/config/jvm-sun-hotspot.json</jmxtrans:configuration>
    </jmxtrans:jmxtrans>


</beans>

public class EmbeddedJMXTrans {
    public static void main(String[] args) {
        System.setProperty("spring.profiles.active", "dev");
        System.setProperty("spring.profiles.default", "dev");
        System.setProperty("spring.liveBeansView.mbeanDomain", "dev");
        ApplicationContext context = new ClassPathXmlApplicationContext("jvmmonitoring/jmxtrans/spring-beans.xml");
        while (true) {

        }
    }
}

The while loop was added to keep the application up till the jvm statistics are printed on console. Here is the jmxtrans.json file

{
    "queries": [
        {
            "objectName": "java.lang:type=Memory",
            "resultAlias": "jvm.memory",
            "attributes": [
                {
                    "name": "HeapMemoryUsage",
                    "keys": ["committed", "used"]
                },
                {
                    "name": "NonHeapMemoryUsage",
                    "keys": ["committed", "used"]
                }
            ]

        },
        {
            "objectName": "java.lang:type=Runtime",
            "resultAlias": "jvm.runtime",
            "attributes": [
                "Uptime"
            ]

        },
        {
            "objectName": "java.lang:type=GarbageCollector,name=*",
            "resultAlias": "jvm.gc.%name%",
            "attributes": [
                "CollectionCount",
                "CollectionTime"
            ]
        },
        {
            "objectName": "java.lang:type=Threading",
            "resultAlias": "jvm.thread",
            "attributes": [
                "ThreadCount"
            ]

        },
        {
            "objectName": "java.lang:type=OperatingSystem",
            "resultAlias": "jvm.os",
            "attributes": [
                "CommittedVirtualMemorySize",
                "FreePhysicalMemorySize",
                "FreeSwapSpaceSize",
                "OpenFileDescriptorCount",
                "ProcessCpuTime",
                "SystemLoadAverage"
            ]

        }
    ],
    "outputWriters": [
        {
          "@class": "org.jmxtrans.embedded.output.ConsoleWriter"
        }
    ]
}

On enabling debug level logs I found that jmxtrans spring bean wasn't created because of a spring circular reference error

DEBUG o.s.b.f.s.DefaultListableBeanFactory 1426 - Ignoring bean creation exception on FactoryBean type check: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'jmxtrans': Requested bean is currently in creation: Is there an unresolvable circular reference?

The complete spring logs are shared here - http://pastebin.com/p2VeNEzE

What could be missing?

Thanks!


Solution

  • The above debug log message about circular reference was misguiding. The issue got resolved after I fetched jmxtrans bean from application context and invoked the start() method.