博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
聊聊springboot的HeapDumpWebEndpoint
阅读量:6148 次
发布时间:2019-06-21

本文共 6126 字,大约阅读时间需要 20 分钟。

本文主要研究下springboot的HeapDumpWebEndpoint

HeapDumpWebEndpointAutoConfiguration

spring-boot-actuator-autoconfigure-2.0.1.RELEASE-sources.jar!/org/springframework/boot/actuate/autoconfigure/management/HeapDumpWebEndpointAutoConfiguration.java

@Configurationpublic class HeapDumpWebEndpointAutoConfiguration {	@Bean	@ConditionalOnMissingBean	@ConditionalOnEnabledEndpoint	public HeapDumpWebEndpoint heapDumpWebEndpoint() {		return new HeapDumpWebEndpoint();	}}复制代码

HeapDumpWebEndpoint

spring-boot-actuator-2.0.1.RELEASE-sources.jar!/org/springframework/boot/actuate/management/HeapDumpWebEndpoint.java

@WebEndpoint(id = "heapdump")public class HeapDumpWebEndpoint {	private final long timeout;	private final Lock lock = new ReentrantLock();	private HeapDumper heapDumper;	public HeapDumpWebEndpoint() {		this(TimeUnit.SECONDS.toMillis(10));	}	protected HeapDumpWebEndpoint(long timeout) {		this.timeout = timeout;	}	@ReadOperation	public WebEndpointResponse
heapDump(@Nullable Boolean live) { try { if (this.lock.tryLock(this.timeout, TimeUnit.MILLISECONDS)) { try { return new WebEndpointResponse<>( dumpHeap(live == null ? true : live)); } finally { this.lock.unlock(); } } } catch (InterruptedException ex) { Thread.currentThread().interrupt(); } catch (IOException ex) { return new WebEndpointResponse<>( WebEndpointResponse.STATUS_INTERNAL_SERVER_ERROR); } catch (HeapDumperUnavailableException ex) { return new WebEndpointResponse<>( WebEndpointResponse.STATUS_SERVICE_UNAVAILABLE); } return new WebEndpointResponse<>(WebEndpointResponse.STATUS_TOO_MANY_REQUESTS); } private Resource dumpHeap(boolean live) throws IOException, InterruptedException { if (this.heapDumper == null) { this.heapDumper = createHeapDumper(); } File file = createTempFile(live); this.heapDumper.dumpHeap(file, live); return new TemporaryFileSystemResource(file); } private File createTempFile(boolean live) throws IOException { String date = new SimpleDateFormat("yyyy-MM-dd-HH-mm").format(new Date()); File file = File.createTempFile("heapdump" + date + (live ? "-live" : ""), ".hprof"); file.delete(); return file; } /** * Factory method used to create the {@link HeapDumper}. * @return the heap dumper to use * @throws HeapDumperUnavailableException if the heap dumper cannot be created */ protected HeapDumper createHeapDumper() throws HeapDumperUnavailableException { return new HotSpotDiagnosticMXBeanHeapDumper(); }}复制代码

这里调用的是dumpHeap方法,委托给heapDumper来执行

heapDumper

