美文网首页
Springboot MBean使用

Springboot MBean使用

作者: SparkOnly | 来源:发表于2021-04-01 15:52 被阅读0次

    MBean,managed bean,被管理的bean,也就是一个被管理的Java对象。
    它暴露了一个管理接口,可以包含以下内容:

    • 一系列可读或可写的属性
    • 一系列可调用的操作
    • 自我描述

    SpringApplicationAdminMXBean

    Springboot里默认有暴露出SpringApplicationAdminMXBean,通过源码可以看出,是在afterPropertiesSet方法注册了mbean,destroy方法取消注册

    @Override
    public void afterPropertiesSet() throws Exception {
        MBeanServer server = ManagementFactory.getPlatformMBeanServer();
        server.registerMBean(new SpringApplicationAdmin(), this.objectName);
        if (logger.isDebugEnabled()) {
            logger.debug("Application Admin MBean registered with name '" + this.objectName + "'");
        }
    }
    
    @Override
    public void destroy() throws Exception {
        ManagementFactory.getPlatformMBeanServer().unregisterMBean(this.objectName);
    }
    

    该bean本身暴露了两个属性(Ready, EmbeddedWebApplication),两个方法(getProperty, shutdown)

    private class SpringApplicationAdmin implements SpringApplicationAdminMXBean {
    
            @Override
            public boolean isReady() {
                return SpringApplicationAdminMXBeanRegistrar.this.ready;
            }
    
            @Override
            public boolean isEmbeddedWebApplication() {
                return SpringApplicationAdminMXBeanRegistrar.this.embeddedWebApplication;
            }
    
            @Override
            public String getProperty(String key) {
                return SpringApplicationAdminMXBeanRegistrar.this.environment.getProperty(key);
            }
    
            @Override
            public void shutdown() {
                logger.info("Application shutdown requested.");
                SpringApplicationAdminMXBeanRegistrar.this.applicationContext.close();
            }
    
        }
    

    通过jvisualvm,可以看到具体mbean的信息


    jvisualvm-Admin

    自己实现

    我们自己也可以仿照Admin类,自己实现一个MBean
    代码如下,定义了一个MBean: cn.ye:type=Hello

    public interface HelloMBean {
    
        public void sayHello();
    
        public int add(int x, int y);
    
        public String getName();
    
        public int getCacheSize();
    
        public void setCacheSize(int size);
    }
    
    public class Hello implements HelloMBean {
    
        private final String name = "Reginald";
        private int cacheSize = DEFAULT_CACHE_SIZE;
        private static final int DEFAULT_CACHE_SIZE = 200;
    
        @Override
        public void sayHello() {
            System.out.println("hello, world");
        }
    
        @Override
        public int add(int x, int y) {
            return x + y;
        }
    
        @Override
        public String getName() {
            return this.name;
        }
    
        @Override
        public int getCacheSize() {
            return this.cacheSize;
        }
    
        @Override
        public synchronized void setCacheSize(int size) {
            this.cacheSize = size;
            System.out.println("Cache size now " + this.cacheSize);
        }
    }
    

    启动类里,注册MBean

    public class Application {
    
        public static void main(String[] args) throws MalformedObjectNameException, NotCompliantMBeanException, InstanceAlreadyExistsException, MBeanRegistrationException {
            SpringApplication.run(Application.class, args);
            MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
            ObjectName name = new ObjectName("cn.ye:type=Hello");
            Hello mbean = new Hello();
            mbs.registerMBean(mbean, name);
        }
    }
    

    启动jvisualvm,可以发现mbean已经注册上去了

    其中CacheSize可修改,Name只读


    Hello

    操作里,也有两个方法,可以正常调用


    Hello-Operations

    相关文章

      网友评论

          本文标题:Springboot MBean使用

          本文链接:https://www.haomeiwen.com/subject/ivmtkltx.html