AsmMethodBoost优化
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
package org.redkale.asm;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Collection;
|
||||
import org.redkale.annotation.Nullable;
|
||||
|
||||
/**
|
||||
@@ -15,6 +16,14 @@ import org.redkale.annotation.Nullable;
|
||||
*/
|
||||
public interface AsmMethodBoost<T> {
|
||||
|
||||
public static AsmMethodBoost create(Collection<AsmMethodBoost> list) {
|
||||
return new AsmMethodBoosts(list);
|
||||
}
|
||||
|
||||
public static AsmMethodBoost create(AsmMethodBoost... items) {
|
||||
return new AsmMethodBoosts(items);
|
||||
}
|
||||
|
||||
/**
|
||||
* 对方法进行动态加强处理
|
||||
*
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
package org.redkale.asm;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* 生产动态字节码的方法扩展器, 可以进行方法加强动作
|
||||
@@ -12,18 +13,22 @@ import java.lang.reflect.Method;
|
||||
*
|
||||
* @since 2.8.0
|
||||
*/
|
||||
public class AsmMethodBoosts<T> implements AsmMethodBoost<T> {
|
||||
class AsmMethodBoosts<T> implements AsmMethodBoost<T> {
|
||||
|
||||
private final AsmMethodBoosts[] items;
|
||||
private final AsmMethodBoost[] items;
|
||||
|
||||
public AsmMethodBoosts(AsmMethodBoosts... items) {
|
||||
public AsmMethodBoosts(Collection<AsmMethodBoost> list) {
|
||||
this.items = list.toArray(new AsmMethodBoost[list.size()]);
|
||||
}
|
||||
|
||||
public AsmMethodBoosts(AsmMethodBoost... items) {
|
||||
this.items = items;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String doMethod(ClassWriter cw, Method method, String newMethodName) {
|
||||
String newName = newMethodName;
|
||||
for (AsmMethodBoosts item : items) {
|
||||
for (AsmMethodBoost item : items) {
|
||||
if (item != null) {
|
||||
newName = item.doMethod(cw, method, newName);
|
||||
}
|
||||
@@ -33,7 +38,7 @@ public class AsmMethodBoosts<T> implements AsmMethodBoost<T> {
|
||||
|
||||
@Override
|
||||
public void doInstance(T service) {
|
||||
for (AsmMethodBoosts item : items) {
|
||||
for (AsmMethodBoost item : items) {
|
||||
if (item != null) {
|
||||
item.doInstance(service);
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ import java.util.function.Consumer;
|
||||
import java.util.logging.*;
|
||||
import org.redkale.annotation.Nonnull;
|
||||
import org.redkale.annotation.Resource;
|
||||
import org.redkale.asm.AsmMethodBoost;
|
||||
import org.redkale.boot.ClassFilter.FilterEntry;
|
||||
import org.redkale.cache.spi.CacheModuleEngine;
|
||||
import org.redkale.cluster.*;
|
||||
@@ -841,6 +842,23 @@ public final class Application {
|
||||
}
|
||||
}
|
||||
|
||||
AsmMethodBoost createAsmMethodBoost(Class serviceClass) {
|
||||
List<AsmMethodBoost> list = null;
|
||||
for (ModuleEngine item : moduleEngines) {
|
||||
AsmMethodBoost boost = item.createAsmMethodBoost(serviceClass);
|
||||
if (boost != null) {
|
||||
if (list == null) {
|
||||
list = new ArrayList<>();
|
||||
}
|
||||
list.add(boost);
|
||||
}
|
||||
}
|
||||
if (list == null) {
|
||||
return null;
|
||||
}
|
||||
return list.size() == 1 ? list.get(0) : AsmMethodBoost.create(list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 进入Application.init方法时被调用
|
||||
*/
|
||||
|
||||
@@ -7,6 +7,7 @@ import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Properties;
|
||||
import java.util.logging.Logger;
|
||||
import org.redkale.asm.AsmMethodBoost;
|
||||
import org.redkale.inject.ResourceEvent;
|
||||
import org.redkale.inject.ResourceFactory;
|
||||
import org.redkale.service.Service;
|
||||
@@ -54,6 +55,17 @@ public abstract class ModuleEngine {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 动态扩展类的方法
|
||||
*
|
||||
* @param serviceClass 类
|
||||
*
|
||||
* @return 方法动态扩展器
|
||||
*/
|
||||
public AsmMethodBoost createAsmMethodBoost(Class serviceClass) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 进入Application.init方法时被调用
|
||||
* 此时状态:
|
||||
|
||||
@@ -14,6 +14,7 @@ import java.util.concurrent.locks.ReentrantLock;
|
||||
import java.util.logging.Level;
|
||||
import java.util.stream.Stream;
|
||||
import org.redkale.annotation.*;
|
||||
import org.redkale.asm.AsmMethodBoost;
|
||||
import static org.redkale.boot.Application.RESNAME_SNCP_ADDRESS;
|
||||
import org.redkale.boot.ClassFilter.FilterEntry;
|
||||
import org.redkale.cluster.ClusterAgent;
|
||||
@@ -170,7 +171,9 @@ public class NodeHttpServer extends NodeServer {
|
||||
} catch (Exception ex) {
|
||||
logger.log(Level.WARNING, "WebSocketServlet getMessageAgent error", ex);
|
||||
}
|
||||
nodeService = Sncp.createLocalService(serverClassLoader, resourceName, org.redkale.net.http.WebSocketNodeService.class, application.getResourceFactory(), application.getSncpRpcGroups(), sncpClient, messageAgent, (String) null, (AnyValue) null);
|
||||
AsmMethodBoost methodBoost = application.createAsmMethodBoost(WebSocketNodeService.class);
|
||||
nodeService = Sncp.createLocalService(serverClassLoader, resourceName, WebSocketNodeService.class, methodBoost,
|
||||
application.getResourceFactory(), application.getSncpRpcGroups(), sncpClient, messageAgent, (String) null, (AnyValue) null);
|
||||
regFactory.register(resourceName, WebSocketNode.class, nodeService);
|
||||
}
|
||||
resourceFactory.inject(resourceName, nodeService, self);
|
||||
|
||||
@@ -18,6 +18,7 @@ import java.util.logging.*;
|
||||
import org.redkale.annotation.*;
|
||||
import org.redkale.annotation.AutoLoad;
|
||||
import org.redkale.annotation.Command;
|
||||
import org.redkale.asm.AsmMethodBoost;
|
||||
import static org.redkale.boot.Application.*;
|
||||
import org.redkale.boot.ClassFilter.FilterEntry;
|
||||
import org.redkale.cluster.ClusterAgent;
|
||||
@@ -28,6 +29,7 @@ import org.redkale.net.*;
|
||||
import org.redkale.net.Filter;
|
||||
import org.redkale.net.client.ClientAddress;
|
||||
import org.redkale.net.http.*;
|
||||
import org.redkale.net.http.WebSocketNodeService;
|
||||
import org.redkale.net.sncp.*;
|
||||
import org.redkale.service.*;
|
||||
import org.redkale.source.*;
|
||||
@@ -262,7 +264,8 @@ public abstract class NodeServer {
|
||||
if (groups.isEmpty() && isSNCP() && NodeServer.this.sncpGroup != null) {
|
||||
groups.add(NodeServer.this.sncpGroup);
|
||||
}
|
||||
nodeService = Sncp.createLocalService(serverClassLoader, resourceName, org.redkale.net.http.WebSocketNodeService.class,
|
||||
AsmMethodBoost methodBoost = application.createAsmMethodBoost(WebSocketNodeService.class);
|
||||
nodeService = Sncp.createLocalService(serverClassLoader, resourceName, WebSocketNodeService.class, methodBoost,
|
||||
application.getResourceFactory(), application.getSncpRpcGroups(), sncpClient, null, (String) null, (AnyValue) null);
|
||||
(isSNCP() ? appResFactory : resourceFactory).register(resourceName, WebSocketNode.class, nodeService);
|
||||
((org.redkale.net.http.WebSocketNodeService) nodeService).setName(resourceName);
|
||||
@@ -324,10 +327,14 @@ public abstract class NodeServer {
|
||||
}
|
||||
|
||||
//ResourceFactory resfactory = (isSNCP() ? appResFactory : resourceFactory);
|
||||
Service service = Modifier.isFinal(resServiceType.getModifiers()) || Sncp.isComponent(resServiceType)
|
||||
? (Service) resServiceType.getConstructor().newInstance()
|
||||
: Sncp.createLocalService(serverClassLoader, resourceName, resServiceType,
|
||||
appResFactory, application.getSncpRpcGroups(), sncpClient, null, null, null);
|
||||
Service service;
|
||||
if (Modifier.isFinal(resServiceType.getModifiers()) || Sncp.isComponent(resServiceType)) {
|
||||
service = (Service) resServiceType.getConstructor().newInstance();
|
||||
} else {
|
||||
AsmMethodBoost methodBoost = application.createAsmMethodBoost(resServiceType);
|
||||
service = Sncp.createLocalService(serverClassLoader, resourceName, resServiceType,
|
||||
methodBoost, appResFactory, application.getSncpRpcGroups(), sncpClient, null, null, null);
|
||||
}
|
||||
appResFactory.register(resourceName, resServiceType, service);
|
||||
|
||||
field.set(srcObj, service);
|
||||
@@ -415,10 +422,14 @@ public abstract class NodeServer {
|
||||
return null;
|
||||
}
|
||||
service = serviceImplClass.getDeclaredConstructor().newInstance();
|
||||
} else if (ws || localMode) { //本地模式
|
||||
service = Sncp.createLocalService(serverClassLoader, resourceName, serviceImplClass, appResourceFactory, rpcGroups, this.sncpClient, agent, group, entry.getProperty());
|
||||
} else if (ws || localMode) { //本地模式
|
||||
AsmMethodBoost methodBoost = application.createAsmMethodBoost(serviceImplClass);
|
||||
service = Sncp.createLocalService(serverClassLoader, resourceName, serviceImplClass,
|
||||
methodBoost, appResourceFactory, rpcGroups, this.sncpClient, agent, group, entry.getProperty());
|
||||
} else {
|
||||
service = Sncp.createRemoteService(serverClassLoader, resourceName, serviceImplClass, appResourceFactory, rpcGroups, this.sncpClient, agent, group, entry.getProperty());
|
||||
AsmMethodBoost methodBoost = application.createAsmMethodBoost(serviceImplClass);
|
||||
service = Sncp.createRemoteService(serverClassLoader, resourceName, serviceImplClass,
|
||||
methodBoost, appResourceFactory, rpcGroups, this.sncpClient, agent, group, entry.getProperty());
|
||||
}
|
||||
final Class restype = Sncp.getResourceType(service);
|
||||
if (rf.find(resourceName, restype) == null) {
|
||||
|
||||
@@ -56,7 +56,7 @@ public class CacheModuleEngine extends ModuleEngine {
|
||||
//设置缓存管理器
|
||||
this.config = application.getAppConfig().getAnyValue("cache");
|
||||
this.cacheManager = createManager(this.config);
|
||||
if (this.config != null && !application.isCompileMode()) {
|
||||
if (!application.isCompileMode()) {
|
||||
this.resourceFactory.inject(this.cacheManager);
|
||||
if (this.cacheManager instanceof Service) {
|
||||
((Service) this.cacheManager).init(this.config);
|
||||
|
||||
@@ -71,6 +71,7 @@ public abstract class Sncp {
|
||||
}
|
||||
|
||||
private Sncp() {
|
||||
//do nothing
|
||||
}
|
||||
|
||||
//key: actionid
|
||||
@@ -459,11 +460,13 @@ public abstract class Sncp {
|
||||
* @param classLoader ClassLoader
|
||||
* @param name 资源名
|
||||
* @param serviceImplClass Service类
|
||||
* @param methodBoost 方法扩展
|
||||
*
|
||||
* @return Service实例
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
protected static <T extends Service> Class<? extends T> createLocalServiceClass(ClassLoader classLoader, final String name, final Class<T> serviceImplClass) {
|
||||
protected static <T extends Service> Class<? extends T> createLocalServiceClass(ClassLoader classLoader,
|
||||
final String name, final Class<T> serviceImplClass, final AsmMethodBoost methodBoost) {
|
||||
Objects.requireNonNull(serviceImplClass);
|
||||
if (!Service.class.isAssignableFrom(serviceImplClass)) {
|
||||
throw new SncpException(serviceImplClass + " is not Service type");
|
||||
@@ -576,7 +579,7 @@ public abstract class Sncp {
|
||||
}
|
||||
|
||||
public static <T extends Service> T createSimpleLocalService(Class<T> serviceImplClass, ResourceFactory resourceFactory) {
|
||||
return createLocalService(null, "", serviceImplClass, resourceFactory, null, null, null, null, null);
|
||||
return createLocalService(null, "", serviceImplClass, null, resourceFactory, null, null, null, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -587,6 +590,7 @@ public abstract class Sncp {
|
||||
* @param classLoader ClassLoader
|
||||
* @param name 资源名
|
||||
* @param serviceImplClass Service类
|
||||
* @param methodBoost 方法扩展
|
||||
* @param resourceFactory ResourceFactory
|
||||
* @param sncpRpcGroups SncpRpcGroups
|
||||
* @param client SncpClient
|
||||
@@ -601,6 +605,7 @@ public abstract class Sncp {
|
||||
final RedkaleClassLoader classLoader,
|
||||
final String name,
|
||||
final Class<T> serviceImplClass,
|
||||
final AsmMethodBoost methodBoost,
|
||||
final ResourceFactory resourceFactory,
|
||||
final SncpRpcGroups sncpRpcGroups,
|
||||
final SncpClient client,
|
||||
@@ -608,7 +613,7 @@ public abstract class Sncp {
|
||||
final String remoteGroup,
|
||||
final AnyValue conf) {
|
||||
try {
|
||||
final Class newClazz = createLocalServiceClass(classLoader, name, serviceImplClass);
|
||||
final Class newClazz = createLocalServiceClass(classLoader, name, serviceImplClass, methodBoost);
|
||||
T service = (T) newClazz.getDeclaredConstructor().newInstance();
|
||||
//--------------------------------------
|
||||
Service remoteService = null;
|
||||
@@ -630,7 +635,8 @@ public abstract class Sncp {
|
||||
field.setAccessible(true);
|
||||
RedkaleClassLoader.putReflectionField(loop.getName(), field);
|
||||
if (remoteService == null && sncpRpcGroups != null && client != null) {
|
||||
remoteService = createRemoteService(classLoader, name, serviceImplClass, resourceFactory, sncpRpcGroups, client, agent, remoteGroup, conf);
|
||||
remoteService = createRemoteService(classLoader, name, serviceImplClass,
|
||||
methodBoost, resourceFactory, sncpRpcGroups, client, agent, remoteGroup, conf);
|
||||
}
|
||||
if (remoteService != null) {
|
||||
field.set(service, remoteService);
|
||||
@@ -648,6 +654,9 @@ public abstract class Sncp {
|
||||
c.setAccessible(true);
|
||||
c.set(service, agent == null ? null : agent.getName());
|
||||
}
|
||||
if (methodBoost != null) {
|
||||
methodBoost.doInstance(service);
|
||||
}
|
||||
return service;
|
||||
} catch (RuntimeException rex) {
|
||||
throw rex;
|
||||
@@ -656,15 +665,15 @@ public abstract class Sncp {
|
||||
}
|
||||
}
|
||||
|
||||
public static <T extends Service> T createSimpleRemoteService(Class<T> serviceImplClass, ResourceFactory resourceFactory,
|
||||
SncpRpcGroups sncpRpcGroups, SncpClient client, String group) {
|
||||
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");
|
||||
}
|
||||
if (client == null) {
|
||||
throw new SncpException("SncpClient is null");
|
||||
}
|
||||
return createRemoteService(null, "", serviceImplClass, resourceFactory, sncpRpcGroups, client, null, group, null);
|
||||
return createRemoteService(null, "", serviceImplClass, null, resourceFactory, sncpRpcGroups, client, null, group, null);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -703,6 +712,7 @@ public abstract class Sncp {
|
||||
* @param classLoader ClassLoader
|
||||
* @param name 资源名
|
||||
* @param serviceTypeOrImplClass Service类
|
||||
* @param methodBoost 方法扩展
|
||||
* @param resourceFactory ResourceFactory
|
||||
* @param sncpRpcGroups SncpRpcGroups
|
||||
* @param client SncpClient
|
||||
@@ -717,6 +727,7 @@ public abstract class Sncp {
|
||||
final ClassLoader classLoader,
|
||||
final String name,
|
||||
final Class<T> serviceTypeOrImplClass,
|
||||
final AsmMethodBoost methodBoost,
|
||||
final ResourceFactory resourceFactory,
|
||||
final SncpRpcGroups sncpRpcGroups,
|
||||
final SncpClient client,
|
||||
@@ -780,6 +791,9 @@ public abstract class Sncp {
|
||||
c.setAccessible(true);
|
||||
c.set(service, info);
|
||||
}
|
||||
if (methodBoost != null) {
|
||||
methodBoost.doInstance(service);
|
||||
}
|
||||
return service;
|
||||
} catch (Throwable ex) {
|
||||
//do nothing
|
||||
@@ -984,6 +998,9 @@ public abstract class Sncp {
|
||||
c.set(service, info);
|
||||
RedkaleClassLoader.putReflectionField(newDynName.replace('/', '.'), c);
|
||||
}
|
||||
if (methodBoost != null) {
|
||||
methodBoost.doInstance(service);
|
||||
}
|
||||
return service;
|
||||
} catch (Exception ex) {
|
||||
throw new SncpException(ex);
|
||||
|
||||
@@ -9,7 +9,7 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
import org.redkale.boot.ClassFilter;
|
||||
|
||||
/**
|
||||
* 协议地址组合对象, 对应application.xml 中 resources->group 节点信息
|
||||
* 协议地址组合对象, 对应application.xml中group节点信息
|
||||
*
|
||||
* <p>
|
||||
* 详情见: https://redkale.org
|
||||
|
||||
Reference in New Issue
Block a user