/**	 * Factory method used to create the {@link HeapDumper}.	 * @return the heap dumper to use	 * @throws HeapDumperUnavailableException if the heap dumper cannot be created	 */	protected HeapDumper createHeapDumper() throws HeapDumperUnavailableException {		return new HotSpotDiagnosticMXBeanHeapDumper();	}	/**	 * Strategy interface used to dump the heap to a file.	 */	@FunctionalInterface	protected interface HeapDumper {		/**		 * Dump the current heap to the specified file.		 * @param file the file to dump the heap to		 * @param live if only live objects (i.e. objects that are reachable from		 * others) should be dumped		 * @throws IOException on IO error		 * @throws InterruptedException on thread interruption		 */		void dumpHeap(File file, boolean live) throws IOException, InterruptedException;	}	/**	 * {@link HeapDumper} that uses {@code com.sun.management.HotSpotDiagnosticMXBean}	 * available on Oracle and OpenJDK to dump the heap to a file.	 */	protected static class HotSpotDiagnosticMXBeanHeapDumper implements HeapDumper {		private Object diagnosticMXBean;		private Method dumpHeapMethod;		@SuppressWarnings("unchecked")		protected HotSpotDiagnosticMXBeanHeapDumper() {			try {				Class
diagnosticMXBeanClass = ClassUtils.resolveClassName( "com.sun.management.HotSpotDiagnosticMXBean", null); this.diagnosticMXBean = ManagementFactory.getPlatformMXBean( (Class
) diagnosticMXBeanClass); this.dumpHeapMethod = ReflectionUtils.findMethod(diagnosticMXBeanClass, "dumpHeap", String.class, Boolean.TYPE); } catch (Throwable ex) { throw new HeapDumperUnavailableException( "Unable to locate HotSpotDiagnosticMXBean", ex); } } @Override public void dumpHeap(File file, boolean live) { ReflectionUtils.invokeMethod(this.dumpHeapMethod, this.diagnosticMXBean, file.getAbsolutePath(), live); } }复制代码

这里创建的是HotSpotDiagnosticMXBeanHeapDumper,利用反射调用HotSpotDiagnosticMXBean的dumpHeap方法

TemporaryFileSystemResource

最后通过TemporaryFileSystemResource返回

private static final class TemporaryFileSystemResource extends FileSystemResource {		private final Log logger = LogFactory.getLog(getClass());		private TemporaryFileSystemResource(File file) {			super(file);		}		@Override		public ReadableByteChannel readableChannel() throws IOException {			ReadableByteChannel readableChannel = super.readableChannel();			return new ReadableByteChannel() {				@Override				public boolean isOpen() {					return readableChannel.isOpen();				}				@Override				public void close() throws IOException {					closeThenDeleteFile(readableChannel);				}				@Override				public int read(ByteBuffer dst) throws IOException {					return readableChannel.read(dst);				}			};		}		@Override		public InputStream getInputStream() throws IOException {			return new FilterInputStream(super.getInputStream()) {				@Override				public void close() throws IOException {					closeThenDeleteFile(this.in);				}			};		}		private void closeThenDeleteFile(Closeable closeable) throws IOException {			try {				closeable.close();			}			finally {				deleteFile();			}		}		private void deleteFile() {			try {				Files.delete(getFile().toPath());			}			catch (IOException ex) {				TemporaryFileSystemResource.this.logger.warn(						"Failed to delete temporary heap dump file '" + getFile() + "'",						ex);			}		}		@Override		public boolean isFile() {			// Prevent zero-copy so we can delete the file on close			return false;		}	}复制代码

小结

heapDump的实现是依赖HotSpotDiagnosticMXBean来,如果不存在则报HeapDumperUnavailableException,然后状态码返回STATUS_SERVICE_UNAVAILABLE。如果存在则默认dump存活的对象。

doc

转载地址:http://ahmya.baihongyu.com/

你可能感兴趣的文章
修改GRUB2背景图片
查看>>
Ajax异步
查看>>
好记性不如烂笔杆-android学习笔记<十六> switcher和gallery
查看>>
JAVA GC
查看>>
3springboot:springboot配置文件(外部配置加载顺序、自动配置原理,@Conditional)
查看>>
9、Dubbo-配置(4)
查看>>
前端第七天
查看>>
图解SSH原理及两种登录方法
查看>>
[转载] 七龙珠第一部——第058话 魔境圣地
查看>>
【总结整理】JQuery基础学习---样式篇
查看>>
查询个人站点的文章、分类和标签查询
查看>>
基础知识:数字、字符串、列表 的类型及内置方法
查看>>
JSP的隐式对象
查看>>
JS图片跟着鼠标跑效果
查看>>
[SCOI2005][BZOJ 1084]最大子矩阵
查看>>
学习笔记之Data Visualization
查看>>
Leetcode 3. Longest Substring Without Repeating Characters
查看>>
416. Partition Equal Subset Sum
查看>>
Django之FBV与CBV
查看>>
Vue之项目搭建
查看>>