mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
add method
This commit is contained in:
parent
99fc30ec90
commit
30f541f849
@ -7,6 +7,7 @@
|
||||
|
||||
### 🐣新特性
|
||||
* 【system 】 OshiUtil增加getCurrentProcess方法
|
||||
* 【extra 】 SpringUtil增加getApplicationName、publishEvent方法(issue#I485NZ@Gitee)
|
||||
|
||||
### 🐞Bug修复
|
||||
|
||||
|
@ -10,6 +10,7 @@ import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.springframework.beans.factory.support.DefaultSingletonBeanRegistry;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.stereotype.Component;
|
||||
@ -77,10 +78,10 @@ public class SpringUtil implements BeanFactoryPostProcessor, ApplicationContextA
|
||||
* 获取{@link ConfigurableListableBeanFactory}
|
||||
*
|
||||
* @return {@link ConfigurableListableBeanFactory}
|
||||
* @since 5.7.7
|
||||
* @throws UtilException 当上下文非ConfigurableListableBeanFactory抛出异常
|
||||
* @since 5.7.7
|
||||
*/
|
||||
public static ConfigurableListableBeanFactory getConfigurableBeanFactory() throws UtilException{
|
||||
public static ConfigurableListableBeanFactory getConfigurableBeanFactory() throws UtilException {
|
||||
final ConfigurableListableBeanFactory factory;
|
||||
if (null != beanFactory) {
|
||||
factory = beanFactory;
|
||||
@ -177,12 +178,22 @@ public class SpringUtil implements BeanFactoryPostProcessor, ApplicationContextA
|
||||
* @since 5.3.3
|
||||
*/
|
||||
public static String getProperty(String key) {
|
||||
if(null == applicationContext){
|
||||
if (null == applicationContext) {
|
||||
return null;
|
||||
}
|
||||
return applicationContext.getEnvironment().getProperty(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取应用程序名称
|
||||
*
|
||||
* @return 应用程序名称
|
||||
* @since 5.7.12
|
||||
*/
|
||||
public static String getApplicationName() {
|
||||
return getProperty("spring.application.name");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前的环境配置,无配置返回null
|
||||
*
|
||||
@ -190,7 +201,7 @@ public class SpringUtil implements BeanFactoryPostProcessor, ApplicationContextA
|
||||
* @since 5.3.3
|
||||
*/
|
||||
public static String[] getActiveProfiles() {
|
||||
if(null == applicationContext){
|
||||
if (null == applicationContext) {
|
||||
return null;
|
||||
}
|
||||
return applicationContext.getEnvironment().getActiveProfiles();
|
||||
@ -237,13 +248,25 @@ public class SpringUtil implements BeanFactoryPostProcessor, ApplicationContextA
|
||||
*/
|
||||
public static void unregisterBean(String beanName) {
|
||||
final ConfigurableListableBeanFactory factory = getConfigurableBeanFactory();
|
||||
if(factory instanceof DefaultSingletonBeanRegistry){
|
||||
if (factory instanceof DefaultSingletonBeanRegistry) {
|
||||
DefaultSingletonBeanRegistry registry = (DefaultSingletonBeanRegistry) factory;
|
||||
registry.destroySingleton(beanName);
|
||||
} else {
|
||||
throw new UtilException("Can not unregister bean, the factory is not a DefaultSingletonBeanRegistry!");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 发布事件
|
||||
*
|
||||
* @param event the event to publish
|
||||
* @since 5.7.12
|
||||
*/
|
||||
public static void publishEvent(ApplicationEvent event) {
|
||||
if (null != applicationContext) {
|
||||
applicationContext.publishEvent(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -1,7 +1,6 @@
|
||||
package cn.hutool.system.oshi;
|
||||
|
||||
import oshi.hardware.CentralProcessor;
|
||||
import oshi.util.Util;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
|
||||
@ -49,11 +48,19 @@ public class CpuInfo {
|
||||
*/
|
||||
private String cpuModel;
|
||||
|
||||
/**
|
||||
* CPU型号信息
|
||||
*/
|
||||
private CpuTicks ticks;
|
||||
|
||||
/**
|
||||
* 空构造
|
||||
*/
|
||||
public CpuInfo() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造
|
||||
* 构造,等待时间为用于计算在一定时长内的CPU负载情况,如传入1000表示最近1秒的负载情况
|
||||
*
|
||||
* @param processor {@link CentralProcessor}
|
||||
* @param waitingTime 设置等待时间,单位毫秒
|
||||
@ -139,9 +146,16 @@ public class CpuInfo {
|
||||
this.cpuModel = cpuModel;
|
||||
}
|
||||
|
||||
public CpuTicks getTicks() {
|
||||
return ticks;
|
||||
}
|
||||
|
||||
public void setTicks(CpuTicks ticks) {
|
||||
this.ticks = ticks;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
DecimalFormat format = new DecimalFormat("#.00");
|
||||
return "CpuInfo{" +
|
||||
"cpu核心数=" + cpuNum +
|
||||
", CPU总的使用率=" + toTal +
|
||||
@ -149,7 +163,7 @@ public class CpuInfo {
|
||||
", CPU用户使用率=" + used +
|
||||
", CPU当前等待率=" + wait +
|
||||
", CPU当前空闲率=" + free +
|
||||
", CPU利用率=" + Double.parseDouble(format.format((100 - getFree()))) +
|
||||
", CPU利用率=" + LOAD_FORMAT.format(100 - free) +
|
||||
", CPU型号信息='" + cpuModel + '\'' +
|
||||
'}';
|
||||
}
|
||||
@ -162,43 +176,18 @@ public class CpuInfo {
|
||||
* @since 5.7.12
|
||||
*/
|
||||
private void init(CentralProcessor processor, long waitingTime) {
|
||||
// CPU信息
|
||||
final long[] prevTicks = processor.getSystemCpuLoadTicks();
|
||||
// 这里必须要设置延迟
|
||||
Util.sleep(waitingTime);
|
||||
final long[] ticks = processor.getSystemCpuLoadTicks();
|
||||
final long nice = tick(prevTicks, ticks, CentralProcessor.TickType.NICE);
|
||||
final long irq = tick(prevTicks, ticks, CentralProcessor.TickType.IRQ);
|
||||
final long softIrq = tick(prevTicks, ticks, CentralProcessor.TickType.SOFTIRQ);
|
||||
final long steal = tick(prevTicks, ticks, CentralProcessor.TickType.STEAL);
|
||||
final long cSys = tick(prevTicks, ticks, CentralProcessor.TickType.SYSTEM);
|
||||
final long user = tick(prevTicks, ticks, CentralProcessor.TickType.USER);
|
||||
final long ioWait = tick(prevTicks, ticks, CentralProcessor.TickType.IOWAIT);
|
||||
// CPU闲置时间
|
||||
final long idle = tick(prevTicks, ticks, CentralProcessor.TickType.IDLE);
|
||||
final CpuTicks ticks = new CpuTicks(processor, waitingTime);
|
||||
this.ticks = ticks;
|
||||
|
||||
this.cpuNum = processor.getLogicalProcessorCount();
|
||||
final long totalCpu = Math.max(user + nice + cSys + idle + ioWait + irq + softIrq + steal, 0);
|
||||
this.toTal = totalCpu;
|
||||
|
||||
this.sys = formatDouble(cSys, totalCpu);
|
||||
this.used = formatDouble(user, totalCpu);
|
||||
this.wait = formatDouble(ioWait, totalCpu);
|
||||
this.free = formatDouble(idle, totalCpu);
|
||||
this.cpuModel = processor.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取一段时间内的CPU负载标记差
|
||||
*
|
||||
* @param prevTicks 开始的ticks
|
||||
* @param ticks 结束的ticks
|
||||
* @param tickType tick类型
|
||||
* @return 标记差
|
||||
* @since 5.7.12
|
||||
*/
|
||||
private static long tick(long[] prevTicks, long[] ticks, CentralProcessor.TickType tickType) {
|
||||
return ticks[tickType.getIndex()] - prevTicks[tickType.getIndex()];
|
||||
final long totalCpu = ticks.totalCpu();
|
||||
this.toTal = totalCpu;
|
||||
this.sys = formatDouble(ticks.cSys, totalCpu);
|
||||
this.used = formatDouble(ticks.user, totalCpu);
|
||||
this.wait = formatDouble(ticks.ioWait, totalCpu);
|
||||
this.free = formatDouble(ticks.idle, totalCpu);
|
||||
}
|
||||
|
||||
/**
|
||||
|
145
hutool-system/src/main/java/cn/hutool/system/oshi/CpuTicks.java
Normal file
145
hutool-system/src/main/java/cn/hutool/system/oshi/CpuTicks.java
Normal file
@ -0,0 +1,145 @@
|
||||
package cn.hutool.system.oshi;
|
||||
|
||||
import oshi.hardware.CentralProcessor;
|
||||
import oshi.util.Util;
|
||||
|
||||
/**
|
||||
* CPU负载时间信息
|
||||
*
|
||||
* @author looly
|
||||
* @since 5.7.12
|
||||
*/
|
||||
public class CpuTicks {
|
||||
|
||||
long idle;
|
||||
long nice;
|
||||
long irq;
|
||||
long softIrq;
|
||||
long steal;
|
||||
long cSys;
|
||||
long user;
|
||||
long ioWait;
|
||||
|
||||
/**
|
||||
* 构造,等待时间为用于计算在一定时长内的CPU负载情况,如传入1000表示最近1秒的负载情况
|
||||
*
|
||||
* @param processor {@link CentralProcessor}
|
||||
* @param waitingTime 设置等待时间,单位毫秒
|
||||
*/
|
||||
public CpuTicks(CentralProcessor processor, long waitingTime) {
|
||||
// CPU信息
|
||||
final long[] prevTicks = processor.getSystemCpuLoadTicks();
|
||||
// 这里必须要设置延迟
|
||||
Util.sleep(waitingTime);
|
||||
final long[] ticks = processor.getSystemCpuLoadTicks();
|
||||
|
||||
this.idle = tick(prevTicks, ticks, CentralProcessor.TickType.IDLE);
|
||||
this.nice = tick(prevTicks, ticks, CentralProcessor.TickType.NICE);
|
||||
this.irq = tick(prevTicks, ticks, CentralProcessor.TickType.IRQ);
|
||||
this.softIrq = tick(prevTicks, ticks, CentralProcessor.TickType.SOFTIRQ);
|
||||
this.steal = tick(prevTicks, ticks, CentralProcessor.TickType.STEAL);
|
||||
this.cSys = tick(prevTicks, ticks, CentralProcessor.TickType.SYSTEM);
|
||||
this.user = tick(prevTicks, ticks, CentralProcessor.TickType.USER);
|
||||
this.ioWait = tick(prevTicks, ticks, CentralProcessor.TickType.IOWAIT);
|
||||
}
|
||||
|
||||
public long getIdle() {
|
||||
return idle;
|
||||
}
|
||||
|
||||
public void setIdle(long idle) {
|
||||
this.idle = idle;
|
||||
}
|
||||
|
||||
public long getNice() {
|
||||
return nice;
|
||||
}
|
||||
|
||||
public void setNice(long nice) {
|
||||
this.nice = nice;
|
||||
}
|
||||
|
||||
public long getIrq() {
|
||||
return irq;
|
||||
}
|
||||
|
||||
public void setIrq(long irq) {
|
||||
this.irq = irq;
|
||||
}
|
||||
|
||||
public long getSoftIrq() {
|
||||
return softIrq;
|
||||
}
|
||||
|
||||
public void setSoftIrq(long softIrq) {
|
||||
this.softIrq = softIrq;
|
||||
}
|
||||
|
||||
public long getSteal() {
|
||||
return steal;
|
||||
}
|
||||
|
||||
public void setSteal(long steal) {
|
||||
this.steal = steal;
|
||||
}
|
||||
|
||||
public long getcSys() {
|
||||
return cSys;
|
||||
}
|
||||
|
||||
public void setcSys(long cSys) {
|
||||
this.cSys = cSys;
|
||||
}
|
||||
|
||||
public long getUser() {
|
||||
return user;
|
||||
}
|
||||
|
||||
public void setUser(long user) {
|
||||
this.user = user;
|
||||
}
|
||||
|
||||
public long getIoWait() {
|
||||
return ioWait;
|
||||
}
|
||||
|
||||
public void setIoWait(long ioWait) {
|
||||
this.ioWait = ioWait;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取CPU总的使用率
|
||||
*
|
||||
* @return CPU总使用率
|
||||
*/
|
||||
public long totalCpu() {
|
||||
return Math.max(user + nice + cSys + idle + ioWait + irq + softIrq + steal, 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "CpuTicks{" +
|
||||
"idle=" + idle +
|
||||
", nice=" + nice +
|
||||
", irq=" + irq +
|
||||
", softIrq=" + softIrq +
|
||||
", steal=" + steal +
|
||||
", cSys=" + cSys +
|
||||
", user=" + user +
|
||||
", ioWait=" + ioWait +
|
||||
'}';
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取一段时间内的CPU负载标记差
|
||||
*
|
||||
* @param prevTicks 开始的ticks
|
||||
* @param ticks 结束的ticks
|
||||
* @param tickType tick类型
|
||||
* @return 标记差
|
||||
* @since 5.7.12
|
||||
*/
|
||||
private static long tick(long[] prevTicks, long[] ticks, CentralProcessor.TickType tickType) {
|
||||
return ticks[tickType.getIndex()] - prevTicks[tickType.getIndex()];
|
||||
}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package cn.hutool.system;
|
||||
|
||||
import cn.hutool.core.lang.Console;
|
||||
import cn.hutool.system.oshi.OshiUtil;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
@Ignore
|
||||
public class OshiPrintTest {
|
||||
|
||||
@Test
|
||||
public void printCpuInfo(){
|
||||
Console.log(OshiUtil.getCpuInfo());
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user