Skip to content

Commit d241171

Browse files
committed
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
1 parent 3953bab commit d241171

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/LaunchedURLClassLoader.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2014 the original author or authors.
2+
* Copyright 2012-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,6 +33,7 @@
3333
*
3434
* @author Phillip Webb
3535
* @author Dave Syer
36+
* @author Andy Wilkinson
3637
*/
3738
public class LaunchedURLClassLoader extends URLClassLoader {
3839

@@ -107,8 +108,14 @@ public Enumeration<URL> getResources(String name) throws IOException {
107108

108109
@Override
109110
public boolean hasMoreElements() {
110-
return rootResources.hasMoreElements()
111-
|| localResources.hasMoreElements();
111+
try {
112+
Handler.setUseFastConnectionExceptions(true);
113+
return rootResources.hasMoreElements()
114+
|| localResources.hasMoreElements();
115+
}
116+
finally {
117+
Handler.setUseFastConnectionExceptions(false);
118+
}
112119
}
113120

114121
@Override

0 commit comments

Comments
 (0)