30 September 2014

Spring Boot provides monitoring and management over JMX out of the box. If you include a library that exposes MBeans, they will automatically be registered and exposed with the MBean server provided by Spring Boot. However, some packages don’t enable their MBeans by default. Jetty is one of those libraries. If you are using Jetty instead of Tomcat (Tomcat is the default option in Spring Boot), you can enable Jetty’s MBeans by adding a customizer to a custom override of the JettyEmbeddedServletContainerFactory bean:

@Bean
public JettyEmbeddedServletContainerFactory jettyEmbeddedServletContainerFactory(@Value("${server.port:8080}") final String port) {
    final JettyEmbeddedServletContainerFactory factory =  new JettyEmbeddedServletContainerFactory(Integer.valueOf(port));
    factory.addServerCustomizers(new JettyServerCustomizer() {
        @Override
        public void customize(final Server server) {
            // Expose Jetty managed beans to the JMX platform server provided by Spring
            final MBeanContainer mbContainer = new MBeanContainer(ManagementFactory.getPlatformMBeanServer());
            server.addBean(mbContainer);
        }
    });
    return factory;
}

The code sample above will register Jetty’s MBeans with the default platform MBean server (which is provided by Spring Boot). This will allow you to see things like the number of threads in use in Jetty’s connection pool and other fun facts that can be very useful when debugging Jetty or configuring it for better performance. It is also worth noting that you will need to include the following dependencies in order to expose the Jetty MBeans:

ext {
    jettyVersion = '9.0.3.v20130506'
}

dependencies {
  ...

  compile "org.eclipse.jetty:jetty-jmx:$jettyVersion"
  compile "org.eclipse.jetty:jetty-server:$jettyVersion"
  compile "org.eclipse.jetty:jetty-util:$jettyVersion"

  ...
}

comments powered by Disqus