-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Jackson serialization for multi classloader (#1438)
* jackson serialization for multi classloader * clear cache --------- Co-authored-by: yuanyuan <[email protected]>
- Loading branch information
1 parent
2d8190f
commit 46d1dcd
Showing
2 changed files
with
30 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
|
||
import java.lang.reflect.Method; | ||
import java.lang.reflect.Type; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
import com.alipay.sofa.rpc.common.utils.ClassUtils; | ||
|
@@ -27,6 +28,8 @@ | |
import com.fasterxml.jackson.databind.JavaType; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import static com.alipay.sofa.rpc.common.utils.ClassLoaderUtils.getCurrentClassLoader; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">zhiyuan.lzy</a> | ||
*/ | ||
|
@@ -37,12 +40,12 @@ public class JacksonHelper { | |
/** | ||
* Request service and method cache {service+method:class} | ||
*/ | ||
private ConcurrentHashMap<String, JavaType[]> requestClassCache = new ConcurrentHashMap<String, JavaType[]>(); | ||
private ConcurrentHashMap<ClassLoader, Map<String, JavaType[]>> requestClassCache = new ConcurrentHashMap<>(); | ||
|
||
/** | ||
* Response service and method cache {service+method:class} | ||
*/ | ||
private ConcurrentHashMap<String, JavaType> responseClassCache = new ConcurrentHashMap<String, JavaType>(); | ||
private ConcurrentHashMap<ClassLoader, Map<String, JavaType>> responseClassCache = new ConcurrentHashMap<>(); | ||
|
||
/** | ||
* Fetch request class for cache according service and method | ||
|
@@ -54,14 +57,30 @@ public class JacksonHelper { | |
public JavaType[] getReqClass(String service, String methodName) { | ||
|
||
String key = buildMethodKey(service, methodName); | ||
Type[] reqClassList = requestClassCache.get(key); | ||
Type[] reqClassList = getRequestCache().get(key); | ||
if (reqClassList == null) { | ||
//read interface and method from cache | ||
String interfaceClass = ConfigUniqueNameGenerator.getInterfaceName(service); | ||
Class clazz = ClassUtils.forName(interfaceClass, true); | ||
loadClassToCache(key, clazz, methodName); | ||
} | ||
return requestClassCache.get(key); | ||
return getRequestCache().get(key); | ||
} | ||
|
||
private Map<String, JavaType[]> getRequestCache() { | ||
return requestClassCache.computeIfAbsent(getCurrentClassLoader(), k -> new ConcurrentHashMap<>()); | ||
} | ||
|
||
private Map<String, JavaType> getResponseCache() { | ||
return responseClassCache.computeIfAbsent(getCurrentClassLoader(), k -> new ConcurrentHashMap<>()); | ||
} | ||
|
||
public void clearCache(ClassLoader classLoader) { | ||
if (classLoader == null) { | ||
return; | ||
} | ||
requestClassCache.remove(classLoader); | ||
responseClassCache.remove(classLoader); | ||
} | ||
|
||
/** | ||
|
@@ -73,14 +92,14 @@ public JavaType[] getReqClass(String service, String methodName) { | |
*/ | ||
public JavaType getResClass(String service, String methodName) { | ||
String key = service + "#" + methodName; | ||
JavaType reqType = responseClassCache.get(key); | ||
JavaType reqType = getResponseCache().get(key); | ||
if (reqType == null) { | ||
// 读取接口里的方法参数和返回值 | ||
String interfaceClass = ConfigUniqueNameGenerator.getInterfaceName(service); | ||
Class clazz = ClassUtils.forName(interfaceClass, true); | ||
loadClassToCache(key, clazz, methodName); | ||
} | ||
return responseClassCache.get(key); | ||
return getResponseCache().get(key); | ||
} | ||
|
||
/** | ||
|
@@ -122,14 +141,14 @@ private void loadClassToCache(String key, Class clazz, String methodName) { | |
JavaType javaType = mapper.getTypeFactory().constructType(parameterTypes[i]); | ||
javaTypes[i] = javaType; | ||
} | ||
requestClassCache.put(key, javaTypes); | ||
getRequestCache().put(key, javaTypes); | ||
|
||
// parse response types | ||
Type resType = jsonMethod.getGenericReturnType(); | ||
if (resType == void.class) { | ||
throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_VOID_RETURN, "jackson", clazz.getName())); | ||
} | ||
JavaType resJavaType = mapper.getTypeFactory().constructType(resType); | ||
responseClassCache.put(key, resJavaType); | ||
getResponseCache().put(key, resJavaType); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters