From d241171fff8c2c18409a87492c6c033c787a06a8 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Mon, 3 Aug 2015 15:39:56 +0100 Subject: [PATCH] Use fast exceptions in hasMoreElements in LaunchedURLClassLoader When nested jars are being used, hasMoreElements requires opening a connection for an entry in every nested jar. If that entry doesn't exist, a FileNotFoundException is thrown to indicate that a particular jar doesn't contain the requested entry. This exception is used to indicate the lack of an entry and is then swallowed, i.e. its stack trace is of no importance. This means that the performance of hasMoreElements can be improved by switching on fast exceptions while it's being called. When fast exceptions are switched on a general purpose pre-initialized FileNotFoundException is thrown rather than creating a new FileNotFoundException instance each time. In certain situations, the use of fast exceptions as described above can improve performance fairly significantly. The JRE's default SAAJ implementation uses META-INF/services-based discovery for _every_ request that's handled by Spring Web Services. Each discovery attempt results in hasMoreElements being called making its performance critical to throughput. See gh-3640 --- .../boot/loader/LaunchedURLClassLoader.java | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/LaunchedURLClassLoader.java b/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/LaunchedURLClassLoader.java index 48b76904d9e..835b4dfa8fb 100644 --- a/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/LaunchedURLClassLoader.java +++ b/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/LaunchedURLClassLoader.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2014 the original author or authors. + * Copyright 2012-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -33,6 +33,7 @@ import org.springframework.boot.loader.jar.JarFile; * * @author Phillip Webb * @author Dave Syer + * @author Andy Wilkinson */ public class LaunchedURLClassLoader extends URLClassLoader { @@ -107,8 +108,14 @@ public class LaunchedURLClassLoader extends URLClassLoader { @Override public boolean hasMoreElements() { - return rootResources.hasMoreElements() - || localResources.hasMoreElements(); + try { + Handler.setUseFastConnectionExceptions(true); + return rootResources.hasMoreElements() + || localResources.hasMoreElements(); + } + finally { + Handler.setUseFastConnectionExceptions(false); + } } @Override