spring boot actuator connect jmx programmatically

随声附和 提交于 2019-12-06 14:40:13

It's probably easier to enable jolokia rather than using RMI; then you can simply

curl http://localhost:8080/actuator/jolokia/exec/org.springframework.boot:type=Admin,name=SpringApplication/shutdown

EDIT

If you prefer to use RMI, refer to the Spring Framework JMX Documentation.

Server app:

@SpringBootApplication
public class So50392589Application {

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

    @Bean
    public RmiRegistryFactoryBean rmi() {
        RmiRegistryFactoryBean rmi = new RmiRegistryFactoryBean();
        rmi.setPort(1099);
        return rmi;
    }

    @Bean
    public ConnectorServerFactoryBean server() throws Exception {
        ConnectorServerFactoryBean fb = new ConnectorServerFactoryBean();
        fb.setObjectName("connector:name=rmi");
        fb.setServiceUrl("service:jmx:rmi://localhost/jndi/rmi://localhost:1099/myconnector");
        return fb;
    }

}

Client app:

@SpringBootApplication
public class JmxClient {

    public static void main(String[] args) {
        new SpringApplicationBuilder(JmxClient.class)
            .web(WebApplicationType.NONE)
            .run(args);
    }

    @Bean
    public ApplicationRunner runner(MBeanServerConnection jmxConnector) {
        return args -> {
            jmxConnector.invoke(new ObjectName("org.springframework.boot:type=Admin,name=SpringApplication"),
                    "shutdown", new Object[0], new String[0]);
        };
    }

    @Bean
    public MBeanServerConnectionFactoryBean jmxConnector() throws Exception {
        MBeanServerConnectionFactoryBean jmx = new MBeanServerConnectionFactoryBean();
        jmx.setServiceUrl("service:jmx:rmi://localhost/jndi/rmi://localhost:1099/myconnector");
        return jmx;
    }

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!