spring jmx

本文是本系列的最后一篇。 看一下第1 部分和第2部分 。

在本系列的最后一篇文章中,我将展示如何在JDK中使用本机JMX支持来实现一种通知机制,该机制可以在HEAP内存超过特定阈值时向侦听器发出警报。

正如我在上一篇文章中讨论的那样,这种方法是理想的,因为它是推式而不是拉式,不是侵入式的,并且对您的应用程序的计算需求最小。

这些是本文说明的解决方案的关键组件:

  • MemoryWarningService:此组件充当侦听器,并向Memory MBean注册其自身以接收通知。 可以使用介于0到1之间的百分比形式的阈值进行配置(其中1为100%)
  • MemoryThreadDumper:当通知MemoryWarningService HEAP使用率超过阈值并且其职责是将线程转储写入文件时,将调用此组件。
  • MemoryWarningServiceConfigurator:此组件是MBean,它公开一种用于更改MemoryWarningService阈值的方法。

该解决方案还提供用于在测试应用程序时填充HEAP的MemoryHeapFiller类和用于引导Spring环境的MemTest类。

当应用程序运行时(使用MemoryHeapFiller设置),您可以在URL上触发JConsole:service:jmx:rmi:// localhost / jndi / rmi:// localhost:8888 / jemosJmxConnector以jemosAdmin / secure连接并更改阈值各种价值。

该代码不是用于生产的:它不健壮,缺少许多注释,并且写入线程转储的文件名是硬编码的。 但是,它代表了一个很好的起点。

该代码附在下面。 您将需要Maven来构建它。

下载Jemos-jmx-experiments-0.0.1-SNAPSHOT-project

我尝试了一个初始阈值为0.5的方案,将其更改为0.3,然后更改为0.8。 结果如下所示:

