Skip to content

Commit

Permalink
Fix getCallerClassLoader bug (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
alaneuler authored Apr 26, 2020
1 parent 6ed1747 commit 5abb1cf
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 13 deletions.
2 changes: 1 addition & 1 deletion core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>sofa-common-tools-parent</artifactId>
<groupId>com.alipay.sofa.common</groupId>
<version>1.1.0</version>
<version>1.1.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@
* Created on 2020/3/23
*/
public class SofaThreadPoolTaskExecutor extends ThreadPoolTaskExecutor {
public static final String SIMPLE_CLASS_NAME = SofaThreadPoolTaskExecutor.class
.getSimpleName();
private static long DEFAULT_TASK_TIMEOUT = 30000;
private static long DEFAULT_PERIOD = 5000;
private static TimeUnit DEFAULT_TIME_UNIT = TimeUnit.MILLISECONDS;
public static final String SIMPLE_CLASS_NAME = SofaThreadPoolTaskExecutor.class
.getSimpleName();
protected static long DEFAULT_TASK_TIMEOUT = 30000;
protected static long DEFAULT_PERIOD = 5000;
protected static TimeUnit DEFAULT_TIME_UNIT = TimeUnit.MILLISECONDS;

private SofaThreadPoolExecutor sofaThreadPoolExecutor;
protected SofaThreadPoolExecutor sofaThreadPoolExecutor;

@Override
protected ExecutorService initializeExecutor(ThreadFactory threadFactory,
Expand Down Expand Up @@ -76,7 +76,7 @@ rejectedExecutionHandler, createName(), DEFAULT_TASK_TIMEOUT, DEFAULT_PERIOD,
return executor;
}

private String createName() {
protected String createName() {
return SIMPLE_CLASS_NAME + String.format("%08x", this.hashCode());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,13 @@ public static Class loadClass(String className, Class referrer) throws ClassNotF
public static ClassLoader getCallerClassLoader() {
try {
String callerClassName = Thread.currentThread().getStackTrace()[3].getClassName();
return loadClass(callerClassName).getClassLoader();

try {
return Class.forName(callerClassName).getClassLoader();
} catch (ClassNotFoundException e) {
return Class.forName(callerClassName, true, getContextClassLoader())
.getClassLoader();
}
} catch (Throwable t) {
throw new RuntimeException("Failed to get caller classloader ", t);
}
Expand Down
2 changes: 1 addition & 1 deletion log-sofa-boot-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>sofa-common-tools-parent</artifactId>
<groupId>com.alipay.sofa.common</groupId>
<version>1.1.0</version>
<version>1.1.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.alipay.sofa.common</groupId>
<artifactId>sofa-common-tools-parent</artifactId>
<version>1.1.0</version>
<version>1.1.1</version>
<modules>
<module>core</module>
<module>log-sofa-boot-starter</module>
Expand All @@ -27,7 +27,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<logback.version>1.1.7</logback.version>
<slf4j.version>1.7.21</slf4j.version>
<common.tools.version>1.1.0</common.tools.version>
<common.tools.version>1.1.1</common.tools.version>
<log4j2.version>2.3</log4j2.version>
<java.version>1.6</java.version>
</properties>
Expand Down
2 changes: 1 addition & 1 deletion test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>sofa-common-tools-parent</artifactId>
<groupId>com.alipay.sofa.common</groupId>
<version>1.1.0</version>
<version>1.1.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down

0 comments on commit 5abb1cf

Please sign in to comment.