欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot JMX的基本使用方式

 更新時間:2021年09月10日 15:55:09   作者:你是小KS  
這篇文章主要介紹了SpringBoot JMX的基本使用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

SpringBoot JMX的基本使用

1. 聲明

當前內容主要為學習和使用SpringBoot注冊JMX的操作,主要方便管理需要的類

當前內容來源:SpringBoot官方文檔

主要內容為:

  • 使用SpringBoot注冊JMX中的MBean
  • 使用jconsole查看和修改屬性

基本的pom依賴

<parent>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-parent</artifactId>
 <version>2.2.13.RELEASE</version>
</parent>
<properties>
 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
 <dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
 </dependency>
</dependencies>

2. 基本demo

application.properties的內容

spring.jmx.enabled=true

mysqldb.properties的內容

jdbc.driverClassName=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=123456
# mysql connector timeout check 
jdbc.maxIdle=216000
jdbc.validationQuery=select 1
jdbc.validationQueryTimeout=1800
jdbc.testOnBorrow=true
jdbc.testWhileIdle=true

配置類AppConfig

@Configuration
@PropertySource(value = {"mysqldb.properties"})
@EnableConfigurationProperties(value = { MySQLDBProperties.class})
public class AppConfig {
}

MySQLDBProperties

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.jmx.export.annotation.ManagedAttribute;
import org.springframework.jmx.export.annotation.ManagedResource;
/**
 * @description 當前內容主要為對應SQLServerDB的數(shù)據庫配置文件中的屬性
 * @author hy
 * @createTime 2021-03-31 13:26:36
 **/
@ConfigurationProperties(prefix = "jdbc")
@ManagedResource("com.hy.springboot.jmx.test.properties:type=MySQLDBProperties,name=MySQLDBProperties")
public class MySQLDBProperties {
	private String url;
	private String driverClassName;
	private String username;
	private String password;
	private Integer maxIdle;
	private Integer validationQueryTimeout;
	private String validationQuery;
	private Boolean testOnBorrow; // 是否在使用的時候進行檢查操作
	private Boolean testWhileIdle;// 測試是否已經不能使用了
	@ManagedAttribute
	public Boolean getTestOnBorrow() {
		return testOnBorrow;
	}
	@ManagedAttribute
	public void setTestOnBorrow(Boolean testOnBorrow) {
		this.testOnBorrow = testOnBorrow;
	}
	@ManagedAttribute
	public Boolean getTestWhileIdle() {
		return testWhileIdle;
	}
	@ManagedAttribute
	public void setTestWhileIdle(Boolean testWhileIdle) {
		this.testWhileIdle = testWhileIdle;
	}
	@ManagedAttribute
	public Integer getValidationQueryTimeout() {
		return validationQueryTimeout;
	}
	@ManagedAttribute
	public void setValidationQueryTimeout(Integer validationQueryTimeout) {
		this.validationQueryTimeout = validationQueryTimeout;
	}
	@ManagedAttribute
	public String getValidationQuery() {
		return validationQuery;
	}
	@ManagedAttribute
	public void setValidationQuery(String validationQuery) {
		this.validationQuery = validationQuery;
	}
	@ManagedAttribute
	public Integer getMaxIdle() {
		return maxIdle;
	}
	@ManagedAttribute
	public void setMaxIdle(Integer maxIdle) {
		this.maxIdle = maxIdle;
	}
	@ManagedAttribute
	public String getUrl() {
		return url;
	}
	@ManagedAttribute
	public void setUrl(String url) {
		this.url = url;
	}
	@ManagedAttribute
	public String getDriverClassName() {
		return driverClassName;
	}
	@ManagedAttribute
	public void setDriverClassName(String driverClassName) {
		this.driverClassName = driverClassName;
	}
	@ManagedAttribute
	public String getUsername() {
		return username;
	}
	@ManagedAttribute
	public void setUsername(String username) {
		this.username = username;
	}
	@ManagedAttribute
	public String getPassword() {
		return password;
	}
	@ManagedAttribute
	public void setPassword(String password) {
		System.out.println("設置新的密碼為:" + password);
		this.password = password;
	}
}

主要借助:@ManagedAttribute和@ManagedResource來實現(xiàn)操作

入口類:基本的main方法

3. 執(zhí)行結果

在這里插入圖片描述

使用jconsole連接并查看MBean結果

在這里插入圖片描述 在這里插入圖片描述