2011-08-15 21:53:21 ClassPathXmlApplicationContext [INFO] Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@a4a63d8: startup date [Mon Aug 15 21:53:21 BST 2011]; root of context hierarchy
2011-08-15 21:53:21 XmlBeanDefinitionReader [INFO] Loading XML bean definitions from class path resource [jemos-jmx-appCtx.xml]
2011-08-15 21:53:21 PropertyPlaceholderConfigurer [INFO] Loading properties file from class path resource [jemos-jmx.properties]
2011-08-15 21:53:21 PropertyPlaceholderConfigurer [INFO] Loading properties file from URL [file:/C:/Users/mtedone/.secure/jmxconnector-credentials.properties]
2011-08-15 21:53:21 ThreadPoolTaskScheduler [INFO] Initializing ExecutorService  'myScheduler'
2011-08-15 21:53:21 ClassPathXmlApplicationContext [INFO] Bean 'myScheduler' of type [class org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2011-08-15 21:53:21 DefaultListableBeanFactory [INFO] Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@30296f76: defining beans [propertyConfigurer,loggerConfigurator,memoryWarningServiceConfigurator,memoryHeapFiller,memoryThreadDumper,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,mbeanExporter,jemosJmxServer,rmiRegistry,clientConnector,memoryMxBean,memoryWarningService,org.springframework.scheduling.annotation.internalAsyncAnnotationProcessor,org.springframework.scheduling.annotation.internalScheduledAnnotationProcessor,myScheduler]; root of factory hierarchy
2011-08-15 21:53:21 AnnotationMBeanExporter [INFO] Registering beans for JMX exposure on startup
2011-08-15 21:53:21 RmiRegistryFactoryBean [INFO] Looking for RMI registry at port '8888'
2011-08-15 21:53:23 RmiRegistryFactoryBean [INFO] Could not detect RMI registry - creating new one
2011-08-15 21:53:23 ConnectorServerFactoryBean [INFO] JMX connector server started: javax.management.remote.rmi.RMIConnectorServer@4355d3a3
2011-08-15 21:53:23 AnnotationMBeanExporter [INFO] Bean with name 'jemosJmxServer' has been autodetected for JMX exposure
2011-08-15 21:53:23 AnnotationMBeanExporter [INFO] Bean with name 'loggerConfigurator' has been autodetected for JMX exposure
2011-08-15 21:53:23 AnnotationMBeanExporter [INFO] Bean with name 'memoryWarningServiceConfigurator' has been autodetected for JMX exposure
2011-08-15 21:53:23 AnnotationMBeanExporter [INFO] Located managed bean 'loggerConfigurator': registering with JMX server as MBean [jemos.mbeans:type=config,name=LoggingConfiguration]
2011-08-15 21:53:23 AnnotationMBeanExporter [INFO] Located MBean 'jemosJmxServer': registering with JMX server as MBean [jemos.mbeans:name=jemosJmxServer,type=RMIConnectorServer]
2011-08-15 21:53:23 AnnotationMBeanExporter [INFO] Located managed bean 'memoryWarningServiceConfigurator': registering with JMX server as MBean [jemos.mbeans:type=config,name=MemoryWarningServiceConfiguration]
2011-08-15 21:53:23 MemoryWarningService [INFO] Percentage is: 0.5
2011-08-15 21:53:23 MemoryWarningService [INFO] Listener added to JMX bean
Adding data...
Adding data...
Adding data...
Adding data...
Adding data...
Adding data...
Adding data...
2011-08-15 21:53:37 MemoryWarningService [INFO] Percentage is: 0.3
2011-08-15 21:53:37 MemoryWarningServiceConfigurator [INFO] Memory threshold set to 0.3
Adding data...
2011-08-15 21:53:38 MemoryWarningService [WARN] Memory usage low!!!
2011-08-15 21:53:38 MemoryWarningService [WARN] percentageUsed = 0.3815679398794023
2011-08-15 21:53:38 MemoryThreadDumper [WARN] Stacks dumped to: C:/tmp/stacks.dump
Adding data...
Adding data...
Adding data...
2011-08-15 21:53:45 MemoryWarningService [INFO] Percentage is: 0.8
2011-08-15 21:53:45 MemoryWarningServiceConfigurator [INFO] Memory threshold set to 0.8
Adding data...
Adding data...
Adding data...
Adding data...
Adding data...
Adding data...
Adding data...
2011-08-15 21:54:01 MemoryWarningService [WARN] Memory usage low!!!
2011-08-15 21:54:01 MemoryWarningService [WARN] percentageUsed = 0.8383333266727508
2011-08-15 21:54:02 MemoryThreadDumper [WARN] Stacks dumped to: C:/tmp/stacks.dump
Adding data...
Adding data...
Adding data...
Exception in thread "JMX server connection timeout 24" java.lang.OutOfMemoryError: Java heap space

内存警告服务

package uk.co.jemos.experiments.jmx;import java.lang.management.ManagementFactory;
import java.lang.management.MemoryNotificationInfo;
import java.lang.management.MemoryPoolMXBean;
import java.lang.management.MemoryType;import javax.annotation.PostConstruct;
import javax.management.Notification;
import javax.management.NotificationEmitter;
import javax.management.NotificationListener;import org.springframework.beans.factory.annotation.Autowired;/*** A component which sends notifications when the HEAP memory is above a certain* threshold.* * @author mtedone* */
public class MemoryWarningService implements NotificationListener {/** This bean's name */public static final String MBEAN_NAME = "jemos.mbeans:type=monitoring,name=MemoryWarningService";/** The application logger */private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(MemoryWarningService.class);@Autowiredprivate NotificationEmitter memoryMxBean;@Autowiredprivate MemoryThreadDumper threadDumper;/** A pool of Memory MX Beans specialised in HEAP management */private static final MemoryPoolMXBean tenuredGenPool = findTenuredGenPool();/*** {@inheritDoc}*/@Overridepublic void handleNotification(Notification notification, Object handback) {if (notification.getType().equals(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED)) {long maxMemory = tenuredGenPool.getUsage().getMax();long usedMemory = tenuredGenPool.getUsage().getUsed();LOG.warn("Memory usage low!!!");double percentageUsed = (double) usedMemory / maxMemory;LOG.warn("percentageUsed = " + percentageUsed);threadDumper.dumpStacks();} else {LOG.info("Other notification received..."+ notification.getMessage());}}/*** It sets the threshold percentage.* * @param percentage*/public void setPercentageUsageThreshold(double percentage) {if (percentage <= 0.0 || percentage > 1.0) {throw new IllegalArgumentException("Percentage not in range");} else {LOG.info("Percentage is: " + percentage);}long maxMemory = tenuredGenPool.getUsage().getMax();long warningThreshold = (long) (maxMemory * percentage);tenuredGenPool.setUsageThreshold(warningThreshold);}@PostConstructpublic void completeSetup() {memoryMxBean.addNotificationListener(this, null, null);LOG.info("Listener added to JMX bean");}/*** Tenured Space Pool can be determined by it being of type HEAP and by it* being possible to set the usage threshold.*/private static MemoryPoolMXBean findTenuredGenPool() {for (MemoryPoolMXBean pool : ManagementFactory.getMemoryPoolMXBeans()) {// I don't know whether this approach is better, or whether// we should rather check for the pool name "Tenured Gen"?if (pool.getType() == MemoryType.HEAP&& pool.isUsageThresholdSupported()) {return pool;}}throw new AssertionError("Could not find tenured space");}}

内存线程转储器

package uk.co.jemos.experiments.jmx;import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Component;/*** This component dumps the thread stack to the file system.* * @author mtedone* */
@Component
public class MemoryThreadDumper {/** The application logger */private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(MemoryThreadDumper.class);/*** It dumps the Thread stacks* * @throws IOException*/public void dumpStacks() {// hard-coded: This needs to be changed to a property or somethingString stackFileName = "C:/tmp/stacks.dump";ThreadMXBean mxBean = ManagementFactory.getThreadMXBean();ThreadInfo[] threadInfos = mxBean.getThreadInfo(mxBean.getAllThreadIds(), 0);Map<Long, ThreadInfo> threadInfoMap = new HashMap<Long, ThreadInfo>();for (ThreadInfo threadInfo : threadInfos) {threadInfoMap.put(threadInfo.getThreadId(), threadInfo);}File dumpFile = new File(stackFileName);BufferedWriter writer = null;try {writer = new BufferedWriter(new FileWriter(dumpFile));this.dumpTraces(mxBean, threadInfoMap, writer);LOG.warn("Stacks dumped to: " + stackFileName);} catch (IOException e) {throw new IllegalStateException("An exception occurred while writing the thread dump");} finally {IOUtils.closeQuietly(writer);}}private void dumpTraces(ThreadMXBean mxBean,Map<Long, ThreadInfo> threadInfoMap, Writer writer)throws IOException {Map<Thread, StackTraceElement[]> stacks = Thread.getAllStackTraces();writer.write("Dump of "+ stacks.size()+ " thread at "+ new SimpleDateFormat("yyyy/MM/dd HH:mm:ss z").format(new Date(System.currentTimeMillis())) + "\n\n");for (Map.Entry<Thread, StackTraceElement[]> entry : stacks.entrySet()) {Thread thread = entry.getKey();writer.write("\"" + thread.getName() + "\" prio="+ thread.getPriority() + " tid=" + thread.getId() + " "+ thread.getState() + " "+ (thread.isDaemon() ? "deamon" : "worker") + "\n");ThreadInfo threadInfo = threadInfoMap.get(thread.getId());if (threadInfo != null) {writer.write("    native=" + threadInfo.isInNative()+ ", suspended=" + threadInfo.isSuspended()+ ", block=" + threadInfo.getBlockedCount() + ", wait="+ threadInfo.getWaitedCount() + "\n");writer.write("    lock=" + threadInfo.getLockName()+ " owned by " + threadInfo.getLockOwnerName() + " ("+ threadInfo.getLockOwnerId() + "), cpu="+ mxBean.getThreadCpuTime(threadInfo.getThreadId())/ 1000000L + ", user="+ mxBean.getThreadUserTime(threadInfo.getThreadId())/ 1000000L + "\n");}for (StackTraceElement element : entry.getValue()) {writer.write("        ");writer.write(element.toString());writer.write("\n");}writer.write("\n");}}}

内存服务配置MBean

package uk.co.jemos.experiments.jmx.mbeans;import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.jmx.export.annotation.ManagedOperation;
import org.springframework.jmx.export.annotation.ManagedOperationParameter;
import org.springframework.jmx.export.annotation.ManagedOperationParameters;
import org.springframework.jmx.export.annotation.ManagedResource;
import org.springframework.stereotype.Component;import uk.co.jemos.experiments.jmx.MemoryWarningService;@Component
@ManagedResource(objectName = MemoryWarningServiceConfigurator.MBEAN_NAME, //
description = "Allows clients to set the memory threshold")
public class MemoryWarningServiceConfigurator implementsApplicationContextAware {/** The application logger */private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(MemoryWarningServiceConfigurator.class);public static final String MBEAN_NAME = "jemos.mbeans:type=config,name=MemoryWarningServiceConfiguration";private ApplicationContext ctx;@ManagedOperation(description = "Sets the memory threshold for the memory warning system")@ManagedOperationParameters({ @ManagedOperationParameter(description = "The memory threshold", name = "memoryThreshold"), })public void setMemoryThreshold(double memoryThreshold) {MemoryWarningService memoryWarningService = (MemoryWarningService) ctx.getBean("memoryWarningService");memoryWarningService.setPercentageUsageThreshold(memoryThreshold);LOG.info("Memory threshold set to " + memoryThreshold);}@Overridepublic void setApplicationContext(ApplicationContext applicationContext)throws BeansException {ctx = applicationContext;}}

Spring配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"xmlns:util="http://www.springframework.org/schema/util"xmlns:task="http://www.springframework.org/schema/task"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"><bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"  ><property name="locations"><list><value>classpath:jemos-jmx.properties</value><value>file:///${user.home}/.secure/jmxconnector-credentials.properties</value></list></property></bean><context:component-scan base-package="uk.co.jemos.experiments.jmx" /><context:mbean-export default-domain="jemos.mbeans" /><bean id="jemosJmxServer" class="org.springframework.jmx.support.ConnectorServerFactoryBean"       depends-on="rmiRegistry"><property name="objectName" value="connector:name=rmi" /><property name="serviceUrl"value="service:jmx:rmi://localhost/jndi/rmi://localhost:${jemos.jmx.rmi.port}/jemosJmxConnector" /><property name="environment"><!-- the following is only valid when the sun jmx implementation is used --><map><entry key="jmx.remote.x.password.file" value="${user.home}/.secure/jmxremote.password" /><entry key="jmx.remote.x.access.file" value="${user.home}/.secure/jmxremote.access" /></map></property></bean><bean id="rmiRegistry" class="org.springframework.remoting.rmi.RmiRegistryFactoryBean"><property name="port" value="${jemos.jmx.rmi.port}" /></bean><bean id="clientConnector" class="org.springframework.jmx.support.MBeanServerConnectionFactoryBean"       depends-on="jemosJmxServer"><property name="serviceUrl"value="service:jmx:rmi://localhost/jndi/rmi://localhost:${jemos.jmx.rmi.port}/jemosJmxConnector" /><property name="environment"><map><entry key="jmx.remote.credentials"><beanfactory-method="commaDelimitedListToStringArray"><constructor-arg value="${jmx.username},${jmx.password}" /></bean></entry></map></property></bean><bean id="memoryMxBean" class="java.lang.management.ManagementFactory"factory-method="getMemoryMXBean" /><bean id="memoryWarningService" class="uk.co.jemos.experiments.jmx.MemoryWarningService"><property name="percentageUsageThreshold" value="0.5" /></bean><task:annotation-driven scheduler="myScheduler" />   <task:scheduler id="myScheduler" pool-size="10" />    </beans>

参考: JMX和Spring –我们的JCG合作伙伴 Marco Tedone的第3部分 ,位于Marco Tedone的博客博客中。

翻译自: https://www.javacodegeeks.com/2012/07/jmx-and-spring-part-3.html

spring jmx

spring jmx_JMX和Spring –第3部分相关推荐

  1. spring jmx_JMX和Spring –第1部分

    spring jmx 这是三篇文章的第一篇,这三篇文章将展示如何通过JMX支持为Spring应用程序赋能. Maven配置 这是用于设置此示例代码的Maven pom.xml: <project ...

  2. spring jmx_JMX和Spring –第2部分

    spring jmx 这篇文章从本教程的第1部分继续. 嗨,在我的上一篇文章中,我解释了如何通过Spring设置JMX服务器以及如何通过身份验证和授权保护对它的访问. 在本文中,我将展示如何实现一个简 ...

  3. spring boot整合spring security笔记

    最近自己做了一个小项目,正在进行springboot和spring Security的整合,有一丢丢的感悟,在这里分享一下: 首先,spring boot整合spring security最好是使用T ...

  4. spring 全局变量_[Spring]-Spring框架-02-IOC容器

    Spring框架 IOC容器 理解IOC容器和DI依赖注入 IOC容器 Inverser Of Control反转控制 就是把原来在程序中创建HelloService对象的控制权限交给Spring管理 ...

  5. Spring Initializr 构建Spring Boot/Cloud工程

    2019独角兽企业重金招聘Python工程师标准>>> 在之前的所有Spring Boot和Spring Cloud相关博文中,都会涉及Spring Boot工程的创建.而创建的方式 ...

  6. Spring MVC 到 Spring BOOT 的简化之路

    点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试资料 来源:juejin.im/post/5aa22d1f5188255 ...

  7. Spring系列之Spring常用注解总结

    参看博客:https://www.cnblogs.com/xiaoxi/p/5935009.html 传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺 ...

  8. spring学习12 -Spring 框架模块以及面试常见问题注解等

    以下为spring常见面试问题: 1.Spring 框架中都用到了哪些设计模式? Spring框架中使用到了大量的设计模式,下面列举了比较有代表性的: 代理模式-在AOP和remoting中被用的比较 ...

  9. Spring Boot(Spring的自动整合框架)

    2019独角兽企业重金招聘Python工程师标准>>> Spring Boot 是一套基于Spring框架的微服务框架,由于Spring是一个轻量级的企业开发框架,主要功能就是用于整 ...

最新文章

  1. OpenCV使用问题汇总
  2. 使用实体框架、Dapper和Chain的仓储模式实现策略
  3. opencv python 将RGB表示的图像转换为HSV(或Ycbcr)表示
  4. how to export many stock data from Wind terminal
  5. SyncStudy Poster
  6. python快速排序算法_Python实现快速排序算法及去重的快速排序的简单示例
  7. Mysql 8 逻辑升级详解
  8. 为什么使用Binder而不是其他IPC机制
  9. ubuntu16.04下安装NS-2.35以及对simple例的理解
  10. C语言基础2-C语言条件结构
  11. MySQL(3)-----DML数据库操作(上)
  12. Discuz 7.0版块横排显示版块图标和版块简介的方法
  13. 【杨中科解惑】我该怎么选择?选择就是放弃
  14. 常见AutoCAD病毒(acad.fas、acad.lsp)清除方法
  15. 地图学(何宗宜版)知识点整理
  16. python实现登录抓取_[Python]网络爬虫(五):利用POST方式登录账号抓取
  17. 3.3Packet Tracer - 实施基本连接
  18. MAT常见泄漏Problem Suspect解析
  19. 寒江独钓 第三章 串口的过滤
  20. 液晶屏 LCD12864 / LCD12232 串行 / 并行接口驱动

热门文章

  1. Hadoop入门(二)集群安装
  2. avue中实现消息的实时展示
  3. 使用java将字符串写入到指定的文件中
  4. 第二章 变量、数据类型和运算符
  5. HBase的hbase shell 详解
  6. php中对象的遍历输出,PHP中的对象遍历技巧
  7. android url回调json,【求助】本地页面如何取某个URL返回的json
  8. 转:java网络编程-HTTP编程
  9. java计算混淆矩阵(分类指标:查准率P,查全率R,P和R的调和均值F1,正确率A)
  10. java无效的源发行版_无效的Java