优化classloader

This commit is contained in:
redkale
2024-10-11 18:57:16 +08:00
parent 5a7181428f
commit 45b0e7098b
36 changed files with 263 additions and 198 deletions

View File

@@ -151,7 +151,8 @@ class AppConfig {
if (compileMode || this.classLoader instanceof RedkaleClassLoader.RedkaleCacheClassLoader) { if (compileMode || this.classLoader instanceof RedkaleClassLoader.RedkaleCacheClassLoader) {
this.serverClassLoader = this.classLoader; this.serverClassLoader = this.classLoader;
} else { } else {
this.serverClassLoader = new RedkaleClassLoader(this.classLoader); //this.serverClassLoader = RedkaleClassLoader.getRedkaleClassLoader(this.classLoader);
this.serverClassLoader = this.classLoader;
} }
} }

View File

@@ -129,11 +129,11 @@ class NodeExpectServiceLoader implements ResourceTypeLoader {
group, group,
entry.getProperty()); entry.getProperty());
} }
final Class restype = Sncp.getResourceType(service); final Class resType = Sncp.getResourceType(service);
if (rf.find(resourceName, restype) == null) { if (rf.find(resourceName, resType) == null) {
regFactory.register(resourceName, restype, service); regFactory.register(resourceName, resType, service);
} else if (nodeServer.isSNCP() && !entry.isAutoload()) { } else if (nodeServer.isSNCP() && !entry.isAutoload()) {
throw new RedkaleException(restype.getSimpleName() + "(class:" + serviceImplClass.getName() + ", name:" throw new RedkaleException(resType.getSimpleName() + "(class:" + serviceImplClass.getName() + ", name:"
+ resourceName + ", group:" + group + ") is repeat."); + resourceName + ", group:" + group + ") is repeat.");
} }
if (Sncp.isRemote(service)) { if (Sncp.isRemote(service)) {

View File

@@ -119,8 +119,8 @@ public class NodeHttpServer extends NodeServer {
@Override @Override
protected void loadService(ClassFilter<? extends Service> serviceFilter) throws Exception { protected void loadService(ClassFilter<? extends Service> serviceFilter) throws Exception {
super.loadService(serviceFilter);
resourceFactory.register(new NodeWebSocketNodeLoader(this)); resourceFactory.register(new NodeWebSocketNodeLoader(this));
super.loadService(serviceFilter);
} }
@Override @Override

View File

@@ -110,12 +110,7 @@ public abstract class NodeServer {
this.server = server; this.server = server;
this.resourceFactory = server.getResourceFactory(); this.resourceFactory = server.getResourceFactory();
this.logger = Logger.getLogger(this.getClass().getSimpleName()); this.logger = Logger.getLogger(this.getClass().getSimpleName());
if (application.isCompileMode() this.serverClassLoader = application.getServerClassLoader();
|| application.getServerClassLoader() instanceof RedkaleClassLoader.RedkaleCacheClassLoader) {
this.serverClassLoader = application.getServerClassLoader();
} else {
this.serverClassLoader = RedkaleClassLoader.getRedkaleClassLoader(application.getServerClassLoader());
}
Thread.currentThread().setContextClassLoader(this.serverClassLoader); Thread.currentThread().setContextClassLoader(this.serverClassLoader);
this.serverThread = Thread.currentThread(); this.serverThread = Thread.currentThread();
this.server.setServerClassLoader(serverClassLoader); this.server.setServerClassLoader(serverClassLoader);
@@ -301,6 +296,12 @@ public abstract class NodeServer {
throw new RedkaleException("Not found group(" + entry.getGroup() + ")"); throw new RedkaleException("Not found group(" + entry.getGroup() + ")");
} }
Service oldOther = resourceFactory.find(entry.getName(), serviceImplClass); Service oldOther = resourceFactory.find(entry.getName(), serviceImplClass);
if (oldOther == null) {
Class<? extends Service> resType = Sncp.getResourceType(serviceImplClass);
if (resType != serviceImplClass) {
oldOther = resourceFactory.find(entry.getName(), resType);
}
}
if (oldOther != null) { // Server加载Service时需要判断是否已在其他协议服务中加载 if (oldOther != null) { // Server加载Service时需要判断是否已在其他协议服务中加载
if (!Sncp.isRemote(oldOther)) { if (!Sncp.isRemote(oldOther)) {
if (!Sncp.isComponent(oldOther)) { if (!Sncp.isComponent(oldOther)) {

View File

@@ -71,7 +71,7 @@ public abstract class JsonDynEncoder<T> extends ObjectEncoder<JsonWriter, T> {
+ clazz.getName().replace('.', '_').replace('$', '_') + "_" + factory.getFeatures() + "_" + clazz.getName().replace('.', '_').replace('$', '_') + "_" + factory.getFeatures() + "_"
+ Utility.md5Hex(elementb.toString()); // tiny必须要加上, 同一个类会有多个字段定制Convert + Utility.md5Hex(elementb.toString()); // tiny必须要加上, 同一个类会有多个字段定制Convert
try { try {
Class clz = RedkaleClassLoader.findDynClass(newDynName.replace('/', '.')); Class clz = loader.findDynClass(newDynName.replace('/', '.'));
Class newClazz = clz == null ? loader.loadClass(newDynName.replace('/', '.')) : clz; Class newClazz = clz == null ? loader.loadClass(newDynName.replace('/', '.')) : clz;
JsonDynEncoder resultEncoder = JsonDynEncoder resultEncoder =
(JsonDynEncoder) newClazz.getConstructor(JsonFactory.class, Type.class, ObjectEncoder.class) (JsonDynEncoder) newClazz.getConstructor(JsonFactory.class, Type.class, ObjectEncoder.class)
@@ -681,7 +681,7 @@ public abstract class JsonDynEncoder<T> extends ObjectEncoder<JsonWriter, T> {
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
byte[] bytes = cw.toByteArray(); byte[] bytes = cw.toByteArray();
Class<?> newClazz = loader.loadClass(newDynName.replace('/', '.'), bytes); Class<?> newClazz = loader.loadClass(newDynName.replace('/', '.'), bytes);
RedkaleClassLoader.putDynClass(newDynName.replace('/', '.'), bytes, newClazz); loader.putDynClass(newDynName.replace('/', '.'), bytes, newClazz);
RedkaleClassLoader.putReflectionDeclaredConstructors( RedkaleClassLoader.putReflectionDeclaredConstructors(
newClazz, newDynName.replace('/', '.'), JsonFactory.class, Type.class); newClazz, newDynName.replace('/', '.'), JsonFactory.class, Type.class);
try { try {

View File

@@ -69,13 +69,13 @@ public abstract class ProtobufDynEncoder<T> extends ProtobufObjectEncoder<T> {
} }
} }
RedkaleClassLoader loader = RedkaleClassLoader.getRedkaleClassLoader(); RedkaleClassLoader classLoader = RedkaleClassLoader.getRedkaleClassLoader();
final String newDynName = "org/redkaledyn/convert/pb/_Dyn" + ProtobufDynEncoder.class.getSimpleName() + "__" final String newDynName = "org/redkaledyn/convert/pb/_Dyn" + ProtobufDynEncoder.class.getSimpleName() + "__"
+ clazz.getName().replace('.', '_').replace('$', '_') + "_" + factory.getFeatures() + "_" + clazz.getName().replace('.', '_').replace('$', '_') + "_" + factory.getFeatures() + "_"
+ Utility.md5Hex(elementb.toString()); // tiny必须要加上, 同一个类会有多个字段定制Convert + Utility.md5Hex(elementb.toString()); // tiny必须要加上, 同一个类会有多个字段定制Convert
try { try {
Class clz = RedkaleClassLoader.findDynClass(newDynName.replace('/', '.')); Class clz = classLoader.findDynClass(newDynName.replace('/', '.'));
Class newClazz = clz == null ? loader.loadClass(newDynName.replace('/', '.')) : clz; Class newClazz = clz == null ? classLoader.loadClass(newDynName.replace('/', '.')) : clz;
ProtobufDynEncoder resultEncoder = (ProtobufDynEncoder) ProtobufDynEncoder resultEncoder = (ProtobufDynEncoder)
newClazz.getConstructor(ProtobufFactory.class, Type.class, ProtobufObjectEncoder.class) newClazz.getConstructor(ProtobufFactory.class, Type.class, ProtobufObjectEncoder.class)
.newInstance(factory, clazz, selfObjEncoder); .newInstance(factory, clazz, selfObjEncoder);
@@ -321,8 +321,8 @@ public abstract class ProtobufDynEncoder<T> extends ProtobufObjectEncoder<T> {
cw.visitEnd(); cw.visitEnd();
byte[] bytes = cw.toByteArray(); byte[] bytes = cw.toByteArray();
Class<ProtobufDynEncoder> newClazz = loader.loadClass(newDynName.replace('/', '.'), bytes); Class<ProtobufDynEncoder> newClazz = classLoader.loadClass(newDynName.replace('/', '.'), bytes);
RedkaleClassLoader.putDynClass(newDynName.replace('/', '.'), bytes, newClazz); classLoader.putDynClass(newDynName.replace('/', '.'), bytes, newClazz);
RedkaleClassLoader.putReflectionDeclaredConstructors(newClazz, newDynName.replace('/', '.')); RedkaleClassLoader.putReflectionDeclaredConstructors(newClazz, newDynName.replace('/', '.'));
try { try {
ProtobufDynEncoder resultEncoder = (ProtobufDynEncoder) ProtobufDynEncoder resultEncoder = (ProtobufDynEncoder)

View File

@@ -328,7 +328,7 @@ public class MessageAsmMethodBoost extends AsmMethodBoost {
consumerBytes.forEach((innerFullName, bytes) -> { consumerBytes.forEach((innerFullName, bytes) -> {
String clzName = innerFullName.replace('/', '.'); String clzName = innerFullName.replace('/', '.');
Class clazz = classLoader.loadClass(clzName, bytes); Class clazz = classLoader.loadClass(clzName, bytes);
RedkaleClassLoader.putDynClass(clzName, bytes, clazz); classLoader.putDynClass(clzName, bytes, clazz);
RedkaleClassLoader.putReflectionPublicConstructors(clazz, clzName); RedkaleClassLoader.putReflectionPublicConstructors(clazz, clzName);
AnnotationVisitor av2 = AnnotationVisitor av2 =
av1.visitAnnotation(null, org.redkale.asm.Type.getDescriptor(DynForMessaged.class)); av1.visitAnnotation(null, org.redkale.asm.Type.getDescriptor(DynForMessaged.class));

View File

@@ -459,7 +459,7 @@ public class MessageModuleEngine extends ModuleEngine {
try { try {
String clzName = innerFullName.replace('/', '.'); String clzName = innerFullName.replace('/', '.');
Class<? extends MessageConsumer> clazz = classLoader.loadClass(clzName, bytes); Class<? extends MessageConsumer> clazz = classLoader.loadClass(clzName, bytes);
RedkaleClassLoader.putDynClass(clzName, bytes, clazz); classLoader.putDynClass(clzName, bytes, clazz);
RedkaleClassLoader.putReflectionPublicConstructors(clazz, clzName); RedkaleClassLoader.putReflectionPublicConstructors(clazz, clzName);
MessageConsumer consumer = (MessageConsumer) clazz.getConstructors()[0].newInstance(service); MessageConsumer consumer = (MessageConsumer) clazz.getConstructors()[0].newInstance(service);
addMessageConsumer(consumer); addMessageConsumer(consumer);

View File

@@ -108,10 +108,10 @@ public class HttpContext extends Context {
final String handlerDesc = Type.getDescriptor(CompletionHandler.class); final String handlerDesc = Type.getDescriptor(CompletionHandler.class);
final String newDynName = "org/redkaledyn/http/handler/_DynHttpAsyncHandler__" final String newDynName = "org/redkaledyn/http/handler/_DynHttpAsyncHandler__"
+ handlerClass.getName().replace('.', '/').replace('$', '_'); + handlerClass.getName().replace('.', '/').replace('$', '_');
RedkaleClassLoader loader = RedkaleClassLoader.getRedkaleClassLoader(); RedkaleClassLoader classLoader = RedkaleClassLoader.getRedkaleClassLoader();
try { try {
Class clz = RedkaleClassLoader.findDynClass(newDynName.replace('/', '.')); Class clz = classLoader.findDynClass(newDynName.replace('/', '.'));
Class newHandlerClazz = clz == null ? loader.loadClass(newDynName.replace('/', '.')) : clz; Class newHandlerClazz = clz == null ? classLoader.loadClass(newDynName.replace('/', '.')) : clz;
return Creator.create(newHandlerClazz); return Creator.create(newHandlerClazz);
} catch (Throwable ex) { } catch (Throwable ex) {
// do nothing // do nothing
@@ -213,8 +213,8 @@ public class HttpContext extends Context {
} }
cw.visitEnd(); cw.visitEnd();
byte[] bytes = cw.toByteArray(); byte[] bytes = cw.toByteArray();
Class<CompletionHandler> newClazz = loader.loadClass(newDynName.replace('/', '.'), bytes); Class<CompletionHandler> newClazz = classLoader.loadClass(newDynName.replace('/', '.'), bytes);
RedkaleClassLoader.putDynClass(newDynName.replace('/', '.'), bytes, newClazz); classLoader.putDynClass(newDynName.replace('/', '.'), bytes, newClazz);
return (Creator<H>) Creator.create(newClazz); return (Creator<H>) Creator.create(newClazz);
} }

View File

@@ -274,7 +274,7 @@ public class HttpServer extends Server<String, HttpContext, HttpRequest, HttpRes
* @return RestServlet * @return RestServlet
*/ */
public <S extends WebSocket, T extends WebSocketServlet> T addRestWebSocketServlet( public <S extends WebSocket, T extends WebSocketServlet> T addRestWebSocketServlet(
final ClassLoader classLoader, final RedkaleClassLoader classLoader,
final Class<S> webSocketType, final Class<S> webSocketType,
final MessageAgent messageAgent, final MessageAgent messageAgent,
final String prefix, final String prefix,
@@ -300,7 +300,7 @@ public class HttpServer extends Server<String, HttpContext, HttpRequest, HttpRes
* @return RestServlet * @return RestServlet
*/ */
public <S extends Service, T extends HttpServlet> T addRestServlet( public <S extends Service, T extends HttpServlet> T addRestServlet(
final ClassLoader classLoader, final RedkaleClassLoader classLoader,
final S service, final S service,
final Class userType, final Class userType,
final Class<T> baseServletType, final Class<T> baseServletType,
@@ -324,7 +324,7 @@ public class HttpServer extends Server<String, HttpContext, HttpRequest, HttpRes
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <S extends Service, T extends HttpServlet> T addRestServlet( public <S extends Service, T extends HttpServlet> T addRestServlet(
final ClassLoader classLoader, final RedkaleClassLoader classLoader,
final String name, final String name,
final S service, final S service,
final Class userType, final Class userType,

View File

@@ -560,12 +560,12 @@ public class HttpServlet extends Servlet<HttpContext, HttpRequest, HttpResponse>
for (Class cz : method.getParameterTypes()) { for (Class cz : method.getParameterTypes()) {
tmpps.append("__").append(cz.getName().replace('.', '_')); tmpps.append("__").append(cz.getName().replace('.', '_'));
} }
RedkaleClassLoader loader = RedkaleClassLoader.getRedkaleClassLoader(); RedkaleClassLoader classLoader = RedkaleClassLoader.getRedkaleClassLoader();
final String newDynName = "org/redkaledyn/http/servlet/action/_DynHttpActionServlet__" final String newDynName = "org/redkaledyn/http/servlet/action/_DynHttpActionServlet__"
+ this.getClass().getName().replace('.', '_').replace('$', '_') + "__" + method.getName() + tmpps; + this.getClass().getName().replace('.', '_').replace('$', '_') + "__" + method.getName() + tmpps;
try { try {
Class clz = RedkaleClassLoader.findDynClass(newDynName.replace('/', '.')); Class clz = classLoader.findDynClass(newDynName.replace('/', '.'));
Class newClazz = clz == null ? loader.loadClass(newDynName.replace('/', '.')) : clz; Class newClazz = clz == null ? classLoader.loadClass(newDynName.replace('/', '.')) : clz;
HttpServlet instance = HttpServlet instance =
(HttpServlet) newClazz.getDeclaredConstructor().newInstance(); (HttpServlet) newClazz.getDeclaredConstructor().newInstance();
instance.getClass().getField("_factServlet").set(instance, this); instance.getClass().getField("_factServlet").set(instance, this);
@@ -626,8 +626,8 @@ public class HttpServlet extends Servlet<HttpContext, HttpRequest, HttpResponse>
cw.visitEnd(); cw.visitEnd();
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
byte[] bytes = cw.toByteArray(); byte[] bytes = cw.toByteArray();
Class<?> newClazz = loader.loadClass(newDynName.replace('/', '.'), bytes); Class<?> newClazz = classLoader.loadClass(newDynName.replace('/', '.'), bytes);
RedkaleClassLoader.putDynClass(newDynName.replace('/', '.'), bytes, newClazz); classLoader.putDynClass(newDynName.replace('/', '.'), bytes, newClazz);
RedkaleClassLoader.putReflectionDeclaredConstructors(newClazz, newDynName.replace('/', '.')); RedkaleClassLoader.putReflectionDeclaredConstructors(newClazz, newDynName.replace('/', '.'));
try { try {
HttpServlet instance = HttpServlet instance =

View File

@@ -307,7 +307,7 @@ public final class Rest {
} }
public static <T extends WebSocketServlet> T createRestWebSocketServlet( public static <T extends WebSocketServlet> T createRestWebSocketServlet(
final ClassLoader classLoader, final Class<? extends WebSocket> webSocketType, MessageAgent messageAgent) { RedkaleClassLoader classLoader, Class<? extends WebSocket> webSocketType, MessageAgent messageAgent) {
if (webSocketType == null) { if (webSocketType == null) {
throw new RestException("Rest WebSocket Class is null on createRestWebSocketServlet"); throw new RestException("Rest WebSocket Class is null on createRestWebSocketServlet");
} }
@@ -348,7 +348,6 @@ public final class Rest {
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
final Set<Field> resourcesFieldSet = new LinkedHashSet<>(); final Set<Field> resourcesFieldSet = new LinkedHashSet<>();
final ClassLoader loader = classLoader == null ? Thread.currentThread().getContextClassLoader() : classLoader;
final Set<String> resourcesFieldNameSet = new HashSet<>(); final Set<String> resourcesFieldNameSet = new HashSet<>();
Class clzz = webSocketType; Class clzz = webSocketType;
do { do {
@@ -456,9 +455,9 @@ public final class Rest {
final String newDynConsumerSimpleName = "_DynRestOnMessageConsumer"; final String newDynConsumerSimpleName = "_DynRestOnMessageConsumer";
final String newDynConsumerFullName = newDynName + "$" + newDynConsumerSimpleName; final String newDynConsumerFullName = newDynName + "$" + newDynConsumerSimpleName;
try { try {
Class clz = RedkaleClassLoader.findDynClass(newDynName.replace('/', '.')); Class clz = classLoader.findDynClass(newDynName.replace('/', '.'));
if (clz == null) { if (clz == null) {
clz = loader.loadClass(newDynName.replace('/', '.')); clz = classLoader.loadClass(newDynName.replace('/', '.'));
} }
T servlet = (T) clz.getDeclaredConstructor().newInstance(); T servlet = (T) clz.getDeclaredConstructor().newInstance();
Map<String, Annotation[]> msgclassToAnnotations = new HashMap<>(); Map<String, Annotation[]> msgclassToAnnotations = new HashMap<>();
@@ -660,7 +659,6 @@ public final class Rest {
mv.visitEnd(); mv.visitEnd();
} }
RedkaleClassLoader newLoader = RedkaleClassLoader.getRedkaleClassLoader(loader);
Map<String, Annotation[]> msgclassToAnnotations = new HashMap<>(); Map<String, Annotation[]> msgclassToAnnotations = new HashMap<>();
for (int i = 0; i < messageMethods.size(); i++) { // _DyncXXXWebSocketMessage 子消息List for (int i = 0; i < messageMethods.size(); i++) { // _DyncXXXWebSocketMessage 子消息List
final Method method = messageMethods.get(i); final Method method = messageMethods.get(i);
@@ -901,8 +899,8 @@ public final class Rest {
} }
cw2.visitEnd(); cw2.visitEnd();
byte[] bytes = cw2.toByteArray(); byte[] bytes = cw2.toByteArray();
Class cz = newLoader.loadClass((newDynSuperMessageFullName).replace('/', '.'), bytes); Class cz = classLoader.loadClass((newDynSuperMessageFullName).replace('/', '.'), bytes);
RedkaleClassLoader.putDynClass((newDynSuperMessageFullName).replace('/', '.'), bytes, cz); classLoader.putDynClass((newDynSuperMessageFullName).replace('/', '.'), bytes, cz);
} }
if (wildcardMethod == null) { // _DynXXXWebSocketMessage class if (wildcardMethod == null) { // _DynXXXWebSocketMessage class
@@ -959,8 +957,8 @@ public final class Rest {
} }
cw2.visitEnd(); cw2.visitEnd();
byte[] bytes = cw2.toByteArray(); byte[] bytes = cw2.toByteArray();
Class cz = newLoader.loadClass(newDynMessageFullName.replace('/', '.'), bytes); Class cz = classLoader.loadClass(newDynMessageFullName.replace('/', '.'), bytes);
RedkaleClassLoader.putDynClass(newDynMessageFullName.replace('/', '.'), bytes, cz); classLoader.putDynClass(newDynMessageFullName.replace('/', '.'), bytes, cz);
} }
{ // _DynXXXWebSocket class { // _DynXXXWebSocket class
@@ -1002,8 +1000,8 @@ public final class Rest {
} }
cw2.visitEnd(); cw2.visitEnd();
byte[] bytes = cw2.toByteArray(); byte[] bytes = cw2.toByteArray();
Class cz = newLoader.loadClass(newDynWebSokcetFullName.replace('/', '.'), bytes); Class cz = classLoader.loadClass(newDynWebSokcetFullName.replace('/', '.'), bytes);
RedkaleClassLoader.putDynClass(newDynWebSokcetFullName.replace('/', '.'), bytes, cz); classLoader.putDynClass(newDynWebSokcetFullName.replace('/', '.'), bytes, cz);
} }
{ // _DynRestOnMessageConsumer class { // _DynRestOnMessageConsumer class
@@ -1110,14 +1108,14 @@ public final class Rest {
} }
cw2.visitEnd(); cw2.visitEnd();
byte[] bytes = cw2.toByteArray(); byte[] bytes = cw2.toByteArray();
Class cz = newLoader.loadClass(newDynConsumerFullName.replace('/', '.'), bytes); Class cz = classLoader.loadClass(newDynConsumerFullName.replace('/', '.'), bytes);
RedkaleClassLoader.putDynClass(newDynConsumerFullName.replace('/', '.'), bytes, cz); classLoader.putDynClass(newDynConsumerFullName.replace('/', '.'), bytes, cz);
} }
cw.visitEnd(); cw.visitEnd();
byte[] bytes = cw.toByteArray(); byte[] bytes = cw.toByteArray();
Class<?> newClazz = newLoader.loadClass(newDynName.replace('/', '.'), bytes); Class<?> newClazz = classLoader.loadClass(newDynName.replace('/', '.'), bytes);
RedkaleClassLoader.putDynClass(newDynName.replace('/', '.'), bytes, newClazz); classLoader.putDynClass(newDynName.replace('/', '.'), bytes, newClazz);
RedkaleClassLoader.putReflectionDeclaredConstructors(newClazz, newDynName.replace('/', '.')); RedkaleClassLoader.putReflectionDeclaredConstructors(newClazz, newDynName.replace('/', '.'));
JsonFactory.root().loadDecoder(newClazz.getAnnotation(RestDyn.class).types()[2]); // 固定Message类 JsonFactory.root().loadDecoder(newClazz.getAnnotation(RestDyn.class).types()[2]); // 固定Message类
@@ -1152,7 +1150,7 @@ public final class Rest {
} }
public static <T extends HttpServlet> T createRestServlet( public static <T extends HttpServlet> T createRestServlet(
final ClassLoader classLoader, final RedkaleClassLoader classLoader,
final Class userType0, final Class userType0,
final Class<T> baseServletType, final Class<T> baseServletType,
final Class<? extends Service> serviceType, final Class<? extends Service> serviceType,
@@ -1277,7 +1275,6 @@ public final class Rest {
final boolean serRpcOnly = controller != null && controller.rpcOnly(); final boolean serRpcOnly = controller != null && controller.rpcOnly();
final Boolean parentNonBlocking = parentNon0; final Boolean parentNonBlocking = parentNon0;
ClassLoader loader = classLoader == null ? Thread.currentThread().getContextClassLoader() : classLoader;
String stname = serviceType.getSimpleName(); String stname = serviceType.getSimpleName();
if (stname.startsWith("Service")) { // 类似ServiceWatchService这样的类保留第一个Service字样 if (stname.startsWith("Service")) { // 类似ServiceWatchService这样的类保留第一个Service字样
stname = "Service" + stname.substring("Service".length()).replaceAll("Service.*$", ""); stname = "Service" + stname.substring("Service".length()).replaceAll("Service.*$", "");
@@ -1304,9 +1301,9 @@ public final class Rest {
+ (namePostfix.isEmpty() ? "" : ("_" + namePostfix) + "DynServlet"); + (namePostfix.isEmpty() ? "" : ("_" + namePostfix) + "DynServlet");
try { try {
Class newClazz = RedkaleClassLoader.findDynClass(newDynName.replace('/', '.')); Class newClazz = classLoader.findClass(newDynName.replace('/', '.'));
if (newClazz == null) { if (newClazz == null) {
newClazz = loader.loadClass(newDynName.replace('/', '.')); newClazz = classLoader.loadClass(newDynName.replace('/', '.'));
} }
T obj = (T) newClazz.getDeclaredConstructor().newInstance(); T obj = (T) newClazz.getDeclaredConstructor().newInstance();
@@ -1989,7 +1986,6 @@ public final class Rest {
return null; // 没有可HttpMapping的方法 return null; // 没有可HttpMapping的方法
} }
Collections.sort(entrys); Collections.sort(entrys);
RedkaleClassLoader newLoader = RedkaleClassLoader.getRedkaleClassLoader(loader);
final int moduleid = controller == null ? 0 : controller.moduleid(); final int moduleid = controller == null ? 0 : controller.moduleid();
{ // 注入 @WebServlet 注解 { // 注入 @WebServlet 注解
String urlpath = ""; String urlpath = "";
@@ -4072,7 +4068,7 @@ public final class Rest {
} }
cw2.visitEnd(); cw2.visitEnd();
byte[] bytes = cw2.toByteArray(); byte[] bytes = cw2.toByteArray();
newLoader.addDynClass((newDynName + "$" + entry.newActionClassName).replace('/', '.'), bytes); classLoader.addDynClass((newDynName + "$" + entry.newActionClassName).replace('/', '.'), bytes);
innerClassBytesMap.put((newDynName + "$" + entry.newActionClassName).replace('/', '.'), bytes); innerClassBytesMap.put((newDynName + "$" + entry.newActionClassName).replace('/', '.'), bytes);
} }
} // end for each } // end for each
@@ -4236,18 +4232,18 @@ public final class Rest {
cw.visitEnd(); cw.visitEnd();
byte[] bytes = cw.toByteArray(); byte[] bytes = cw.toByteArray();
newLoader.addDynClass(newDynName.replace('/', '.'), bytes); classLoader.addDynClass(newDynName.replace('/', '.'), bytes);
try { try {
Class<?> newClazz = newLoader.findClass(newDynName.replace('/', '.')); Class<?> newClazz = classLoader.findClass(newDynName.replace('/', '.'));
innerClassBytesMap.forEach((n, bs) -> { innerClassBytesMap.forEach((n, bs) -> {
try { try {
RedkaleClassLoader.putDynClass(n, bs, newLoader.findClass(n)); classLoader.putDynClass(n, bs, classLoader.findClass(n));
RedkaleClassLoader.putReflectionClass(n); RedkaleClassLoader.putReflectionClass(n);
} catch (Exception e) { } catch (Exception e) {
throw new RestException(e); throw new RestException(e);
} }
}); });
RedkaleClassLoader.putDynClass(newDynName.replace('/', '.'), bytes, newClazz); classLoader.putDynClass(newDynName.replace('/', '.'), bytes, newClazz);
RedkaleClassLoader.putReflectionDeclaredConstructors(newClazz, newDynName.replace('/', '.')); RedkaleClassLoader.putReflectionDeclaredConstructors(newClazz, newDynName.replace('/', '.'));
for (java.lang.reflect.Type t : retvalTypes) { for (java.lang.reflect.Type t : retvalTypes) {
JsonFactory.root().loadEncoder(t); JsonFactory.root().loadEncoder(t);

View File

@@ -483,7 +483,7 @@ public abstract class Sncp {
* 创建Service的本地模式Class * 创建Service的本地模式Class
* *
* @param <T> Service子类 * @param <T> Service子类
* @param dynLoader DynBytesClassLoader * @param classLoader DynBytesClassLoader
* @param name 资源名 * @param name 资源名
* @param serviceImplClass Service类 * @param serviceImplClass Service类
* @param methodBoost 方法扩展 * @param methodBoost 方法扩展
@@ -491,7 +491,7 @@ public abstract class Sncp {
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
protected static <T extends Service> Class<? extends T> createLocalServiceClass( protected static <T extends Service> Class<? extends T> createLocalServiceClass(
RedkaleClassLoader dynLoader, RedkaleClassLoader classLoader,
final String name, final String name,
final Class<T> serviceImplClass, final Class<T> serviceImplClass,
final AsmMethodBoost methodBoost) { final AsmMethodBoost methodBoost) {
@@ -527,17 +527,16 @@ public abstract class Sncp {
} }
newDynName += "_" + (normal ? name : hash(name)); newDynName += "_" + (normal ? name : hash(name));
} }
if (Utility.inNativeImage() || methodBoost == null) { // 加强动态时不能重复加载 // if (Utility.inNativeImage() || methodBoost == null) { // 加强动态时不能重复加载
try { try {
final String newDynClass = newDynName.replace('/', '.'); final String newDynClass = newDynName.replace('/', '.');
Class clz = RedkaleClassLoader.findDynClass(newDynClass); return (Class<T>) classLoader.findClass(newDynClass);
return (Class<T>) (clz == null ? dynLoader.loadClass(newDynClass) : clz); } catch (ClassNotFoundException e) {
} catch (ClassNotFoundException e) { // do nothing
// do nothing } catch (Throwable t) {
} catch (Throwable t) { t.printStackTrace();
t.printStackTrace();
}
} }
// }
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
ClassWriter cw = new ClassWriter(COMPUTE_FRAMES); ClassWriter cw = new ClassWriter(COMPUTE_FRAMES);
FieldVisitor fv; FieldVisitor fv;
@@ -584,8 +583,8 @@ public abstract class Sncp {
fv.visitEnd(); fv.visitEnd();
} }
if (methodBoost != null) { if (methodBoost != null) {
createNewMethods(dynLoader, serviceImplClass, methodBoost, new HashSet<>(), cw, newDynName, supDynName); createNewMethods(classLoader, serviceImplClass, methodBoost, new HashSet<>(), cw, newDynName, supDynName);
methodBoost.doAfterMethods(dynLoader, cw, newDynName, FIELDPREFIX); methodBoost.doAfterMethods(classLoader, cw, newDynName, FIELDPREFIX);
} }
{ // 构造函数 { // 构造函数
mv = new MethodDebugVisitor(cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null)); mv = new MethodDebugVisitor(cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null));
@@ -593,7 +592,7 @@ public abstract class Sncp {
mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKESPECIAL, supDynName, "<init>", "()V", false); mv.visitMethodInsn(INVOKESPECIAL, supDynName, "<init>", "()V", false);
if (methodBoost != null) { if (methodBoost != null) {
methodBoost.doConstructorMethod(dynLoader, cw, mv, newDynName, FIELDPREFIX, false); methodBoost.doConstructorMethod(classLoader, cw, mv, newDynName, FIELDPREFIX, false);
} }
mv.visitInsn(RETURN); mv.visitInsn(RETURN);
mv.visitMaxs(1, 1); mv.visitMaxs(1, 1);
@@ -602,9 +601,9 @@ public abstract class Sncp {
cw.visitEnd(); cw.visitEnd();
byte[] bytes = cw.toByteArray(); byte[] bytes = cw.toByteArray();
final String newDynClass = newDynName.replace('/', '.'); final String newDynClass = newDynName.replace('/', '.');
Class<?> newClazz = dynLoader.loadClass(newDynClass, bytes); Class<?> newClazz = classLoader.loadClass(newDynClass, bytes);
RedkaleClassLoader.putDynClass(newDynClass, bytes, newClazz); classLoader.putDynClass(newDynClass, bytes, newClazz);
RedkaleClassLoader.putReflectionPublicClasses(newDynClass); RedkaleClassLoader.putReflectionPublicClasses(newDynClass);
RedkaleClassLoader.putReflectionDeclaredConstructors(newClazz, newDynClass); RedkaleClassLoader.putReflectionDeclaredConstructors(newClazz, newDynClass);
try { try {
@@ -620,8 +619,9 @@ public abstract class Sncp {
} }
public static <T extends Service> T createSimpleLocalService( public static <T extends Service> T createSimpleLocalService(
Class<T> serviceImplClass, ResourceFactory resourceFactory) { RedkaleClassLoader classLoader, Class<T> serviceImplClass, ResourceFactory resourceFactory) {
return createLocalService(null, "", serviceImplClass, null, resourceFactory, null, null, null, null, null); return createLocalService(
classLoader, "", serviceImplClass, null, resourceFactory, null, null, null, null, null);
} }
private static void createNewMethods( private static void createNewMethods(
@@ -765,8 +765,7 @@ public abstract class Sncp {
final String remoteGroup, final String remoteGroup,
final AnyValue conf) { final AnyValue conf) {
try { try {
final RedkaleClassLoader dynLoader = RedkaleClassLoader.getRedkaleClassLoader(classLoader); final Class newClazz = createLocalServiceClass(classLoader, name, serviceImplClass, methodBoost);
final Class newClazz = createLocalServiceClass(dynLoader, name, serviceImplClass, methodBoost);
T service = (T) newClazz.getDeclaredConstructor().newInstance(); T service = (T) newClazz.getDeclaredConstructor().newInstance();
// -------------------------------------- // --------------------------------------
Service remoteService = null; Service remoteService = null;
@@ -818,7 +817,7 @@ public abstract class Sncp {
} }
if (methodBoost != null) { if (methodBoost != null) {
// 必须用servcie的ClassLoader 因为service是动态ClassLoader会与doMethod里的动态ClassLoader不一致 // 必须用servcie的ClassLoader 因为service是动态ClassLoader会与doMethod里的动态ClassLoader不一致
methodBoost.doInstance(dynLoader, resourceFactory, service); methodBoost.doInstance(classLoader, resourceFactory, service);
} }
return service; return service;
} catch (RuntimeException rex) { } catch (RuntimeException rex) {
@@ -829,6 +828,7 @@ public abstract class Sncp {
} }
public static <T extends Service> T createSimpleRemoteService( public static <T extends Service> T createSimpleRemoteService(
RedkaleClassLoader classLoader,
Class<T> serviceImplClass, Class<T> serviceImplClass,
ResourceFactory resourceFactory, ResourceFactory resourceFactory,
SncpRpcGroups sncpRpcGroups, SncpRpcGroups sncpRpcGroups,
@@ -841,7 +841,7 @@ public abstract class Sncp {
throw new SncpException(SncpClient.class.getSimpleName() + " is null"); throw new SncpException(SncpClient.class.getSimpleName() + " is null");
} }
return createRemoteService( return createRemoteService(
null, "", serviceImplClass, null, resourceFactory, sncpRpcGroups, client, null, group, null); classLoader, "", serviceImplClass, null, resourceFactory, sncpRpcGroups, client, null, group, null);
} }
/** /**
@@ -897,7 +897,7 @@ public abstract class Sncp {
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static <T extends Service> T createRemoteService( public static <T extends Service> T createRemoteService(
final ClassLoader classLoader, final RedkaleClassLoader classLoader,
final String name, final String name,
final Class<T> serviceTypeOrImplClass, final Class<T> serviceTypeOrImplClass,
final AsmMethodBoost methodBoost, final AsmMethodBoost methodBoost,
@@ -938,7 +938,6 @@ public abstract class Sncp {
final String sncpInfoDesc = Type.getDescriptor(SncpRemoteInfo.class); final String sncpInfoDesc = Type.getDescriptor(SncpRemoteInfo.class);
final String sncpDynDesc = Type.getDescriptor(SncpDyn.class); final String sncpDynDesc = Type.getDescriptor(SncpDyn.class);
final String anyValueDesc = Type.getDescriptor(AnyValue.class); final String anyValueDesc = Type.getDescriptor(AnyValue.class);
final RedkaleClassLoader dynLoader = RedkaleClassLoader.getRedkaleClassLoader(classLoader);
String newDynName = "org/redkaledyn/service/remote/_DynRemoteService__" String newDynName = "org/redkaledyn/service/remote/_DynRemoteService__"
+ serviceTypeOrImplClass.getName().replace('.', '_').replace('$', '_'); + serviceTypeOrImplClass.getName().replace('.', '_').replace('$', '_');
if (!name.isEmpty()) { if (!name.isEmpty()) {
@@ -955,8 +954,8 @@ public abstract class Sncp {
} }
try { try {
final String newDynClass = newDynName.replace('/', '.'); final String newDynClass = newDynName.replace('/', '.');
Class clz = RedkaleClassLoader.findDynClass(newDynClass); Class clz = classLoader.findDynClass(newDynClass);
Class newClazz = clz == null ? dynLoader.loadClass(newDynClass) : clz; Class newClazz = clz == null ? classLoader.loadClass(newDynClass) : clz;
T service = (T) newClazz.getDeclaredConstructor().newInstance(); T service = (T) newClazz.getDeclaredConstructor().newInstance();
{ {
Field c = newClazz.getDeclaredField(FIELDPREFIX + "_conf"); Field c = newClazz.getDeclaredField(FIELDPREFIX + "_conf");
@@ -975,7 +974,7 @@ public abstract class Sncp {
} }
if (methodBoost != null) { if (methodBoost != null) {
// 必须用servcie的ClassLoader 因为service是动态ClassLoader会与doMethod里的动态ClassLoader不一致 // 必须用servcie的ClassLoader 因为service是动态ClassLoader会与doMethod里的动态ClassLoader不一致
methodBoost.doInstance(dynLoader, resourceFactory, service); methodBoost.doInstance(classLoader, resourceFactory, service);
} }
return service; return service;
} catch (Throwable ex) { } catch (Throwable ex) {
@@ -1087,7 +1086,7 @@ public abstract class Sncp {
if (methodBoost != null) { if (methodBoost != null) {
List<Class<? extends Annotation>> filterAnns = methodBoost.filterMethodAnnotations(method); List<Class<? extends Annotation>> filterAnns = methodBoost.filterMethodAnnotations(method);
newMethod = methodBoost.doMethod( newMethod = methodBoost.doMethod(
dynLoader, cw, serviceTypeOrImplClass, newDynName, FIELDPREFIX, filterAnns, method, null); classLoader, cw, serviceTypeOrImplClass, newDynName, FIELDPREFIX, filterAnns, method, null);
} }
if (newMethod != null) { if (newMethod != null) {
acc = newMethod.getMethodAccs(); acc = newMethod.getMethodAccs();
@@ -1214,8 +1213,8 @@ public abstract class Sncp {
mv.visitEnd(); mv.visitEnd();
} }
if (methodBoost != null) { if (methodBoost != null) {
createNewMethods(dynLoader, serviceTypeOrImplClass, methodBoost, methodKeys, cw, newDynName, supDynName); createNewMethods(classLoader, serviceTypeOrImplClass, methodBoost, methodKeys, cw, newDynName, supDynName);
methodBoost.doAfterMethods(dynLoader, cw, newDynName, FIELDPREFIX); methodBoost.doAfterMethods(classLoader, cw, newDynName, FIELDPREFIX);
} }
{ // 构造函数 { // 构造函数
mv = new MethodDebugVisitor(cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null)); mv = new MethodDebugVisitor(cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null));
@@ -1228,7 +1227,7 @@ public abstract class Sncp {
"()V", "()V",
false); false);
if (methodBoost != null) { if (methodBoost != null) {
methodBoost.doConstructorMethod(dynLoader, cw, mv, newDynName, FIELDPREFIX, true); methodBoost.doConstructorMethod(classLoader, cw, mv, newDynName, FIELDPREFIX, true);
} }
mv.visitInsn(RETURN); mv.visitInsn(RETURN);
mv.visitMaxs(1, 1); mv.visitMaxs(1, 1);
@@ -1237,8 +1236,8 @@ public abstract class Sncp {
cw.visitEnd(); cw.visitEnd();
byte[] bytes = cw.toByteArray(); byte[] bytes = cw.toByteArray();
final String newDynClass = newDynName.replace('/', '.'); final String newDynClass = newDynName.replace('/', '.');
Class<?> newClazz = dynLoader.loadClass(newDynClass, bytes); Class<?> newClazz = classLoader.loadClass(newDynClass, bytes);
RedkaleClassLoader.putDynClass(newDynClass, bytes, newClazz); classLoader.putDynClass(newDynClass, bytes, newClazz);
RedkaleClassLoader.putReflectionPublicConstructors(newClazz, newDynClass); RedkaleClassLoader.putReflectionPublicConstructors(newClazz, newDynClass);
RedkaleClassLoader.putReflectionDeclaredConstructors(newClazz, newDynClass); RedkaleClassLoader.putReflectionDeclaredConstructors(newClazz, newDynClass);
try { try {
@@ -1263,7 +1262,7 @@ public abstract class Sncp {
} }
if (methodBoost != null) { if (methodBoost != null) {
// 必须用servcie的ClassLoader 因为service是动态ClassLoader会与doMethod里的动态ClassLoader不一致 // 必须用servcie的ClassLoader 因为service是动态ClassLoader会与doMethod里的动态ClassLoader不一致
methodBoost.doInstance(dynLoader, resourceFactory, service); methodBoost.doInstance(classLoader, resourceFactory, service);
} }
return service; return service;
} catch (Exception ex) { } catch (Exception ex) {

View File

@@ -356,11 +356,11 @@ public abstract class SncpActionServlet extends SncpServlet {
final boolean boolReturnTypeFuture = Future.class.isAssignableFrom(method.getReturnType()); final boolean boolReturnTypeFuture = Future.class.isAssignableFrom(method.getReturnType());
final String newDynName = "org/redkaledyn/sncp/servlet/action/_DynSncpActionServlet__" final String newDynName = "org/redkaledyn/sncp/servlet/action/_DynSncpActionServlet__"
+ resourceType.getSimpleName() + "_" + method.getName() + "_" + actionid; + resourceType.getSimpleName() + "_" + method.getName() + "_" + actionid;
RedkaleClassLoader loader = RedkaleClassLoader.getRedkaleClassLoader(); RedkaleClassLoader classLoader = RedkaleClassLoader.getRedkaleClassLoader();
Class<?> newClazz = null; Class<?> newClazz = null;
try { try {
Class clz = RedkaleClassLoader.findDynClass(newDynName.replace('/', '.')); Class clz = classLoader.findDynClass(newDynName.replace('/', '.'));
newClazz = clz == null ? loader.loadClass(newDynName.replace('/', '.')) : clz; newClazz = clz == null ? classLoader.loadClass(newDynName.replace('/', '.')) : clz;
} catch (Throwable ex) { } catch (Throwable ex) {
// do nothing // do nothing
} }
@@ -371,8 +371,7 @@ public abstract class SncpActionServlet extends SncpServlet {
TypeToken.getGenericType(method.getGenericReturnType(), serviceClass); TypeToken.getGenericType(method.getGenericReturnType(), serviceClass);
final Class[] paramClasses = method.getParameterTypes(); final Class[] paramClasses = method.getParameterTypes();
java.lang.reflect.Type paramComposeBeanType0 = SncpRemoteAction.createParamComposeBeanType( java.lang.reflect.Type paramComposeBeanType0 = SncpRemoteAction.createParamComposeBeanType(classLoader, serviceImplClass, method, actionid, originalParamTypes, paramClasses);
loader, serviceImplClass, method, actionid, originalParamTypes, paramClasses);
if (paramComposeBeanType0 != null && paramComposeBeanType0 == originalParamTypes[0]) { if (paramComposeBeanType0 != null && paramComposeBeanType0 == originalParamTypes[0]) {
paramComposeBeanType0 = null; paramComposeBeanType0 = null;
} }
@@ -712,8 +711,8 @@ public abstract class SncpActionServlet extends SncpServlet {
cw.visitEnd(); cw.visitEnd();
byte[] bytes = cw.toByteArray(); byte[] bytes = cw.toByteArray();
newClazz = loader.loadClass(newDynName.replace('/', '.'), bytes); newClazz = classLoader.loadClass(newDynName.replace('/', '.'), bytes);
RedkaleClassLoader.putDynClass(newDynName.replace('/', '.'), bytes, newClazz); classLoader.putDynClass(newDynName.replace('/', '.'), bytes, newClazz);
RedkaleClassLoader.putReflectionDeclaredConstructors(newClazz, newDynName.replace('/', '.')); RedkaleClassLoader.putReflectionDeclaredConstructors(newClazz, newDynName.replace('/', '.'));
try { try {

View File

@@ -54,10 +54,10 @@ public interface SncpAsyncHandler<V, A> extends CompletionHandler<V, A> {
final String realHandlerDesc = Type.getDescriptor(CompletionHandler.class); final String realHandlerDesc = Type.getDescriptor(CompletionHandler.class);
final String newDynName = "org/redkaledyn/sncp/handler/_Dyn" + sncpHandlerClass.getSimpleName() final String newDynName = "org/redkaledyn/sncp/handler/_Dyn" + sncpHandlerClass.getSimpleName()
+ "__" + handlerClass.getName().replace('.', '/').replace('$', '_'); + "__" + handlerClass.getName().replace('.', '/').replace('$', '_');
RedkaleClassLoader loader = RedkaleClassLoader.getRedkaleClassLoader(); RedkaleClassLoader classLoader = RedkaleClassLoader.getRedkaleClassLoader();
try { try {
Class clz = RedkaleClassLoader.findDynClass(newDynName.replace('/', '.')); Class clz = classLoader.findDynClass(newDynName.replace('/', '.'));
Class newHandlerClazz = clz == null ? loader.loadClass(newDynName.replace('/', '.')) : clz; Class newHandlerClazz = clz == null ? classLoader.loadClass(newDynName.replace('/', '.')) : clz;
return (Creator<SncpAsyncHandler>) Creator.create(newHandlerClazz); return (Creator<SncpAsyncHandler>) Creator.create(newHandlerClazz);
} catch (Throwable ex) { } catch (Throwable ex) {
// do nothing // do nothing
@@ -206,8 +206,8 @@ public interface SncpAsyncHandler<V, A> extends CompletionHandler<V, A> {
} }
cw.visitEnd(); cw.visitEnd();
byte[] bytes = cw.toByteArray(); byte[] bytes = cw.toByteArray();
Class newClazz = loader.loadClass(newDynName.replace('/', '.'), bytes); Class newClazz = classLoader.loadClass(newDynName.replace('/', '.'), bytes);
RedkaleClassLoader.putDynClass(newDynName.replace('/', '.'), bytes, newClazz); classLoader.putDynClass(newDynName.replace('/', '.'), bytes, newClazz);
return (Creator<SncpAsyncHandler>) Creator.create(newClazz); return (Creator<SncpAsyncHandler>) Creator.create(newClazz);
}) })
.create(factHandler); .create(factHandler);

View File

@@ -249,7 +249,7 @@ public final class SncpRemoteAction {
} }
public static Type createParamComposeBeanType( public static Type createParamComposeBeanType(
RedkaleClassLoader loader, RedkaleClassLoader classLoader,
Class resourceType, Class resourceType,
Method method, Method method,
Uint128 actionid, Uint128 actionid,
@@ -273,8 +273,8 @@ public final class SncpRemoteAction {
final String newDynName = "org/redkaledyn/sncp/servlet/action/_DynSncpActionParamBean_" final String newDynName = "org/redkaledyn/sncp/servlet/action/_DynSncpActionParamBean_"
+ resourceType.getSimpleName() + "_" + method.getName() + "_" + actionid; + resourceType.getSimpleName() + "_" + method.getName() + "_" + actionid;
try { try {
Class clz = RedkaleClassLoader.findDynClass(newDynName.replace('/', '.')); Class clz = classLoader.findDynClass(newDynName.replace('/', '.'));
Class<?> newClazz = clz == null ? loader.loadClass(newDynName.replace('/', '.')) : clz; Class<?> newClazz = clz == null ? classLoader.loadClass(newDynName.replace('/', '.')) : clz;
return newClazz; return newClazz;
} catch (Throwable ex) { } catch (Throwable ex) {
// do nothing // do nothing
@@ -327,8 +327,8 @@ public final class SncpRemoteAction {
cw.visitEnd(); cw.visitEnd();
byte[] bytes = cw.toByteArray(); byte[] bytes = cw.toByteArray();
Class newClazz = loader.loadClass(newDynName.replace('/', '.'), bytes); Class newClazz = classLoader.loadClass(newDynName.replace('/', '.'), bytes);
RedkaleClassLoader.putDynClass(newDynName.replace('/', '.'), bytes, newClazz); classLoader.putDynClass(newDynName.replace('/', '.'), bytes, newClazz);
RedkaleClassLoader.putReflectionDeclaredConstructors(newClazz, newDynName.replace('/', '.')); RedkaleClassLoader.putReflectionDeclaredConstructors(newClazz, newDynName.replace('/', '.'));
Creator.load(newClazz, 1); // 只一个Object[]参数 Creator.load(newClazz, 1); // 只一个Object[]参数
ProtobufFactory.root().loadDecoder(newClazz); ProtobufFactory.root().loadDecoder(newClazz);

View File

@@ -80,12 +80,12 @@ public abstract class EntityFullFunc<T> {
final String objectDesc = Type.getDescriptor(Object.class); final String objectDesc = Type.getDescriptor(Object.class);
final String serisDesc = Type.getDescriptor(Serializable[].class); final String serisDesc = Type.getDescriptor(Serializable[].class);
RedkaleClassLoader loader = RedkaleClassLoader.getRedkaleClassLoader(); RedkaleClassLoader classLoader = RedkaleClassLoader.getRedkaleClassLoader();
final String newDynName = "org/redkaledyn/source/_Dyn" + EntityFullFunc.class.getSimpleName() + "__" final String newDynName = "org/redkaledyn/source/_Dyn" + EntityFullFunc.class.getSimpleName() + "__"
+ entityType.getName().replace('.', '_').replace('$', '_'); + entityType.getName().replace('.', '_').replace('$', '_');
try { try {
Class clz = RedkaleClassLoader.findDynClass(newDynName.replace('/', '.')); Class clz = classLoader.findDynClass(newDynName.replace('/', '.'));
return (EntityFullFunc) (clz == null ? loader.loadClass(newDynName.replace('/', '.')) : clz) return (EntityFullFunc) (clz == null ? classLoader.loadClass(newDynName.replace('/', '.')) : clz)
.getConstructor(Class.class, Creator.class, Attribute[].class) .getConstructor(Class.class, Creator.class, Attribute[].class)
.newInstance(entityType, creator, attrs); .newInstance(entityType, creator, attrs);
} catch (Throwable ex) { } catch (Throwable ex) {
@@ -793,8 +793,8 @@ public abstract class EntityFullFunc<T> {
cw.visitEnd(); cw.visitEnd();
byte[] bytes = cw.toByteArray(); byte[] bytes = cw.toByteArray();
Class<EntityFullFunc> newClazz = loader.loadClass(newDynName.replace('/', '.'), bytes); Class<EntityFullFunc> newClazz = classLoader.loadClass(newDynName.replace('/', '.'), bytes);
RedkaleClassLoader.putDynClass(newDynName.replace('/', '.'), bytes, newClazz); classLoader.putDynClass(newDynName.replace('/', '.'), bytes, newClazz);
RedkaleClassLoader.putReflectionDeclaredConstructors(newClazz, newDynName.replace('/', '.')); RedkaleClassLoader.putReflectionDeclaredConstructors(newClazz, newDynName.replace('/', '.'));
try { try {
return newClazz.getConstructor(Class.class, Creator.class, Attribute[].class) return newClazz.getConstructor(Class.class, Creator.class, Attribute[].class)

View File

@@ -60,14 +60,14 @@ public final class DataSqlMapperBuilder {
if (!mapperType.isInterface()) { if (!mapperType.isInterface()) {
throw new SourceException(mapperType + " is not interface"); throw new SourceException(mapperType + " is not interface");
} }
final RedkaleClassLoader loader = RedkaleClassLoader.getRedkaleClassLoader(); final RedkaleClassLoader classLoader = RedkaleClassLoader.getRedkaleClassLoader();
final Class entityType = entityType(mapperType); final Class entityType = entityType(mapperType);
final String supDynName = mapperType.getName().replace('.', '/'); final String supDynName = mapperType.getName().replace('.', '/');
final String newDynName = "org/redkaledyn/source/mapper/_DynDataSqlMapper_" final String newDynName = "org/redkaledyn/source/mapper/_DynDataSqlMapper_"
+ mapperType.getName().replace('.', '_').replace('$', '_'); + mapperType.getName().replace('.', '_').replace('$', '_');
try { try {
Class clz = RedkaleClassLoader.findDynClass(newDynName.replace('/', '.')); Class clz = classLoader.findDynClass(newDynName.replace('/', '.'));
Class newClazz = clz == null ? loader.loadClass(newDynName.replace('/', '.')) : clz; Class newClazz = clz == null ? classLoader.loadClass(newDynName.replace('/', '.')) : clz;
M mapper = (M) newClazz.getDeclaredConstructor().newInstance(); M mapper = (M) newClazz.getDeclaredConstructor().newInstance();
{ // DataSqlSource { // DataSqlSource
Field c = newClazz.getDeclaredField("_source"); Field c = newClazz.getDeclaredField("_source");
@@ -363,8 +363,8 @@ public final class DataSqlMapperBuilder {
cw.visitEnd(); cw.visitEnd();
byte[] bytes = cw.toByteArray(); byte[] bytes = cw.toByteArray();
Class<?> newClazz = loader.loadClass(newDynName.replace('/', '.'), bytes); Class<?> newClazz = classLoader.loadClass(newDynName.replace('/', '.'), bytes);
RedkaleClassLoader.putDynClass(newDynName.replace('/', '.'), bytes, newClazz); classLoader.putDynClass(newDynName.replace('/', '.'), bytes, newClazz);
RedkaleClassLoader.putReflectionPublicConstructors(newClazz, newDynName.replace('/', '.')); RedkaleClassLoader.putReflectionPublicConstructors(newClazz, newDynName.replace('/', '.'));
RedkaleClassLoader.putReflectionDeclaredConstructors(newClazz, newDynName.replace('/', '.')); RedkaleClassLoader.putReflectionDeclaredConstructors(newClazz, newDynName.replace('/', '.'));
try { try {

View File

@@ -1002,9 +1002,9 @@ public interface Attribute<T, F> {
final String interDesc = Type.getDescriptor(TypeToken.typeToClass(subclass)); final String interDesc = Type.getDescriptor(TypeToken.typeToClass(subclass));
final String columnDesc = Type.getDescriptor(column); final String columnDesc = Type.getDescriptor(column);
Class realclz = TypeToken.typeToClass(subclass); Class realclz = TypeToken.typeToClass(subclass);
RedkaleClassLoader loader = RedkaleClassLoader.getRedkaleClassLoader(); RedkaleClassLoader classLoader = RedkaleClassLoader.getRedkaleClassLoader();
try { try {
loader.loadClass(realclz.getName()); classLoader.loadClass(realclz.getName());
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
// do nothing // do nothing
} }
@@ -1021,8 +1021,8 @@ public interface Attribute<T, F> {
final String newDynName = "org/redkaledyn/attribute/" + pkgname + "_Dyn" + Attribute.class.getSimpleName() final String newDynName = "org/redkaledyn/attribute/" + pkgname + "_Dyn" + Attribute.class.getSimpleName()
+ "__" + clzname + "__" + fieldkey.substring(fieldkey.indexOf('.') + 1); + "__" + clzname + "__" + fieldkey.substring(fieldkey.indexOf('.') + 1);
try { try {
Class clz = RedkaleClassLoader.findDynClass(newDynName.replace('/', '.')); Class clz = classLoader.findDynClass(newDynName.replace('/', '.'));
Attribute rs = (Attribute) (clz == null ? loader.loadClass(newDynName.replace('/', '.')) : clz) Attribute rs = (Attribute) (clz == null ? classLoader.loadClass(newDynName.replace('/', '.')) : clz)
.getDeclaredConstructor() .getDeclaredConstructor()
.newInstance(); .newInstance();
java.lang.reflect.Field _gtype = rs.getClass().getDeclaredField("_gtype"); java.lang.reflect.Field _gtype = rs.getClass().getDeclaredField("_gtype");
@@ -1233,8 +1233,8 @@ public interface Attribute<T, F> {
cw.visitEnd(); cw.visitEnd();
byte[] bytes = cw.toByteArray(); byte[] bytes = cw.toByteArray();
Class<Attribute> newClazz = loader.loadClass(newDynName.replace('/', '.'), bytes); Class<Attribute> newClazz = classLoader.loadClass(newDynName.replace('/', '.'), bytes);
RedkaleClassLoader.putDynClass(newDynName.replace('/', '.'), bytes, newClazz); classLoader.putDynClass(newDynName.replace('/', '.'), bytes, newClazz);
RedkaleClassLoader.putReflectionDeclaredConstructors(newClazz, newDynName.replace('/', '.')); RedkaleClassLoader.putReflectionDeclaredConstructors(newClazz, newDynName.replace('/', '.'));
try { try {
Attribute rs = newClazz.getDeclaredConstructor().newInstance(); Attribute rs = newClazz.getDeclaredConstructor().newInstance();

View File

@@ -679,7 +679,7 @@ public interface Copier<S, D> extends BiFunction<S, D, D> {
final String srcClassName = srcClass.getName().replace('.', '/'); final String srcClassName = srcClass.getName().replace('.', '/');
final String destDesc = Type.getDescriptor(destClass); final String destDesc = Type.getDescriptor(destClass);
final String srcDesc = Type.getDescriptor(srcClass); final String srcDesc = Type.getDescriptor(srcClass);
final RedkaleClassLoader loader = RedkaleClassLoader.getRedkaleClassLoader(); final RedkaleClassLoader classLoader = RedkaleClassLoader.getRedkaleClassLoader();
final String utilClassName = Utility.class.getName().replace('.', '/'); final String utilClassName = Utility.class.getName().replace('.', '/');
final String newDynName = "org/redkaledyn/copier/_Dyn" + Copier.class.getSimpleName() + "_" + options final String newDynName = "org/redkaledyn/copier/_Dyn" + Copier.class.getSimpleName() + "_" + options
+ "__" + srcClass.getName().replace('.', '_').replace('$', '_') + "__" + srcClass.getName().replace('.', '_').replace('$', '_')
@@ -688,8 +688,8 @@ public interface Copier<S, D> extends BiFunction<S, D, D> {
: ("__" + destClass.getName().replace('.', '_').replace('$', '_'))) : ("__" + destClass.getName().replace('.', '_').replace('$', '_')))
+ (extendInfo.length() == 0 ? "" : Utility.md5Hex(extendInfo.toString())); + (extendInfo.length() == 0 ? "" : Utility.md5Hex(extendInfo.toString()));
try { try {
Class clz = RedkaleClassLoader.findDynClass(newDynName.replace('/', '.')); Class clz = classLoader.findDynClass(newDynName.replace('/', '.'));
return (Copier) (clz == null ? loader.loadClass(newDynName.replace('/', '.')) : clz) return (Copier) (clz == null ? classLoader.loadClass(newDynName.replace('/', '.')) : clz)
.getDeclaredConstructor() .getDeclaredConstructor()
.newInstance(); .newInstance();
} catch (Throwable ex) { } catch (Throwable ex) {
@@ -1337,8 +1337,8 @@ public interface Copier<S, D> extends BiFunction<S, D, D> {
cw.visitEnd(); cw.visitEnd();
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
byte[] bytes = cw.toByteArray(); byte[] bytes = cw.toByteArray();
Class<?> newClazz = loader.loadClass(newDynName.replace('/', '.'), bytes); Class<?> newClazz = classLoader.loadClass(newDynName.replace('/', '.'), bytes);
RedkaleClassLoader.putDynClass(newDynName.replace('/', '.'), bytes, newClazz); classLoader.putDynClass(newDynName.replace('/', '.'), bytes, newClazz);
RedkaleClassLoader.putReflectionDeclaredConstructors(newClazz, newDynName.replace('/', '.')); RedkaleClassLoader.putReflectionDeclaredConstructors(newClazz, newDynName.replace('/', '.'));
try { try {
return (Copier) newClazz.getDeclaredConstructor().newInstance(); return (Copier) newClazz.getDeclaredConstructor().newInstance();

View File

@@ -292,13 +292,12 @@ public interface Creator<T> {
final String supDynName = Creator.class.getName().replace('.', '/'); final String supDynName = Creator.class.getName().replace('.', '/');
final String interName = clazz.getName().replace('.', '/'); final String interName = clazz.getName().replace('.', '/');
final String interDesc = Type.getDescriptor(clazz); final String interDesc = Type.getDescriptor(clazz);
RedkaleClassLoader loader = RedkaleClassLoader classLoader = RedkaleClassLoader.getRedkaleClassLoader();
RedkaleClassLoader.getRedkaleClassLoader();
final String newDynName = "org/redkaledyn/creator/_Dyn" + Creator.class.getSimpleName() + "__" final String newDynName = "org/redkaledyn/creator/_Dyn" + Creator.class.getSimpleName() + "__"
+ clazz.getName().replace('.', '_').replace('$', '_') + (paramCount < 0 ? "" : ("_" + paramCount)); + clazz.getName().replace('.', '_').replace('$', '_') + (paramCount < 0 ? "" : ("_" + paramCount));
try { try {
Class clz = RedkaleClassLoader.findDynClass(newDynName.replace('/', '.')); Class clz = classLoader.findDynClass(newDynName.replace('/', '.'));
return (Creator) (clz == null ? loader.loadClass(newDynName.replace('/', '.')) : clz) return (Creator) (clz == null ? classLoader.loadClass(newDynName.replace('/', '.')) : clz)
.getDeclaredConstructor() .getDeclaredConstructor()
.newInstance(); .newInstance();
} catch (Throwable ex) { } catch (Throwable ex) {
@@ -569,8 +568,8 @@ public interface Creator<T> {
byte[] bytes = cw.toByteArray(); byte[] bytes = cw.toByteArray();
try { try {
Class newClazz = loader.loadClass(newDynName.replace('/', '.'), bytes); Class newClazz = classLoader.loadClass(newDynName.replace('/', '.'), bytes);
RedkaleClassLoader.putDynClass(newDynName.replace('/', '.'), bytes, newClazz); classLoader.putDynClass(newDynName.replace('/', '.'), bytes, newClazz);
RedkaleClassLoader.putReflectionDeclaredConstructors(newClazz, newDynName.replace('/', '.')); RedkaleClassLoader.putReflectionDeclaredConstructors(newClazz, newDynName.replace('/', '.'));
return (Creator) newClazz.getDeclaredConstructor().newInstance(); return (Creator) newClazz.getDeclaredConstructor().newInstance();
} catch (Exception ex) { } catch (Exception ex) {

View File

@@ -5,12 +5,11 @@
*/ */
package org.redkale.util; package org.redkale.util;
import static org.redkale.util.Utility.hexToBin;
import java.lang.reflect.*; import java.lang.reflect.*;
import java.util.*; import java.util.*;
import java.util.concurrent.*; import java.util.concurrent.*;
import java.util.function.Function; import java.util.function.Function;
import static org.redkale.util.Utility.hexToBin;
/** /**
* Flow简单的操作 * Flow简单的操作
@@ -134,7 +133,8 @@ public abstract class Flows {
return defineClass(name, b, 0, b.length); return defineClass(name, b, 0, b.length);
} }
}.loadClass("org.redkale.util.AnonymousMonoFutureFunction", classBytes); }.loadClass("org.redkale.util.AnonymousMonoFutureFunction", classBytes);
RedkaleClassLoader.putDynClass(monoFuncClass.getName(), classBytes, monoFuncClass); RedkaleClassLoader.getRedkaleClassLoader()
.putDynClass(monoFuncClass.getName(), classBytes, monoFuncClass);
} }
RedkaleClassLoader.putReflectionDeclaredConstructors(monoFuncClass, monoFuncClass.getName()); RedkaleClassLoader.putReflectionDeclaredConstructors(monoFuncClass, monoFuncClass.getName());
reactorMonoFunction0 = (Function<Object, CompletableFuture>) reactorMonoFunction0 = (Function<Object, CompletableFuture>)
@@ -158,7 +158,8 @@ public abstract class Flows {
return defineClass(name, b, 0, b.length); return defineClass(name, b, 0, b.length);
} }
}.loadClass("org.redkale.util.AnonymousFluxFutureFunction", classBytes); }.loadClass("org.redkale.util.AnonymousFluxFutureFunction", classBytes);
RedkaleClassLoader.putDynClass(fluxFuncClass.getName(), classBytes, fluxFuncClass); RedkaleClassLoader.getRedkaleClassLoader()
.putDynClass(fluxFuncClass.getName(), classBytes, fluxFuncClass);
} }
RedkaleClassLoader.putReflectionDeclaredConstructors(fluxFuncClass, fluxFuncClass.getName()); RedkaleClassLoader.putReflectionDeclaredConstructors(fluxFuncClass, fluxFuncClass.getName());
reactorFluxFunction0 = (Function<Object, CompletableFuture>) reactorFluxFunction0 = (Function<Object, CompletableFuture>)

View File

@@ -231,12 +231,12 @@ class Inners {
} }
final String interName = clazz.getName().replace('.', '/'); final String interName = clazz.getName().replace('.', '/');
final String interDesc = org.redkale.asm.Type.getDescriptor(clazz); final String interDesc = org.redkale.asm.Type.getDescriptor(clazz);
final RedkaleClassLoader loader = RedkaleClassLoader.getRedkaleClassLoader(); final RedkaleClassLoader classLoader = RedkaleClassLoader.getRedkaleClassLoader();
final String newDynName = "org/redkaledyn/creator/_DynArrayFunction__" final String newDynName = "org/redkaledyn/creator/_DynArrayFunction__"
+ clazz.getName().replace('.', '_').replace('$', '_'); + clazz.getName().replace('.', '_').replace('$', '_');
try { try {
Class clz = RedkaleClassLoader.findDynClass(newDynName.replace('/', '.')); Class clz = classLoader.findDynClass(newDynName.replace('/', '.'));
return (IntFunction) (clz == null ? loader.loadClass(newDynName.replace('/', '.')) : clz) return (IntFunction) (clz == null ? classLoader.loadClass(newDynName.replace('/', '.')) : clz)
.getDeclaredConstructor() .getDeclaredConstructor()
.newInstance(); .newInstance();
} catch (Throwable ex) { } catch (Throwable ex) {
@@ -283,8 +283,8 @@ class Inners {
cw.visitEnd(); cw.visitEnd();
final byte[] bytes = cw.toByteArray(); final byte[] bytes = cw.toByteArray();
try { try {
Class<?> resultClazz = loader.loadClass(newDynName.replace('/', '.'), bytes); Class<?> resultClazz = classLoader.loadClass(newDynName.replace('/', '.'), bytes);
RedkaleClassLoader.putDynClass(newDynName.replace('/', '.'), bytes, resultClazz); classLoader.putDynClass(newDynName.replace('/', '.'), bytes, resultClazz);
RedkaleClassLoader.putReflectionDeclaredConstructors(resultClazz, newDynName.replace('/', '.')); RedkaleClassLoader.putReflectionDeclaredConstructors(resultClazz, newDynName.replace('/', '.'));
return (IntFunction<T[]>) resultClazz.getDeclaredConstructor().newInstance(); return (IntFunction<T[]>) resultClazz.getDeclaredConstructor().newInstance();
} catch (Throwable ex) { } catch (Throwable ex) {

View File

@@ -91,7 +91,7 @@ public interface Invoker<C, R> {
} else if (returnType == void.class) { } else if (returnType == void.class) {
returnDesc = Type.getDescriptor(Void.class); returnDesc = Type.getDescriptor(Void.class);
} }
RedkaleClassLoader loader = RedkaleClassLoader.getRedkaleClassLoader(); RedkaleClassLoader classLoader = RedkaleClassLoader.getRedkaleClassLoader();
StringBuilder sbpts = new StringBuilder(); StringBuilder sbpts = new StringBuilder();
for (Class c : method.getParameterTypes()) { for (Class c : method.getParameterTypes()) {
sbpts.append('_').append(c.getName().replace('.', '_').replace('$', '_')); sbpts.append('_').append(c.getName().replace('.', '_').replace('$', '_'));
@@ -99,8 +99,8 @@ public interface Invoker<C, R> {
final String newDynName = "org/redkaledyn/invoker/_Dyn" + Invoker.class.getSimpleName() + "_" final String newDynName = "org/redkaledyn/invoker/_Dyn" + Invoker.class.getSimpleName() + "_"
+ clazz.getName().replace('.', '_').replace('$', '_') + "_" + method.getName() + sbpts; + clazz.getName().replace('.', '_').replace('$', '_') + "_" + method.getName() + sbpts;
try { try {
Class clz = RedkaleClassLoader.findDynClass(newDynName.replace('/', '.')); Class clz = classLoader.findDynClass(newDynName.replace('/', '.'));
return (Invoker<C, T>) (clz == null ? loader.loadClass(newDynName.replace('/', '.')) : clz) return (Invoker<C, T>) (clz == null ? classLoader.loadClass(newDynName.replace('/', '.')) : clz)
.getDeclaredConstructor() .getDeclaredConstructor()
.newInstance(); .newInstance();
} catch (Throwable ex) { } catch (Throwable ex) {
@@ -208,9 +208,9 @@ public interface Invoker<C, R> {
Class<?> resultClazz = null; Class<?> resultClazz = null;
try { try {
if (resultClazz == null) { if (resultClazz == null) {
resultClazz = loader.loadClass(newDynName.replace('/', '.'), bytes); resultClazz = classLoader.loadClass(newDynName.replace('/', '.'), bytes);
} }
RedkaleClassLoader.putDynClass(newDynName.replace('/', '.'), bytes, resultClazz); classLoader.putDynClass(newDynName.replace('/', '.'), bytes, resultClazz);
RedkaleClassLoader.putReflectionDeclaredConstructors(resultClazz, newDynName.replace('/', '.')); RedkaleClassLoader.putReflectionDeclaredConstructors(resultClazz, newDynName.replace('/', '.'));
return (Invoker<C, T>) resultClazz.getDeclaredConstructor().newInstance(); return (Invoker<C, T>) resultClazz.getDeclaredConstructor().newInstance();
} catch (Exception ex) { } catch (Exception ex) {

View File

@@ -66,10 +66,14 @@ public class RedkaleClassLoader extends URLClassLoader {
"org.redkale.watch" "org.redkale.watch"
}; };
// redkale里所有使用动态字节码生成的类都需要存于此处 private final ConcurrentHashMap<String, byte[]> dynClassBytesMap = new ConcurrentHashMap<>();
private static final ConcurrentHashMap<String, byte[]> dynClassBytesMap = new ConcurrentHashMap<>();
private static final ConcurrentHashMap<String, Class> dynClassTypeMap = new ConcurrentHashMap<>(); private final ConcurrentHashMap<String, Class> dynClassTypeMap = new ConcurrentHashMap<>();
// redkale里所有使用动态字节码生成的类都需要存于此处
private static final ConcurrentHashMap<String, byte[]> allDynClassBytesMap = new ConcurrentHashMap<>();
private static final ConcurrentHashMap<String, Class> allDynClassTypeMap = new ConcurrentHashMap<>();
private static final CopyOnWriteArraySet<String> resourcePathSet = new CopyOnWriteArraySet<>(); private static final CopyOnWriteArraySet<String> resourcePathSet = new CopyOnWriteArraySet<>();
@@ -171,20 +175,30 @@ public class RedkaleClassLoader extends URLClassLoader {
} }
} }
public static byte[] putDynClass(String name, byte[] bs, Class clazz) { static void putDynClass0(String name, byte[] bs, Class clazz) {
Objects.requireNonNull(name);
Objects.requireNonNull(bs);
Objects.requireNonNull(clazz);
allDynClassTypeMap.put(name, clazz);
allDynClassBytesMap.put(name, bs);
}
public void putDynClass(String name, byte[] bs, Class clazz) {
Objects.requireNonNull(name); Objects.requireNonNull(name);
Objects.requireNonNull(bs); Objects.requireNonNull(bs);
Objects.requireNonNull(clazz); Objects.requireNonNull(clazz);
dynClassTypeMap.put(name, clazz); dynClassTypeMap.put(name, clazz);
return dynClassBytesMap.put(name, bs); dynClassBytesMap.put(name, bs);
allDynClassTypeMap.put(name, clazz);
allDynClassBytesMap.put(name, bs);
} }
public static Class findDynClass(String name) { public Class findDynClass(String name) {
return dynClassTypeMap.get(name); return dynClassTypeMap.get(name);
} }
public static void forEachDynClass(BiConsumer<String, byte[]> action) { public static void forEachDynClass(BiConsumer<String, byte[]> action) {
dynClassBytesMap.forEach(action); allDynClassBytesMap.forEach(action);
} }
public static void putReflectionClass(String className) { public static void putReflectionClass(String className) {

View File

@@ -67,13 +67,13 @@ public interface Reproduce<D, S> extends BiFunction<D, S, D> {
final String srcClassName = srcClass.getName().replace('.', '/'); final String srcClassName = srcClass.getName().replace('.', '/');
final String destDesc = Type.getDescriptor(destClass); final String destDesc = Type.getDescriptor(destClass);
final String srcDesc = Type.getDescriptor(srcClass); final String srcDesc = Type.getDescriptor(srcClass);
final RedkaleClassLoader loader = RedkaleClassLoader.getRedkaleClassLoader(); final RedkaleClassLoader classLoader = RedkaleClassLoader.getRedkaleClassLoader();
final String newDynName = "org/redkaledyn/reproduce/_Dyn" + Reproduce.class.getSimpleName() final String newDynName = "org/redkaledyn/reproduce/_Dyn" + Reproduce.class.getSimpleName()
+ "__" + destClass.getName().replace('.', '_').replace('$', '_') + "__" + destClass.getName().replace('.', '_').replace('$', '_')
+ "__" + srcClass.getName().replace('.', '_').replace('$', '_'); + "__" + srcClass.getName().replace('.', '_').replace('$', '_');
try { try {
Class clz = RedkaleClassLoader.findDynClass(newDynName.replace('/', '.')); Class clz = classLoader.findDynClass(newDynName.replace('/', '.'));
return (Reproduce) (clz == null ? loader.loadClass(newDynName.replace('/', '.')) : clz) return (Reproduce) (clz == null ? classLoader.loadClass(newDynName.replace('/', '.')) : clz)
.getDeclaredConstructor() .getDeclaredConstructor()
.newInstance(); .newInstance();
} catch (Throwable ex) { } catch (Throwable ex) {
@@ -231,8 +231,8 @@ public interface Reproduce<D, S> extends BiFunction<D, S, D> {
cw.visitEnd(); cw.visitEnd();
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
byte[] bytes = cw.toByteArray(); byte[] bytes = cw.toByteArray();
Class<?> newClazz = loader.loadClass(newDynName.replace('/', '.'), bytes); Class<?> newClazz = classLoader.loadClass(newDynName.replace('/', '.'), bytes);
RedkaleClassLoader.putDynClass(newDynName.replace('/', '.'), bytes, newClazz); classLoader.putDynClass(newDynName.replace('/', '.'), bytes, newClazz);
RedkaleClassLoader.putReflectionDeclaredConstructors(newClazz, newDynName.replace('/', '.')); RedkaleClassLoader.putReflectionDeclaredConstructors(newClazz, newDynName.replace('/', '.'));
try { try {
return (Reproduce) newClazz.getDeclaredConstructor().newInstance(); return (Reproduce) newClazz.getDeclaredConstructor().newInstance();

View File

@@ -662,7 +662,7 @@ public abstract class TypeToken<T> {
// 注意: RetResult<Map<String, Long>[]> 这种泛型带[]的尚未实现支持 // 注意: RetResult<Map<String, Long>[]> 这种泛型带[]的尚未实现支持
private static Type createParameterizedType0(final Class rawType, final Type... actualTypeArguments) { private static Type createParameterizedType0(final Class rawType, final Type... actualTypeArguments) {
RedkaleClassLoader loader = RedkaleClassLoader.getRedkaleClassLoader(); RedkaleClassLoader classLoader = RedkaleClassLoader.getRedkaleClassLoader();
StringBuilder tmpps = new StringBuilder(getClassTypeDescriptor(rawType)); StringBuilder tmpps = new StringBuilder(getClassTypeDescriptor(rawType));
for (Type cz : actualTypeArguments) { for (Type cz : actualTypeArguments) {
tmpps.append(" ").append(getClassTypeDescriptor(cz)); tmpps.append(" ").append(getClassTypeDescriptor(cz));
@@ -683,7 +683,7 @@ public abstract class TypeToken<T> {
final String newDynName = final String newDynName =
"org/redkaledyn/typetoken/_Dyn" + TypeToken.class.getSimpleName() + "_" + nsb.toString(); "org/redkaledyn/typetoken/_Dyn" + TypeToken.class.getSimpleName() + "_" + nsb.toString();
try { try {
return loader.loadClass(newDynName.replace('/', '.')) return classLoader.loadClass(newDynName.replace('/', '.'))
.getField("field") .getField("field")
.getGenericType(); .getGenericType();
} catch (Throwable ex) { } catch (Throwable ex) {
@@ -715,8 +715,8 @@ public abstract class TypeToken<T> {
} }
cw.visitEnd(); cw.visitEnd();
byte[] bytes = cw.toByteArray(); byte[] bytes = cw.toByteArray();
Class<?> newClazz = loader.loadClass(newDynName.replace('/', '.'), bytes); Class<?> newClazz = classLoader.loadClass(newDynName.replace('/', '.'), bytes);
RedkaleClassLoader.putDynClass(newDynName.replace('/', '.'), bytes, newClazz); classLoader.putDynClass(newDynName.replace('/', '.'), bytes, newClazz);
RedkaleClassLoader.putReflectionPublicFields(newDynName.replace('/', '.')); RedkaleClassLoader.putReflectionPublicFields(newDynName.replace('/', '.'));
try { try {
return newClazz.getField("field").getGenericType(); return newClazz.getField("field").getGenericType();

View File

@@ -97,7 +97,7 @@ public final class Utility {
return defineClass(name, b, 0, b.length); return defineClass(name, b, 0, b.length);
} }
}.loadClass(unsafeName, classBytes); }.loadClass(unsafeName, classBytes);
RedkaleClassLoader.putDynClass(unsafeClazz1.getName(), classBytes, unsafeClazz1); RedkaleClassLoader.putDynClass0(unsafeClazz1.getName(), classBytes, unsafeClazz1);
RedkaleClassLoader.putReflectionDeclaredConstructors(unsafeClazz1, unsafeClazz1.getName()); RedkaleClassLoader.putReflectionDeclaredConstructors(unsafeClazz1, unsafeClazz1.getName());
} }
unsafe0 = unsafeClazz1.getDeclaredConstructor().newInstance(); unsafe0 = unsafeClazz1.getDeclaredConstructor().newInstance();
@@ -245,7 +245,7 @@ public final class Utility {
return defineClass(name, b, 0, b.length); return defineClass(name, b, 0, b.length);
} }
}.loadClass("org.redkale.util.SignalShutDown", classBytes); }.loadClass("org.redkale.util.SignalShutDown", classBytes);
RedkaleClassLoader.putDynClass(shutdownClazz1.getName(), classBytes, shutdownClazz1); RedkaleClassLoader.putDynClass0(shutdownClazz1.getName(), classBytes, shutdownClazz1);
RedkaleClassLoader.putReflectionDeclaredConstructors(shutdownClazz1, shutdownClazz1.getName()); RedkaleClassLoader.putReflectionDeclaredConstructors(shutdownClazz1, shutdownClazz1.getName());
} }
signalShutdownConsumer0 = shutdownClazz1.getDeclaredConstructor().newInstance(); signalShutdownConsumer0 = shutdownClazz1.getDeclaredConstructor().newInstance();
@@ -2226,12 +2226,11 @@ public final class Utility {
/** /**
* 将一个或多个新元素添加到数组结尾 * 将一个或多个新元素添加到数组结尾
* *
* @param <T> 泛型
* @param array 原数组 * @param array 原数组
* @param objs 待追加数据 * @param objs 待追加数据
* @return 新数组 * @return 新数组
*/ */
public static <T> Object[][] append(final Object[][] array, final Object[]... objs) { public static Object[][] append(final Object[][] array, final Object[]... objs) {
if (array == null || array.length == 0) { if (array == null || array.length == 0) {
return objs; return objs;
} }

View File

@@ -20,6 +20,7 @@ import org.redkale.net.sncp.SncpClient;
import org.redkale.net.sncp.SncpRpcGroups; import org.redkale.net.sncp.SncpRpcGroups;
import org.redkale.source.CacheMemorySource; import org.redkale.source.CacheMemorySource;
import org.redkale.util.Environment; import org.redkale.util.Environment;
import org.redkale.util.RedkaleClassLoader;
import org.redkale.util.Utility; import org.redkale.util.Utility;
/** @author zhangjx */ /** @author zhangjx */
@@ -33,6 +34,8 @@ public class CachedInstanceTest {
private static CachedManagerService manager2; private static CachedManagerService manager2;
private static RedkaleClassLoader classLoader;
public static void main(String[] args) throws Throwable { public static void main(String[] args) throws Throwable {
LoggingBaseHandler.initDebugLogConfig(); LoggingBaseHandler.initDebugLogConfig();
CachedInstanceTest test = new CachedInstanceTest(); CachedInstanceTest test = new CachedInstanceTest();
@@ -43,7 +46,7 @@ public class CachedInstanceTest {
@BeforeAll @BeforeAll
public static void init() throws Exception { public static void init() throws Exception {
classLoader = RedkaleClassLoader.getRedkaleClassLoader();
CacheMemorySource remoteSource = new CacheMemorySource("cache-remote"); CacheMemorySource remoteSource = new CacheMemorySource("cache-remote");
remoteSource.init(null); remoteSource.init(null);
resourceFactory = ResourceFactory.create(); resourceFactory = ResourceFactory.create();
@@ -61,6 +64,7 @@ public class CachedInstanceTest {
@Test @Test
public void run1() throws Exception { public void run1() throws Exception {
ClassLoader parent = Thread.currentThread().getContextClassLoader();
Class<CachedInstance> serviceClass = CachedInstance.class; Class<CachedInstance> serviceClass = CachedInstance.class;
CachedAsmMethodBoost boost = new CachedAsmMethodBoost(false, serviceClass); CachedAsmMethodBoost boost = new CachedAsmMethodBoost(false, serviceClass);
CachedAsmMethodBoost boost2 = new CachedAsmMethodBoost(false, serviceClass); CachedAsmMethodBoost boost2 = new CachedAsmMethodBoost(false, serviceClass);
@@ -69,10 +73,19 @@ public class CachedInstanceTest {
SncpClient client = new SncpClient( SncpClient client = new SncpClient(
"", iGroup, "0", new InetSocketAddress("127.0.0.1", 8080), new ClientAddress(), "TCP", 1, 16); "", iGroup, "0", new InetSocketAddress("127.0.0.1", 8080), new ClientAddress(), "TCP", 1, 16);
CachedInstance instance = Sncp.createLocalService( CachedInstance instance = Sncp.createLocalService(
null, "", serviceClass, boost, resourceFactory, grous, client, null, null, null); classLoader, "", serviceClass, boost, resourceFactory, grous, client, null, null, null);
resourceFactory.inject(instance); resourceFactory.inject(instance);
CachedInstance instance2 = Sncp.createLocalService( CachedInstance instance2 = Sncp.createLocalService(
null, "", serviceClass, boost2, resourceFactory2, grous, client, null, null, null); new RedkaleClassLoader(parent),
"",
serviceClass,
boost2,
resourceFactory2,
grous,
client,
null,
null,
null);
resourceFactory2.inject(instance2); resourceFactory2.inject(instance2);
System.out.println(instance.getName2()); System.out.println(instance.getName2());
@@ -98,6 +111,7 @@ public class CachedInstanceTest {
@Test @Test
public void run2() throws Exception { public void run2() throws Exception {
ClassLoader parent = Thread.currentThread().getContextClassLoader();
Class<CachedInstance> serviceClass = CachedInstance.class; Class<CachedInstance> serviceClass = CachedInstance.class;
CachedAsmMethodBoost boost = new CachedAsmMethodBoost(false, serviceClass); CachedAsmMethodBoost boost = new CachedAsmMethodBoost(false, serviceClass);
CachedAsmMethodBoost boost2 = new CachedAsmMethodBoost(false, serviceClass); CachedAsmMethodBoost boost2 = new CachedAsmMethodBoost(false, serviceClass);
@@ -106,10 +120,28 @@ public class CachedInstanceTest {
SncpClient client = new SncpClient( SncpClient client = new SncpClient(
"", iGroup, "0", new InetSocketAddress("127.0.0.1", 8080), new ClientAddress(), "TCP", 1, 16); "", iGroup, "0", new InetSocketAddress("127.0.0.1", 8080), new ClientAddress(), "TCP", 1, 16);
CachedInstance instance = Sncp.createLocalService( CachedInstance instance = Sncp.createLocalService(
null, "", serviceClass, boost, resourceFactory, grous, client, null, null, null); new RedkaleClassLoader(parent),
"",
serviceClass,
boost,
resourceFactory,
grous,
client,
null,
null,
null);
resourceFactory.inject(instance); resourceFactory.inject(instance);
CachedInstance instance2 = Sncp.createLocalService( CachedInstance instance2 = Sncp.createLocalService(
null, "", serviceClass, boost2, resourceFactory2, grous, client, null, null, null); new RedkaleClassLoader(parent),
"",
serviceClass,
boost2,
resourceFactory2,
grous,
client,
null,
null,
null);
resourceFactory2.inject(instance2); resourceFactory2.inject(instance2);
int threads = Runtime.getRuntime().availableProcessors() * 10; int threads = Runtime.getRuntime().availableProcessors() * 10;

View File

@@ -20,6 +20,7 @@ import org.redkale.net.http.HttpServer;
import org.redkale.net.http.HttpServlet; import org.redkale.net.http.HttpServlet;
import org.redkale.net.sncp.Sncp; import org.redkale.net.sncp.Sncp;
import org.redkale.util.AnyValueWriter; import org.redkale.util.AnyValueWriter;
import org.redkale.util.RedkaleClassLoader;
/** /**
* *
@@ -44,12 +45,13 @@ public class RestConvertTest {
Method method = Application.class.getDeclaredMethod("initWorkExecutor"); Method method = Application.class.getDeclaredMethod("initWorkExecutor");
method.setAccessible(true); method.setAccessible(true);
method.invoke(application); method.invoke(application);
RedkaleClassLoader classLoader = RedkaleClassLoader.getRedkaleClassLoader();
// ------------------------ 初始化 CService ------------------------------------ // ------------------------ 初始化 CService ------------------------------------
RestConvertService service = Sncp.createSimpleLocalService(RestConvertService.class, resFactory); RestConvertService service =
Sncp.createSimpleLocalService(application.getClassLoader(), RestConvertService.class, resFactory);
HttpServer server = new HttpServer(application, System.currentTimeMillis(), resFactory); HttpServer server = new HttpServer(application, System.currentTimeMillis(), resFactory);
server.getResourceFactory().register(application); server.getResourceFactory().register(application);
System.out.println("servlet = " + server.addRestServlet(null, service, null, HttpServlet.class, "")); System.out.println("servlet = " + server.addRestServlet(classLoader, service, null, HttpServlet.class, ""));
server.init(AnyValueWriter.create("port", 0)); server.init(AnyValueWriter.create("port", 0));
server.start(); server.start();

View File

@@ -40,10 +40,12 @@ public class RestSleepTest {
method.invoke(application); method.invoke(application);
// ------------------------ 初始化 CService ------------------------------------ // ------------------------ 初始化 CService ------------------------------------
RestSleepService service = Sncp.createSimpleLocalService(RestSleepService.class, resFactory); RedkaleClassLoader classLoader = RedkaleClassLoader.getRedkaleClassLoader();
RestSleepService service =
Sncp.createSimpleLocalService(application.getClassLoader(), RestSleepService.class, resFactory);
HttpServer server = new HttpServer(application, System.currentTimeMillis(), resFactory); HttpServer server = new HttpServer(application, System.currentTimeMillis(), resFactory);
server.getResourceFactory().register(application); server.getResourceFactory().register(application);
System.out.println("servlet = " + server.addRestServlet(null, service, null, HttpServlet.class, "")); System.out.println("servlet = " + server.addRestServlet(classLoader, service, null, HttpServlet.class, ""));
server.init(AnyValueWriter.create("port", 0)); server.init(AnyValueWriter.create("port", 0));
server.start(); server.start();

View File

@@ -24,6 +24,7 @@ import org.redkale.net.sncp.Sncp;
import org.redkale.net.sncp.SncpClient; import org.redkale.net.sncp.SncpClient;
import org.redkale.net.sncp.SncpRpcGroups; import org.redkale.net.sncp.SncpRpcGroups;
import org.redkale.util.AnyValue; import org.redkale.util.AnyValue;
import org.redkale.util.RedkaleClassLoader;
import org.redkale.util.Utility; import org.redkale.util.Utility;
/** /**
@@ -38,6 +39,8 @@ public class MessagedInstanceTest {
private static ResourceFactory resourceFactory; private static ResourceFactory resourceFactory;
private static RedkaleClassLoader classLoader;
public static void main(String[] args) throws Throwable { public static void main(String[] args) throws Throwable {
LoggingBaseHandler.initDebugLogConfig(); LoggingBaseHandler.initDebugLogConfig();
MessagedInstanceTest test = new MessagedInstanceTest(); MessagedInstanceTest test = new MessagedInstanceTest();
@@ -51,6 +54,7 @@ public class MessagedInstanceTest {
application = Application.create(true); application = Application.create(true);
resourceFactory = application.getResourceFactory(); resourceFactory = application.getResourceFactory();
engine = new MessageModuleEngine(application); engine = new MessageModuleEngine(application);
classLoader = RedkaleClassLoader.getRedkaleClassLoader();
MessageAgent agent = createMessageAgent(application, "mymq"); MessageAgent agent = createMessageAgent(application, "mymq");
MessageAgent[] messageAgents = new MessageAgent[] {agent}; MessageAgent[] messageAgents = new MessageAgent[] {agent};
@@ -68,9 +72,8 @@ public class MessagedInstanceTest {
SncpClient client = new SncpClient( SncpClient client = new SncpClient(
"", iGroup, "0", new InetSocketAddress("127.0.0.1", 8080), new ClientAddress(), "TCP", 1, 16); "", iGroup, "0", new InetSocketAddress("127.0.0.1", 8080), new ClientAddress(), "TCP", 1, 16);
TestMessageService instance = Sncp.createLocalService( TestMessageService instance = Sncp.createLocalService(
null, "", serviceClass, boost, resourceFactory, grous, client, null, null, null); classLoader, "", serviceClass, boost, resourceFactory, grous, client, null, null, null);
resourceFactory.inject(instance); resourceFactory.inject(instance);
} }
@Test @Test

View File

@@ -51,7 +51,7 @@ public class ABMainService implements Service {
rpcGroups.computeIfAbsent("g99", "TCP").putAddress(new InetSocketAddress("127.0.0.1", 5599)); rpcGroups.computeIfAbsent("g99", "TCP").putAddress(new InetSocketAddress("127.0.0.1", 5599));
// ------------------------ 初始化 CService ------------------------------------ // ------------------------ 初始化 CService ------------------------------------
CService cservice = Sncp.createSimpleLocalService(CService.class, resFactory); CService cservice = Sncp.createSimpleLocalService(application.getClassLoader(), CService.class, resFactory);
SncpServer cserver = new SncpServer(); SncpServer cserver = new SncpServer();
cserver.getResourceFactory().register(application); cserver.getResourceFactory().register(application);
// cserver.getLogger().setLevel(Level.WARNING); // cserver.getLogger().setLevel(Level.WARNING);
@@ -60,8 +60,9 @@ public class ABMainService implements Service {
cserver.start(); cserver.start();
// ------------------------ 初始化 BCService ------------------------------------ // ------------------------ 初始化 BCService ------------------------------------
BCService bcservice = Sncp.createSimpleLocalService(BCService.class, resFactory); BCService bcservice = Sncp.createSimpleLocalService(application.getClassLoader(), BCService.class, resFactory);
CService remoteCService = Sncp.createSimpleRemoteService(CService.class, resFactory, rpcGroups, client, "g77"); CService remoteCService = Sncp.createSimpleRemoteService(
application.getClassLoader(), CService.class, resFactory, rpcGroups, client, "g77");
if (remoteCService != null) { if (remoteCService != null) {
resFactory.inject(remoteCService); resFactory.inject(remoteCService);
resFactory.register("", remoteCService); resFactory.register("", remoteCService);
@@ -74,9 +75,10 @@ public class ABMainService implements Service {
bcserver.start(); bcserver.start();
// ------------------------ 初始化 ABMainService ------------------------------------ // ------------------------ 初始化 ABMainService ------------------------------------
ABMainService service = Sncp.createSimpleLocalService(ABMainService.class, resFactory); ABMainService service =
BCService remoteBCService = Sncp.createSimpleLocalService(application.getClassLoader(), ABMainService.class, resFactory);
Sncp.createSimpleRemoteService(BCService.class, resFactory, rpcGroups, client, "g88"); BCService remoteBCService = Sncp.createSimpleRemoteService(
application.getClassLoader(), BCService.class, resFactory, rpcGroups, client, "g88");
if (remoteBCService != null) { if (remoteBCService != null) {
resFactory.inject(remoteBCService); resFactory.inject(remoteBCService);
resFactory.register("", remoteBCService); resFactory.register("", remoteBCService);

View File

@@ -41,7 +41,8 @@ public class SncpSleepTest {
resFactory.register(ProtobufConvert.root()); resFactory.register(ProtobufConvert.root());
// ------------------------ 初始化 CService ------------------------------------ // ------------------------ 初始化 CService ------------------------------------
SncpSleepService service = Sncp.createSimpleLocalService(SncpSleepService.class, resFactory); SncpSleepService service =
Sncp.createSimpleLocalService(application.getClassLoader(), SncpSleepService.class, resFactory);
resFactory.inject(service); resFactory.inject(service);
SncpServer server = new SncpServer(application, System.currentTimeMillis(), null, resFactory); SncpServer server = new SncpServer(application, System.currentTimeMillis(), null, resFactory);
server.getResourceFactory().register(application); server.getResourceFactory().register(application);
@@ -56,8 +57,8 @@ public class SncpSleepTest {
new SncpClient("", asyncGroup, "0", sncpAddress, new ClientAddress(sncpAddress), "TCP", 16, 100); new SncpClient("", asyncGroup, "0", sncpAddress, new ClientAddress(sncpAddress), "TCP", 16, 100);
final SncpRpcGroups rpcGroups = application.getSncpRpcGroups(); final SncpRpcGroups rpcGroups = application.getSncpRpcGroups();
rpcGroups.computeIfAbsent("cs", "TCP").putAddress(sncpAddress); rpcGroups.computeIfAbsent("cs", "TCP").putAddress(sncpAddress);
SncpSleepService remoteCService = SncpSleepService remoteCService = Sncp.createSimpleRemoteService(
Sncp.createSimpleRemoteService(SncpSleepService.class, resFactory, rpcGroups, client, "cs"); application.getClassLoader(), SncpSleepService.class, resFactory, rpcGroups, client, "cs");
long s = System.currentTimeMillis(); long s = System.currentTimeMillis();
CompletableFuture[] futures = CompletableFuture[] futures =
new CompletableFuture[] {remoteCService.sleep200(), remoteCService.sleep300(), remoteCService.sleep500() new CompletableFuture[] {remoteCService.sleep200(), remoteCService.sleep300(), remoteCService.sleep500()

View File

@@ -8,6 +8,7 @@ package org.redkale.test.sncp;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.util.concurrent.*; import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.redkale.boot.*; import org.redkale.boot.*;
import org.redkale.convert.pb.ProtobufConvert; import org.redkale.convert.pb.ProtobufConvert;
@@ -37,25 +38,32 @@ public class SncpTest {
private static Application application; private static Application application;
private static RedkaleClassLoader classLoader;
private static SncpRpcGroups rpcGroups; private static SncpRpcGroups rpcGroups;
private boolean main; private boolean main;
public static void main(String[] args) throws Throwable { public static void main(String[] args) throws Throwable {
SncpTest test = new SncpTest(); SncpTest test = new SncpTest();
init();
test.main = true; test.main = true;
test.run(); test.run();
} }
@Test @BeforeAll
public void run() throws Exception { public static void init() throws Exception {
LoggingBaseHandler.initDebugLogConfig(); LoggingBaseHandler.initDebugLogConfig();
application = Application.create(true); application = Application.create(true);
classLoader = application.getClassLoader();
rpcGroups = application.getSncpRpcGroups(); rpcGroups = application.getSncpRpcGroups();
factory = application.getResourceFactory(); factory = application.getResourceFactory();
factory.register("", ProtobufConvert.class, ProtobufFactory.root().getConvert()); factory.register("", ProtobufConvert.class, ProtobufFactory.root().getConvert());
factory.register("", Application.class, application); factory.register("", Application.class, application);
}
@Test
public void run() throws Exception {
if (System.getProperty("client") == null) { if (System.getProperty("client") == null) {
runServer(); runServer();
if (port2 > 0) { if (port2 > 0) {
@@ -95,6 +103,7 @@ public class SncpTest {
100); 100);
final SncpTestIService service = Sncp.createSimpleRemoteService( final SncpTestIService service = Sncp.createSimpleRemoteService(
classLoader,
SncpTestIService.class, SncpTestIService.class,
factory, factory,
rpcGroups, rpcGroups,
@@ -200,6 +209,7 @@ public class SncpTest {
} }
SncpTestIService service = Sncp.createSimpleLocalService( SncpTestIService service = Sncp.createSimpleLocalService(
classLoader,
SncpTestServiceImpl.class, SncpTestServiceImpl.class,
factory); // Sncp.createSimpleLocalService(SncpTestServiceImpl.class, null, factory, factory); // Sncp.createSimpleLocalService(SncpTestServiceImpl.class, null, factory,
// transFactory, addr, "server"); // transFactory, addr, "server");
@@ -242,6 +252,7 @@ public class SncpTest {
.putAddress(new InetSocketAddress(myhost, port)); .putAddress(new InetSocketAddress(myhost, port));
Service service = Sncp.createSimpleLocalService( Service service = Sncp.createSimpleLocalService(
new RedkaleClassLoader(Thread.currentThread().getContextClassLoader()),
SncpTestServiceImpl.class, SncpTestServiceImpl.class,
factory); // Sncp.createSimpleLocalService(SncpTestServiceImpl.class, null, factory, factory); // Sncp.createSimpleLocalService(SncpTestServiceImpl.class, null, factory,
// transFactory, addr, "server"); // transFactory, addr, "server");

View File

@@ -95,7 +95,8 @@ public class SncpTestServiceImpl implements SncpTestIService {
final SncpClient client = final SncpClient client =
new SncpClient("", asyncGroup, "0", sncpAddress, new ClientAddress(sncpAddress), "TCP", 16, 100); new SncpClient("", asyncGroup, "0", sncpAddress, new ClientAddress(sncpAddress), "TCP", 16, 100);
Service service = Sncp.createSimpleLocalService(SncpTestServiceImpl.class, factory); Service service =
Sncp.createSimpleLocalService(application.getClassLoader(), SncpTestServiceImpl.class, factory);
for (Method method : service.getClass().getDeclaredMethods()) { for (Method method : service.getClass().getDeclaredMethods()) {
System.out.println(method); System.out.println(method);
} }
@@ -104,7 +105,8 @@ public class SncpTestServiceImpl implements SncpTestIService {
System.out.println(method); System.out.println(method);
} }
System.out.println("-----------------------------------"); System.out.println("-----------------------------------");
service = Sncp.createSimpleRemoteService(SncpTestServiceImpl.class, factory, rpcGroups, client, "g70"); service = Sncp.createSimpleRemoteService(
application.getClassLoader(), SncpTestServiceImpl.class, factory, rpcGroups, client, "g70");
for (Method method : service.getClass().getDeclaredMethods()) { for (Method method : service.getClass().getDeclaredMethods()) {
System.out.println(method); System.out.println(method);
} }
@@ -113,7 +115,8 @@ public class SncpTestServiceImpl implements SncpTestIService {
System.out.println(method); System.out.println(method);
} }
System.out.println("-----------------------------------"); System.out.println("-----------------------------------");
service = Sncp.createSimpleRemoteService(SncpTestIService.class, factory, rpcGroups, client, "g70"); service = Sncp.createSimpleRemoteService(
application.getClassLoader(), SncpTestIService.class, factory, rpcGroups, client, "g70");
for (Method method : service.getClass().getDeclaredMethods()) { for (Method method : service.getClass().getDeclaredMethods()) {
System.out.println(method); System.out.println(method);
} }