Sncp.remote优化
This commit is contained in:
@@ -403,10 +403,12 @@ public abstract class NodeServer {
|
||||
continue; //本地模式或Component不能实例化接口和抽象类的Service类
|
||||
}
|
||||
|
||||
final ResourceTypeLoader resourceLoader = (ResourceFactory rf, String srcResourceName, final Object srcObj, final String resourceName, Field field, final Object attachment) -> {
|
||||
final ResourceTypeLoader resourceLoader = (ResourceFactory rf, String srcResourceName,
|
||||
final Object srcObj, final String resourceName, Field field, final Object attachment) -> {
|
||||
try {
|
||||
if (Sncp.loadMethodActions(Sncp.getResourceType(serviceImplClass)).isEmpty()
|
||||
&& (serviceImplClass.getAnnotation(Priority.class) == null && serviceImplClass.getAnnotation(javax.annotation.Priority.class) == null)) { //class没有可用的方法且没有标记启动优先级的, 通常为BaseService
|
||||
if (Sncp.loadRemoteMethodActions(Sncp.getResourceType(serviceImplClass)).isEmpty()
|
||||
&& (serviceImplClass.getAnnotation(Priority.class) == null
|
||||
&& serviceImplClass.getAnnotation(javax.annotation.Priority.class) == null)) { //class没有可用的方法且没有标记启动优先级的, 通常为BaseService
|
||||
if (!serviceImplClass.getName().startsWith("org.redkale.") && !serviceImplClass.getSimpleName().contains("Base")) {
|
||||
logger.log(Level.FINE, serviceImplClass + " cannot load because not found less one public non-final method");
|
||||
}
|
||||
|
||||
@@ -75,7 +75,7 @@ public abstract class Sncp {
|
||||
}
|
||||
|
||||
//key: actionid
|
||||
public static LinkedHashMap<Uint128, Method> loadMethodActions(final Class serviceTypeOrImplClass) {
|
||||
public static LinkedHashMap<Uint128, Method> loadRemoteMethodActions(final Class serviceTypeOrImplClass) {
|
||||
final List<Method> list = new ArrayList<>();
|
||||
final List<Method> multis = new ArrayList<>();
|
||||
final Map<Uint128, Method> actionids = new LinkedHashMap<>();
|
||||
@@ -552,8 +552,40 @@ public abstract class Sncp {
|
||||
mv.visitEnd();
|
||||
}
|
||||
if (methodBoost != null) {
|
||||
Class clazz = serviceImplClass;
|
||||
Set<String> methodKeys = new HashSet<>();
|
||||
createNewMethods(serviceImplClass, methodBoost, new HashSet<>(), cw, newDynName, supDynName);
|
||||
methodBoost.doAfterMethods(cw, newDynName, FIELDPREFIX);
|
||||
}
|
||||
cw.visitEnd();
|
||||
byte[] bytes = cw.toByteArray();
|
||||
Class<?> newClazz = new ClassLoader(loader) {
|
||||
public final Class<?> loadClass(String name, byte[] b) {
|
||||
return defineClass(name, b, 0, b.length);
|
||||
}
|
||||
}.loadClass(newDynName.replace('/', '.'), bytes);
|
||||
RedkaleClassLoader.putDynClass(newDynName.replace('/', '.'), bytes, newClazz);
|
||||
RedkaleClassLoader.putReflectionPublicClasses(newDynName.replace('/', '.'));
|
||||
RedkaleClassLoader.putReflectionDeclaredConstructors(newClazz, newDynName.replace('/', '.'));
|
||||
try {
|
||||
Field c = newClazz.getDeclaredField(FIELDPREFIX + "_conf");
|
||||
RedkaleClassLoader.putReflectionField(newDynName.replace('/', '.'), c);
|
||||
|
||||
c = newClazz.getDeclaredField(FIELDPREFIX + "_mq");
|
||||
RedkaleClassLoader.putReflectionField(newDynName.replace('/', '.'), c);
|
||||
} catch (Exception e) {
|
||||
//do nothing
|
||||
}
|
||||
return (Class<T>) newClazz;
|
||||
}
|
||||
|
||||
public static <T extends Service> T createSimpleLocalService(Class<T> serviceImplClass, ResourceFactory resourceFactory) {
|
||||
return createLocalService(null, "", serviceImplClass, null, resourceFactory, null, null, null, null, null);
|
||||
}
|
||||
|
||||
private static void createNewMethods(Class clazz, final AsmMethodBoost methodBoost, Set<String> methodKeys, ClassWriter cw, String newDynName, String supDynName) {
|
||||
if (methodBoost == null) {
|
||||
return;
|
||||
}
|
||||
MethodDebugVisitor mv = null;
|
||||
do {
|
||||
Map<String, AsmMethodBean> methodBeans = AsmMethodBoost.getMethodBeans(clazz);
|
||||
for (final Method method : clazz.getDeclaredMethods()) {
|
||||
@@ -643,32 +675,6 @@ public abstract class Sncp {
|
||||
}
|
||||
}
|
||||
} while ((clazz = clazz.getSuperclass()) != Object.class);
|
||||
methodBoost.doAfterMethods(cw, newDynName, FIELDPREFIX);
|
||||
}
|
||||
cw.visitEnd();
|
||||
byte[] bytes = cw.toByteArray();
|
||||
Class<?> newClazz = new ClassLoader(loader) {
|
||||
public final Class<?> loadClass(String name, byte[] b) {
|
||||
return defineClass(name, b, 0, b.length);
|
||||
}
|
||||
}.loadClass(newDynName.replace('/', '.'), bytes);
|
||||
RedkaleClassLoader.putDynClass(newDynName.replace('/', '.'), bytes, newClazz);
|
||||
RedkaleClassLoader.putReflectionPublicClasses(newDynName.replace('/', '.'));
|
||||
RedkaleClassLoader.putReflectionDeclaredConstructors(newClazz, newDynName.replace('/', '.'));
|
||||
try {
|
||||
Field c = newClazz.getDeclaredField(FIELDPREFIX + "_conf");
|
||||
RedkaleClassLoader.putReflectionField(newDynName.replace('/', '.'), c);
|
||||
|
||||
c = newClazz.getDeclaredField(FIELDPREFIX + "_mq");
|
||||
RedkaleClassLoader.putReflectionField(newDynName.replace('/', '.'), c);
|
||||
} catch (Exception e) {
|
||||
//do nothing
|
||||
}
|
||||
return (Class<T>) newClazz;
|
||||
}
|
||||
|
||||
public static <T extends Service> T createSimpleLocalService(Class<T> serviceImplClass, ResourceFactory resourceFactory) {
|
||||
return createLocalService(null, "", serviceImplClass, null, resourceFactory, null, null, null, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -757,10 +763,10 @@ public abstract class Sncp {
|
||||
public static <T extends Service> T createSimpleRemoteService(Class<T> serviceImplClass,
|
||||
ResourceFactory resourceFactory, SncpRpcGroups sncpRpcGroups, SncpClient client, String group) {
|
||||
if (sncpRpcGroups == null) {
|
||||
throw new SncpException("SncpRpcGroups is null");
|
||||
throw new SncpException(SncpRpcGroups.class.getSimpleName() + " is null");
|
||||
}
|
||||
if (client == null) {
|
||||
throw new SncpException("SncpClient is null");
|
||||
throw new SncpException(SncpClient.class.getSimpleName() + " is null");
|
||||
}
|
||||
return createRemoteService(null, "", serviceImplClass, null, resourceFactory, sncpRpcGroups, client, null, group, null);
|
||||
}
|
||||
@@ -974,11 +980,29 @@ public abstract class Sncp {
|
||||
// mv.visitMaxs(1, 1);
|
||||
// mv.visitEnd();
|
||||
// }
|
||||
Set<String> methodKeys = new HashSet<>();
|
||||
Map<String, AsmMethodBean> methodBeans = AsmMethodBoost.getMethodBeans(serviceTypeOrImplClass);
|
||||
for (final SncpRemoteAction entry : info.getActions()) {
|
||||
final java.lang.reflect.Method method = entry.method;
|
||||
{
|
||||
mv = new MethodDebugVisitor(cw.visitMethod(ACC_PUBLIC, method.getName(), Type.getMethodDescriptor(method), null, null));
|
||||
String mk = Utility.methodKey(method);
|
||||
if (methodKeys.contains(mk)) {
|
||||
//跳过已处理的继承方法
|
||||
continue;
|
||||
}
|
||||
methodKeys.add(mk);
|
||||
|
||||
int acc = ACC_PUBLIC;
|
||||
String newMethodName = null;
|
||||
if (methodBoost != null) {
|
||||
List<Class<? extends Annotation>> filterAnns = methodBoost.filterMethodAnnotations(method);
|
||||
newMethodName = methodBoost.doMethod(cw, newDynName, FIELDPREFIX, filterAnns, method, null);
|
||||
}
|
||||
if (newMethodName != null) {
|
||||
acc = ACC_PRIVATE;
|
||||
} else {
|
||||
newMethodName = method.getName();
|
||||
}
|
||||
mv = new MethodDebugVisitor(cw.visitMethod(acc, newMethodName, Type.getMethodDescriptor(method), null, null));
|
||||
Label l0 = new Label();
|
||||
mv.visitLabel(l0);
|
||||
//mv.setDebug(true);
|
||||
@@ -1072,6 +1096,9 @@ public abstract class Sncp {
|
||||
mv.visitMaxs(20, 20);
|
||||
mv.visitEnd();
|
||||
}
|
||||
if (methodBoost != null) {
|
||||
createNewMethods(serviceTypeOrImplClass, methodBoost, methodKeys, cw, newDynName, supDynName);
|
||||
methodBoost.doAfterMethods(cw, newDynName, FIELDPREFIX);
|
||||
}
|
||||
cw.visitEnd();
|
||||
byte[] bytes = cw.toByteArray();
|
||||
|
||||
@@ -3,9 +3,6 @@
|
||||
*/
|
||||
package org.redkale.net.sncp;
|
||||
|
||||
import org.redkale.mq.spi.MessageRecord;
|
||||
import org.redkale.mq.spi.MessageClient;
|
||||
import org.redkale.mq.spi.MessageAgent;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.*;
|
||||
import java.net.*;
|
||||
@@ -16,8 +13,10 @@ import java.util.concurrent.*;
|
||||
import java.util.logging.*;
|
||||
import org.redkale.convert.*;
|
||||
import org.redkale.convert.json.JsonConvert;
|
||||
import org.redkale.mq.*;
|
||||
import static org.redkale.net.sncp.Sncp.loadMethodActions;
|
||||
import org.redkale.mq.spi.MessageAgent;
|
||||
import org.redkale.mq.spi.MessageClient;
|
||||
import org.redkale.mq.spi.MessageRecord;
|
||||
import static org.redkale.net.sncp.Sncp.loadRemoteMethodActions;
|
||||
import static org.redkale.net.sncp.SncpHeader.HEADER_SUBSIZE;
|
||||
import org.redkale.service.*;
|
||||
import org.redkale.util.*;
|
||||
@@ -90,7 +89,7 @@ public class SncpRemoteInfo<S extends Service> {
|
||||
this.messageClient = messageAgent == null ? null : messageAgent.getSncpMessageClient();
|
||||
this.topic = messageAgent == null ? null : Sncp.generateSncpReqTopic(resourceName, resourceType, messageAgent.getNodeid());
|
||||
|
||||
for (Map.Entry<Uint128, Method> en : loadMethodActions(Sncp.getServiceType(serviceImplClass)).entrySet()) {
|
||||
for (Map.Entry<Uint128, Method> en : loadRemoteMethodActions(Sncp.getServiceType(serviceImplClass)).entrySet()) {
|
||||
this.actions.put(en.getKey().toString(), new SncpRemoteAction(serviceImplClass, resourceType, en.getValue(), serviceid, en.getKey(), sncpClient));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,7 +60,7 @@ public class SncpServlet extends Servlet<SncpContext, SncpRequest, SncpResponse>
|
||||
this.serviceid = Sncp.serviceid(resourceName, resourceType);
|
||||
|
||||
Class serviceImplClass = Sncp.getServiceType(service);
|
||||
for (Map.Entry<Uint128, Method> en : Sncp.loadMethodActions(serviceImplClass).entrySet()) {
|
||||
for (Map.Entry<Uint128, Method> en : Sncp.loadRemoteMethodActions(serviceImplClass).entrySet()) {
|
||||
SncpActionServlet action;
|
||||
try {
|
||||
action = SncpActionServlet.create(resourceName, resourceType, serviceImplClass, service, serviceid, en.getKey(), en.getValue());
|
||||
|
||||
@@ -100,7 +100,7 @@ public class SncpTestServiceImpl implements SncpTestIService {
|
||||
System.out.println(method);
|
||||
}
|
||||
System.out.println("-----------------------------------");
|
||||
for (Method method : Sncp.loadMethodActions(service.getClass()).values()) {
|
||||
for (Method method : Sncp.loadRemoteMethodActions(service.getClass()).values()) {
|
||||
System.out.println(method);
|
||||
}
|
||||
System.out.println("-----------------------------------");
|
||||
@@ -109,7 +109,7 @@ public class SncpTestServiceImpl implements SncpTestIService {
|
||||
System.out.println(method);
|
||||
}
|
||||
System.out.println("-----------------------------------");
|
||||
for (Method method : Sncp.loadMethodActions(service.getClass()).values()) {
|
||||
for (Method method : Sncp.loadRemoteMethodActions(service.getClass()).values()) {
|
||||
System.out.println(method);
|
||||
}
|
||||
System.out.println("-----------------------------------");
|
||||
@@ -118,7 +118,7 @@ public class SncpTestServiceImpl implements SncpTestIService {
|
||||
System.out.println(method);
|
||||
}
|
||||
System.out.println("-----------------------------------");
|
||||
for (Method method : Sncp.loadMethodActions(service.getClass()).values()) {
|
||||
for (Method method : Sncp.loadRemoteMethodActions(service.getClass()).values()) {
|
||||
System.out.println(method);
|
||||
}
|
||||
System.out.println("-----------------------------------");
|
||||
|
||||
Reference in New Issue
Block a user