zoukankan      html  css  js  c++  java
  • springboot自定义jmx对象

    在使用springboot-admin对springboot项目进行监控的时候我们发现其是具有web访问jmx对象的功能的,那它内部是怎么实现的呢。

    Jolokia是一个JMX-http桥梁,它提供了访问JMX bean的HTTP访问方式。

    <dependency>
        <groupId>org.jolokia</groupId>
        <artifactId>jolokia-core</artifactId>
    </dependency>

    什么情况我们需要使用JMX? 我认为比较实用有如下2点:

    1、获取java对象里的属性的实时情况。

    2、动态修改对象里的属性的值。

    例如:你有一个耗时较长的定时任务,里面会处理一批数据,这时通过jmx暴露当前已处理的数据的相关数据就能得到实时的结果(当然,你可以通过写日志、数据库、缓存来实现,但这无疑增加了更业务无关的代码)。

    那要怎么做呢?

    首先看一下相关注解定义

    将类的所有实例标识为JMX受控资源 ManagedResource @ManagedResource Class 类
    将方法标识为JMX操作 ManagedOperation @ManagedOperation  Method方法
    将getter或者setter标识为部分JMX属性 ManagedAttribute @ManagedAttribute Method (only getters and setters) 方法(仅getters和setters)
    定义操作参数说明 ManagedOperationParameter @ManagedOperationParameter@ManagedOperationParameters Method 方法

    例子:

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.jmx.export.annotation.ManagedAttribute;
    import org.springframework.jmx.export.annotation.ManagedResource;
    import lombok.extern.slf4j.Slf4j;
    
    @Service
    @Slf4j
    @ManagedResource (objectName= "com.longge:name=spideMpbServiceImpl" , description= "brower spider service" )
    public class SpideMpbServiceImpl implements SpideMpbService {
        // 临时表当前最大id
        private Long tempMaxId = 0L;
         
        /**
         * 暴露mbean方法
         * @return
         */
        @ManagedAttribute(description="temp info now max id")
        public Long getNowTempMaxId() {
            return tempMaxId;
        }
    }

     在JMC的Mbean选项卡、springboot-admin的jmx就能看到这属性和这方法

  • 相关阅读:
    Response生成注册验证码实现例子02
    Mysql 自增字段起始值auto_increment的修改方法
    elite核心库的加载方式及自动加载类库
    elite核心类库之事件类
    wamp速度缓慢的解决办法
    Dwzdialog中批量提交的问题处理
    PHP中缀表达式与逆波兰式的计算(用于工资项目等四则计算)
    PHP工资计算之逆波兰式
    elite核心类库之模板类
    PHP soap访问接口出错汇总及解决办法
  • 原文地址:https://www.cnblogs.com/yangzhilong/p/10794795.html
Copyright © 2011-2022 走看看