使用JMX可將一些需要的信息注冊,然后通過jconsole動態(tài)查看運行中的屬性,也可以修改屬性

springboot自定義jmx對象

在使用springboot-admin對springboot項目進行監(jiān)控的時候我們發(fā)現(xiàn)其是具有web訪問jmx對象的功能的,那它內部是怎么實現(xiàn)的呢。

Jolokia是一個JMX-http橋梁,它提供了訪問JMX bean的HTTP訪問方式。

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

什么情況我們需要使用JMX?

我認為比較實用有如下2點:

1、獲取java對象里的屬性的實時情況。

2、動態(tài)修改對象里的屬性的值。

例如:你有一個耗時較長的定時任務,里面會處理一批數(shù)據,這時通過jmx暴露當前已處理的數(shù)據的相關數(shù)據就能得到實時的結果(當然,你可以通過寫日志、數(shù)據庫、緩存來實現(xiàn),但這無疑增加了更業(yè)務無關的代碼)。

那要怎么做呢?

首先看一下相關注解定義

將類的所有實例標識為JMX受控資源 ManagedResource @ManagedResource Class 類
將方法標識為JMX操作 ManagedOperation @ManagedOperation  Method方法
將getter或者setter標識為部分JMX屬性 ManagedAttribute @ManagedAttribute Method (only getters and setters) 方法(僅getters和setters)
定義操作參數(shù)說明 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就能看到這屬性和這方法。

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • java使用文件流實現(xiàn)查看下載次數(shù)

    java使用文件流實現(xiàn)查看下載次數(shù)

    這篇文章主要為大家詳細介紹了java使用文件流實現(xiàn)查看下載次數(shù),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-07-07
  • Java如何將任意類型的Object對象轉換為相應的實體對象

    Java如何將任意類型的Object對象轉換為相應的實體對象

    這篇文章主要介紹了Java如何將任意類型的Object對象轉換為相應的實體對象問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • MyBatis分頁查詢與特殊字符處理方式

    MyBatis分頁查詢與特殊字符處理方式

    這篇文章主要介紹了MyBatis分頁查詢與特殊字符處理方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • java 多線程Thread與runnable的區(qū)別

    java 多線程Thread與runnable的區(qū)別

    這篇文章主要介紹了java 多線程Thread與runnable的區(qū)別的相關資料,java線程有兩種方法繼承thread類與實現(xiàn)runnable接口,下面就提供實例幫助大家理解,需要的朋友可以參考下
    2017-08-08
  • Java 將Excel轉為SVG的方法

    Java 將Excel轉為SVG的方法

    本文以Java示例展示如何將Excel文檔轉為SVG格式。通過本文中的方法,在將Excel轉為SVG時,如果sheet工作表中手動設置了分頁,則將每個分頁的內容單獨保存為一個svg文件,如果sheet工作表中沒有設置分頁,則將Excel sheet表格中默認的分頁范圍保存為svg。
    2021-05-05
  • SpringBoot?Aop實現(xiàn)接口請求次數(shù)統(tǒng)計

    SpringBoot?Aop實現(xiàn)接口請求次數(shù)統(tǒng)計

    我們通過Spring AOP在每次執(zhí)行方法前或執(zhí)行方法后進行切面的處理,進而統(tǒng)計方法訪問的次數(shù)等功能,本文主要介紹了SpringBoot?Aop實現(xiàn)接口請求次數(shù)統(tǒng)計
    2024-02-02
  • Java面試題之MD5加密的安全性詳解

    Java面試題之MD5加密的安全性詳解

    MD5 是 Message Digest Algorithm 的縮寫,譯為信息摘要算法,它是 Java 語言中使用很廣泛的一種加密算法。本文將通過示例討論下MD5的安全性,感興趣的可以了解一下
    2022-10-10
  • java操作XML實例代碼

    java操作XML實例代碼

    這篇文章主要介紹了java操作XML實例代碼,有需要的朋友可以參考一下
    2014-01-01
  • Spring MVC 啟動過程源碼分析詳解

    Spring MVC 啟動過程源碼分析詳解

    這篇文章主要介紹了Spring MVC 啟動過程源碼分析詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-07-07
  • SpringMVC的REST風格的四種請求方式總結

    SpringMVC的REST風格的四種請求方式總結

    下面小編就為大家?guī)硪黄猄pringMVC的REST風格的四種請求方式總結。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-08-08

最新評論