Skip to content

Commit a4005a1

Browse files
author
zhourenjian@gmail.com
committed
On reloading class, try to pre-load class in given class loader to avoid being blocked on reading class bytes from disk
1 parent 711b3a7 commit a4005a1

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

sources/net.sf.j2s.ajax/ajaxrpc/net/sf/j2s/ajax/SimpleClassLoader.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,26 @@ private byte[] loadClassData(String className) throws IOException {
131131
return buff;
132132
}
133133

134+
/**
135+
* Load given class.
136+
*
137+
* @param clazzName
138+
* @return
139+
*/
140+
public static Class<?> loadSimpleClass(String clazzName) {
141+
try {
142+
ClassLoader classLoader = hasClassReloaded ? allLoaders.get(clazzName) : null;
143+
if (classLoader != null) {
144+
return classLoader.loadClass(clazzName);
145+
} else {
146+
return Class.forName(clazzName);
147+
}
148+
} catch (Exception e) {
149+
//e.printStackTrace();
150+
}
151+
return null;
152+
}
153+
134154
/**
135155
* Load given class and create instance by default constructor.
136156
*
@@ -225,6 +245,24 @@ public static void reloadSimpleClasses(String[] clazzNames, String path, String
225245
if (creatingMore) { // keep it in all loaders
226246
allLoaders.put("." + (checkedLoaders.size() + 1), loader);
227247
}
248+
/*
249+
* It is important to pre-load classes. On server with
250+
* heavy traffic, If classes are not not being pre-
251+
* loaded, later reading class bytes from disk (IO) may
252+
* cause lots of threads from thread pool hanging for
253+
* some milliseconds, and threads are eaten up, and
254+
* server may be down in seconds!
255+
*/
256+
for (int i = 0; i < clazzNames.length; i++) {
257+
String name = clazzNames[i];
258+
if (name != null && name.length() > 0) {
259+
try {
260+
loader.loadClass(name);
261+
} catch (ClassNotFoundException e) {
262+
e.printStackTrace();
263+
}
264+
}
265+
}
228266
for (int i = 0; i < clazzNames.length; i++) {
229267
String name = clazzNames[i];
230268
if (name != null && name.length() > 0) {

0 commit comments

Comments
 (0)