cache
This commit is contained in:
@@ -468,7 +468,7 @@ public abstract class Sncp {
|
|||||||
* <blockquote>
|
* <blockquote>
|
||||||
*
|
*
|
||||||
* <pre>
|
* <pre>
|
||||||
* @Resource(name = "")
|
* @Resource(resourceName = "")
|
||||||
* @SncpDyn(remote = false)
|
* @SncpDyn(remote = false)
|
||||||
* @ResourceType(TestService.class)
|
* @ResourceType(TestService.class)
|
||||||
* public final class _DynLocalTestService extends TestService {
|
* public final class _DynLocalTestService extends TestService {
|
||||||
@@ -484,7 +484,7 @@ public abstract class Sncp {
|
|||||||
*
|
*
|
||||||
* @param <T> Service子类
|
* @param <T> Service子类
|
||||||
* @param classLoader DynBytesClassLoader
|
* @param classLoader DynBytesClassLoader
|
||||||
* @param name 资源名
|
* @param resourceName 资源名
|
||||||
* @param serviceImplClass Service类
|
* @param serviceImplClass Service类
|
||||||
* @param methodBoost 方法扩展
|
* @param methodBoost 方法扩展
|
||||||
* @return Service实例
|
* @return Service实例
|
||||||
@@ -492,14 +492,14 @@ 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 classLoader,
|
RedkaleClassLoader classLoader,
|
||||||
final String name,
|
final String resourceName,
|
||||||
final Class<T> serviceImplClass,
|
final Class<T> serviceImplClass,
|
||||||
final AsmMethodBoost methodBoost) {
|
final AsmMethodBoost methodBoost) {
|
||||||
Objects.requireNonNull(serviceImplClass);
|
Objects.requireNonNull(serviceImplClass);
|
||||||
if (!Service.class.isAssignableFrom(serviceImplClass)) {
|
if (!Service.class.isAssignableFrom(serviceImplClass)) {
|
||||||
throw new SncpException(serviceImplClass + " is not Service type");
|
throw new SncpException(serviceImplClass + " is not Service type");
|
||||||
}
|
}
|
||||||
ResourceFactory.checkResourceName(name);
|
ResourceFactory.checkResourceName(resourceName);
|
||||||
int mod = serviceImplClass.getModifiers();
|
int mod = serviceImplClass.getModifiers();
|
||||||
if (!java.lang.reflect.Modifier.isPublic(mod)) {
|
if (!java.lang.reflect.Modifier.isPublic(mod)) {
|
||||||
throw new SncpException(serviceImplClass + " is not public");
|
throw new SncpException(serviceImplClass + " is not public");
|
||||||
@@ -515,9 +515,9 @@ public abstract class Sncp {
|
|||||||
// serviceImplClass.getSimpleName();
|
// serviceImplClass.getSimpleName();
|
||||||
String newDynName = "org/redkaledyn/service/local/_DynLocalService__"
|
String newDynName = "org/redkaledyn/service/local/_DynLocalService__"
|
||||||
+ serviceImplClass.getName().replace('.', '_').replace('$', '_');
|
+ serviceImplClass.getName().replace('.', '_').replace('$', '_');
|
||||||
if (!name.isEmpty()) {
|
if (!resourceName.isEmpty()) {
|
||||||
boolean normal = true;
|
boolean normal = true;
|
||||||
for (char ch : name.toCharArray()) {
|
for (char ch : resourceName.toCharArray()) {
|
||||||
if (!((ch >= '0' && ch <= '9') || ch == '_' || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))) {
|
if (!((ch >= '0' && ch <= '9') || ch == '_' || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))) {
|
||||||
normal = false;
|
normal = false;
|
||||||
}
|
}
|
||||||
@@ -525,11 +525,11 @@ public abstract class Sncp {
|
|||||||
if (!normal) {
|
if (!normal) {
|
||||||
throw new SncpException(serviceImplClass + "'s resource name is illegal, must be 0-9 _ a-z A-Z");
|
throw new SncpException(serviceImplClass + "'s resource name is illegal, must be 0-9 _ a-z A-Z");
|
||||||
}
|
}
|
||||||
newDynName += "_" + (normal ? name : hash(name));
|
newDynName += "_" + (normal ? resourceName : hash(resourceName));
|
||||||
}
|
}
|
||||||
// if (Utility.inNativeImage() || methodBoost == null) { // 加强动态时不能重复加载
|
// if (Utility.inNativeImage() || methodBoost == null) { // 加强动态时不能重复加载
|
||||||
try {
|
try {
|
||||||
return (Class<T>)classLoader.loadClass(newDynName.replace('/', '.'));
|
return (Class<T>) classLoader.loadClass(newDynName.replace('/', '.'));
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
// do nothing
|
// do nothing
|
||||||
}
|
}
|
||||||
@@ -543,7 +543,7 @@ public abstract class Sncp {
|
|||||||
cw.visit(V11, ACC_PUBLIC + ACC_SUPER, newDynName, null, supDynName, null);
|
cw.visit(V11, ACC_PUBLIC + ACC_SUPER, newDynName, null, supDynName, null);
|
||||||
{ // 给动态生成的Service类标记上Resource
|
{ // 给动态生成的Service类标记上Resource
|
||||||
av0 = cw.visitAnnotation(resDesc, true);
|
av0 = cw.visitAnnotation(resDesc, true);
|
||||||
av0.visit("name", name);
|
av0.visit("name", resourceName);
|
||||||
av0.visitEnd();
|
av0.visitEnd();
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
@@ -736,7 +736,7 @@ public abstract class Sncp {
|
|||||||
*
|
*
|
||||||
* @param <T> Service泛型
|
* @param <T> Service泛型
|
||||||
* @param classLoader ClassLoader
|
* @param classLoader ClassLoader
|
||||||
* @param name 资源名
|
* @param resourceName 资源名
|
||||||
* @param serviceImplClass Service类
|
* @param serviceImplClass Service类
|
||||||
* @param methodBoost 方法扩展
|
* @param methodBoost 方法扩展
|
||||||
* @param resourceFactory ResourceFactory
|
* @param resourceFactory ResourceFactory
|
||||||
@@ -750,7 +750,7 @@ public abstract class Sncp {
|
|||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public static <T extends Service> T createLocalService(
|
public static <T extends Service> T createLocalService(
|
||||||
final RedkaleClassLoader classLoader,
|
final RedkaleClassLoader classLoader,
|
||||||
final String name,
|
final String resourceName,
|
||||||
final Class<T> serviceImplClass,
|
final Class<T> serviceImplClass,
|
||||||
final AsmMethodBoost methodBoost,
|
final AsmMethodBoost methodBoost,
|
||||||
final ResourceFactory resourceFactory,
|
final ResourceFactory resourceFactory,
|
||||||
@@ -760,7 +760,7 @@ public abstract class Sncp {
|
|||||||
final String remoteGroup,
|
final String remoteGroup,
|
||||||
final AnyValue conf) {
|
final AnyValue conf) {
|
||||||
try {
|
try {
|
||||||
final Class newClazz = createLocalServiceClass(classLoader, name, serviceImplClass, methodBoost);
|
final Class newClazz = createLocalServiceClass(classLoader, resourceName, serviceImplClass, methodBoost);
|
||||||
T service = (T) newClazz.getDeclaredConstructor().newInstance();
|
T service = (T) newClazz.getDeclaredConstructor().newInstance();
|
||||||
// --------------------------------------
|
// --------------------------------------
|
||||||
Service remoteService = null;
|
Service remoteService = null;
|
||||||
@@ -784,7 +784,7 @@ public abstract class Sncp {
|
|||||||
if (remoteService == null && sncpRpcGroups != null && client != null) {
|
if (remoteService == null && sncpRpcGroups != null && client != null) {
|
||||||
remoteService = createRemoteService(
|
remoteService = createRemoteService(
|
||||||
classLoader,
|
classLoader,
|
||||||
name,
|
resourceName,
|
||||||
serviceImplClass,
|
serviceImplClass,
|
||||||
methodBoost,
|
methodBoost,
|
||||||
resourceFactory,
|
resourceFactory,
|
||||||
@@ -845,7 +845,7 @@ public abstract class Sncp {
|
|||||||
* <blockquote>
|
* <blockquote>
|
||||||
*
|
*
|
||||||
* <pre>
|
* <pre>
|
||||||
* @Resource(name = "")
|
* @Resource(resourceName = "")
|
||||||
* @SncpDyn(remote = true)
|
* @SncpDyn(remote = true)
|
||||||
* @ResourceType(TestService.class)
|
* @ResourceType(TestService.class)
|
||||||
* public final class _DynRemoteTestService extends TestService {
|
* public final class _DynRemoteTestService extends TestService {
|
||||||
@@ -879,7 +879,7 @@ public abstract class Sncp {
|
|||||||
*
|
*
|
||||||
* @param <T> Service泛型
|
* @param <T> Service泛型
|
||||||
* @param classLoader ClassLoader
|
* @param classLoader ClassLoader
|
||||||
* @param name 资源名
|
* @param resourceName 资源名
|
||||||
* @param serviceTypeOrImplClass Service类
|
* @param serviceTypeOrImplClass Service类
|
||||||
* @param methodBoost 方法扩展
|
* @param methodBoost 方法扩展
|
||||||
* @param resourceFactory ResourceFactory
|
* @param resourceFactory ResourceFactory
|
||||||
@@ -893,7 +893,7 @@ public abstract class Sncp {
|
|||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public static <T extends Service> T createRemoteService(
|
public static <T extends Service> T createRemoteService(
|
||||||
final RedkaleClassLoader classLoader,
|
final RedkaleClassLoader classLoader,
|
||||||
final String name,
|
final String resourceName,
|
||||||
final Class<T> serviceTypeOrImplClass,
|
final Class<T> serviceTypeOrImplClass,
|
||||||
final AsmMethodBoost methodBoost,
|
final AsmMethodBoost methodBoost,
|
||||||
final ResourceFactory resourceFactory,
|
final ResourceFactory resourceFactory,
|
||||||
@@ -912,14 +912,14 @@ public abstract class Sncp {
|
|||||||
throw new SncpException(SncpRpcGroups.class.getSimpleName() + "/" + SncpClient.class.getSimpleName()
|
throw new SncpException(SncpRpcGroups.class.getSimpleName() + "/" + SncpClient.class.getSimpleName()
|
||||||
+ " and " + MessageAgent.class.getSimpleName() + " are both null");
|
+ " and " + MessageAgent.class.getSimpleName() + " are both null");
|
||||||
}
|
}
|
||||||
ResourceFactory.checkResourceName(name);
|
ResourceFactory.checkResourceName(resourceName);
|
||||||
final int mod = serviceTypeOrImplClass.getModifiers();
|
final int mod = serviceTypeOrImplClass.getModifiers();
|
||||||
boolean realed = !(java.lang.reflect.Modifier.isAbstract(mod) || serviceTypeOrImplClass.isInterface());
|
boolean realed = !(java.lang.reflect.Modifier.isAbstract(mod) || serviceTypeOrImplClass.isInterface());
|
||||||
if (!java.lang.reflect.Modifier.isPublic(mod)) {
|
if (!java.lang.reflect.Modifier.isPublic(mod)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
final SncpRemoteInfo info = createSncpRemoteInfo(
|
final SncpRemoteInfo info = createSncpRemoteInfo(
|
||||||
name,
|
resourceName,
|
||||||
getResourceType(serviceTypeOrImplClass),
|
getResourceType(serviceTypeOrImplClass),
|
||||||
serviceTypeOrImplClass,
|
serviceTypeOrImplClass,
|
||||||
ProtobufConvert.root(),
|
ProtobufConvert.root(),
|
||||||
@@ -935,9 +935,9 @@ public abstract class Sncp {
|
|||||||
final String anyValueDesc = Type.getDescriptor(AnyValue.class);
|
final String anyValueDesc = Type.getDescriptor(AnyValue.class);
|
||||||
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 (!resourceName.isEmpty()) {
|
||||||
boolean normal = true;
|
boolean normal = true;
|
||||||
for (char ch : name.toCharArray()) {
|
for (char ch : resourceName.toCharArray()) {
|
||||||
if (!((ch >= '0' && ch <= '9') || ch == '_' || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))) {
|
if (!((ch >= '0' && ch <= '9') || ch == '_' || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))) {
|
||||||
normal = false;
|
normal = false;
|
||||||
}
|
}
|
||||||
@@ -945,7 +945,7 @@ public abstract class Sncp {
|
|||||||
if (!normal) {
|
if (!normal) {
|
||||||
throw new SncpException(serviceTypeOrImplClass + "'s resource name is illegal, must be 0-9 _ a-z A-Z");
|
throw new SncpException(serviceTypeOrImplClass + "'s resource name is illegal, must be 0-9 _ a-z A-Z");
|
||||||
}
|
}
|
||||||
newDynName += "_" + (normal ? name : hash(name));
|
newDynName += "_" + (normal ? resourceName : hash(resourceName));
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
Class newClazz = classLoader.loadClass(newDynName.replace('/', '.'));
|
Class newClazz = classLoader.loadClass(newDynName.replace('/', '.'));
|
||||||
@@ -988,7 +988,7 @@ public abstract class Sncp {
|
|||||||
serviceTypeOrImplClass.isInterface() ? new String[] {supDynName} : null);
|
serviceTypeOrImplClass.isInterface() ? new String[] {supDynName} : null);
|
||||||
{ // 给动态生成的Service类标记上Resource
|
{ // 给动态生成的Service类标记上Resource
|
||||||
av0 = cw.visitAnnotation(resDesc, true);
|
av0 = cw.visitAnnotation(resDesc, true);
|
||||||
av0.visit("name", name);
|
av0.visit("name", resourceName);
|
||||||
av0.visitEnd();
|
av0.visitEnd();
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -49,6 +49,7 @@ public class CachedInstanceTest {
|
|||||||
classLoader = RedkaleClassLoader.currentClassLoader();
|
classLoader = RedkaleClassLoader.currentClassLoader();
|
||||||
CacheMemorySource remoteSource = new CacheMemorySource("cache-remote");
|
CacheMemorySource remoteSource = new CacheMemorySource("cache-remote");
|
||||||
remoteSource.init(null);
|
remoteSource.init(null);
|
||||||
|
|
||||||
resourceFactory = ResourceFactory.create();
|
resourceFactory = ResourceFactory.create();
|
||||||
resourceFactory.register(new Environment());
|
resourceFactory.register(new Environment());
|
||||||
manager = CachedManagerService.create(remoteSource);
|
manager = CachedManagerService.create(remoteSource);
|
||||||
@@ -64,28 +65,19 @@ public class CachedInstanceTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void run1() throws Exception {
|
public void run1() throws Exception {
|
||||||
ClassLoader parent = Thread.currentThread().getContextClassLoader();
|
RedkaleClassLoader classLoader = RedkaleClassLoader.currentClassLoader();
|
||||||
Class<CachedInstance> serviceClass = CachedInstance.class;
|
Class<CachedInstance> instanceClass = CachedInstance.class;
|
||||||
CachedAsmMethodBoost boost = new CachedAsmMethodBoost(false, serviceClass);
|
CachedAsmMethodBoost boost = new CachedAsmMethodBoost(false, instanceClass);
|
||||||
CachedAsmMethodBoost boost2 = new CachedAsmMethodBoost(false, serviceClass);
|
CachedAsmMethodBoost boost2 = new CachedAsmMethodBoost(false, instanceClass);
|
||||||
SncpRpcGroups grous = new SncpRpcGroups();
|
SncpRpcGroups grous = new SncpRpcGroups();
|
||||||
AsyncGroup iGroup = AsyncGroup.create("", Utility.newScheduledExecutor(1), 0, 0);
|
AsyncGroup iGroup = AsyncGroup.create("", Utility.newScheduledExecutor(1), 0, 0);
|
||||||
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(
|
||||||
classLoader, "", serviceClass, boost, resourceFactory, grous, client, null, null, null);
|
classLoader, "a", instanceClass, boost, resourceFactory, grous, client, null, null, null);
|
||||||
resourceFactory.inject(instance);
|
resourceFactory.inject(instance);
|
||||||
CachedInstance instance2 = Sncp.createLocalService(
|
CachedInstance instance2 = Sncp.createLocalService(
|
||||||
new RedkaleClassLoader(parent),
|
classLoader, "b", instanceClass, boost2, resourceFactory2, grous, client, null, null, null);
|
||||||
"",
|
|
||||||
serviceClass,
|
|
||||||
boost2,
|
|
||||||
resourceFactory2,
|
|
||||||
grous,
|
|
||||||
client,
|
|
||||||
null,
|
|
||||||
null,
|
|
||||||
null);
|
|
||||||
resourceFactory2.inject(instance2);
|
resourceFactory2.inject(instance2);
|
||||||
|
|
||||||
System.out.println(instance.getName2());
|
System.out.println(instance.getName2());
|
||||||
|
|||||||
Reference in New Issue
Block a user