How to configure JMX with Spring Boot
Asked Answered
B

2

10

I have created a Spring Integration application with Spring Boot. I would like to know how to configure JMX with Spring Boot. I believe by default JMX is configured when using Spring Boot Actuator.

Do I need to configure anything else to be able to export MBeans for Spring Integration?

Most of the example I see have the following line in the applicationContext.xml

<context:mbean-export/>
<context:mbean-server/>

My Application.java class looks like this.

package com.jbhunt.app.consumerappointmentintegration;

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.ImportResource;

    @Configuration
    @ComponentScan
    @EnableAutoConfiguration
    @ImportResource("classpath:META-INF/spring/integration/spring-integration-context.xml")
    public class Application {

        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }

Adding this line to the configuration doesn't seem to export the Spring Integration mbeans

@EnableIntegrationMBeanExport(server = "mbeanServer",  defaultDomain="my.company.domain")

I'm referencing this video https://www.youtube.com/watch?v=TetfR7ULnA8

Ber answered 26/8, 2014 at 3:5 Comment(0)
C
21

As you understand the Spring Integration JMX is enabled by default, if you just have spring-integration-jmx in the classpath. And, of course, if spring.jmx.enabled = true (default).

You can't overrride that just declaring one more @EnableIntegrationMBeanExport, because it is based on @Import and you just can't override import classes because of (from ConfigurationClassParser):

imports.addAll(sourceClass.getAnnotationAttributes(Import.class.getName(), "value"));

If imported classes are already there, they aren't overridable.

You have several choices to achieve your requirements:

  1. Disable default Spring Boot JMX - just addind to the application.properties spring.jmx.enabled = false and continue to use @EnableIntegrationMBeanExport

  2. Configure IntegrationMBeanExporter @Bean manually.

  3. Just configure your my.company.domain domain in the application.properties:

    spring.jmx.default_domain = my.company.domain
    
Coff answered 26/8, 2014 at 4:17 Comment(7)
Adding spring-integration-jmx to the classpath did the trick. Why wouldn't this be include in spring-boot-starter-integration? Thanks for the help again!!Ber
Because in most cases people don't want to have JMX in their apps. Of course, it might be a good point to discuss. Feel free to raise an GH issue: github.com/spring-projects/spring-boot/issues. Mention me, please, there - @artembilanCoff
Issue has been created github.com/spring-projects/spring-boot/issues/1440Ber
Thanks. No, I'm not planning, but +1 to my answer would be great ;-)Coff
What exactly is meant by "add spring-integration-jmx to the classpath"? And is that just for a custom jmx mbean server / exporter?Lavenialaver
Sorry, looks like very broad subject, so requires a separate SO thread.Coff
How to define a dynamic random value to jmx.default-domain in the use case of hosting multiple applications in a single tomcat service?Abrahan
T
5

It is quite late to add this; but in addition to the endpoints.jmx.domain I found it useful to change the spring.jmx.default-domain to someting unique per application

This is with multiple instances of Spring Boot 1.4.1 apps running in Tomcat 7

Typescript answered 17/10, 2016 at 10:47 Comment(1)
There is a discussion ongoing regarding this issue: github.com/spring-projects/spring-boot/issues/5911 Hope, Spring devs fixes thisBarrie

© 2022 - 2024 — McMap. All rights reserved.