This commit is contained in:
Redkale
2017-05-27 19:34:35 +08:00
parent 88672b5522
commit 054e853074
11 changed files with 35 additions and 87 deletions

View File

@@ -361,12 +361,11 @@ public abstract class NodeServer {
} }
if (SncpClient.parseMethod(serviceImplClass).isEmpty()) return; //class没有可用的方法 通常为BaseService if (SncpClient.parseMethod(serviceImplClass).isEmpty()) return; //class没有可用的方法 通常为BaseService
//final ServiceWrapper wrapper = new ServiceWrapper(serviceImplClass, service, resourceName, localed ? NodeServer.this.sncpGroup : null, groups, entry.getProperty()); //final ServiceWrapper wrapper = new ServiceWrapper(serviceImplClass, service, resourceName, localed ? NodeServer.this.sncpGroup : null, groups, entry.getProperty());
for (final Class restype : Sncp.getResourceTypes(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 (isSNCP() && !entry.isAutoload()) { } else if (isSNCP() && !entry.isAutoload()) {
throw new RuntimeException(restype.getSimpleName() + "(class:" + serviceImplClass.getName() + ", name:" + resourceName + ", group:" + groups + ") is repeat."); throw new RuntimeException(restype.getSimpleName() + "(class:" + serviceImplClass.getName() + ", name:" + resourceName + ", group:" + groups + ") is repeat.");
}
} }
if (Sncp.isRemote(service)) { if (Sncp.isRemote(service)) {
remoteServices.add(service); remoteServices.add(service);
@@ -384,10 +383,7 @@ public abstract class NodeServer {
}; };
if (entry.isExpect()) { if (entry.isExpect()) {
ResourceType rty = entry.getType().getAnnotation(ResourceType.class); ResourceType rty = entry.getType().getAnnotation(ResourceType.class);
Class[] resTypes = rty == null ? new Class[]{} : rty.value(); resourceFactory.register(resourceLoader, rty == null ? entry.getType() : rty.value());
for (final Class restype : resTypes) {
resourceFactory.register(resourceLoader, restype);
}
} else { } else {
resourceLoader.load(resourceFactory, null, entry.getName(), null, false); resourceLoader.load(resourceFactory, null, entry.getName(), null, false);
} }
@@ -416,7 +412,7 @@ public abstract class NodeServer {
//----------------- init ----------------- //----------------- init -----------------
List<Service> swlist = new ArrayList<>(localServices); List<Service> swlist = new ArrayList<>(localServices);
Collections.sort(swlist, (o1, o2) -> { Collections.sort(swlist, (o1, o2) -> {
int rs = Sncp.getResourceTypes(o1)[0].getName().compareTo(Sncp.getResourceTypes(o2)[0].getName()); int rs = Sncp.getResourceType(o1).getName().compareTo(Sncp.getResourceType(o2).getName());
if (rs == 0) rs = Sncp.getResourceName(o1).compareTo(Sncp.getResourceName(o2)); if (rs == 0) rs = Sncp.getResourceName(o1).compareTo(Sncp.getResourceName(o2));
return rs; return rs;
}); });
@@ -448,16 +444,7 @@ public abstract class NodeServer {
private void calcMaxLength(Service y) { //计算toString中的长度 private void calcMaxLength(Service y) { //计算toString中的长度
maxNameLength = Math.max(maxNameLength, Sncp.getResourceName(y).length()); maxNameLength = Math.max(maxNameLength, Sncp.getResourceName(y).length());
StringBuilder s = new StringBuilder(); maxClassNameLength = Math.max(maxClassNameLength, Sncp.getResourceType(y).getName().length() + 1);
Class[] types = Sncp.getResourceTypes(y);
if (types.length == 1) {
s.append(types[0].getName());
} else {
s.append('[');
s.append(Arrays.asList(types).stream().map((Class t) -> t.getName()).collect(Collectors.joining(",")));
s.append(']');
}
maxClassNameLength = Math.max(maxClassNameLength, s.length() + 1);
} }
protected List<Transport> loadTransports(final HashSet<String> groups) { protected List<Transport> loadTransports(final HashSet<String> groups) {

View File

@@ -11,7 +11,6 @@ import java.net.InetSocketAddress;
import java.security.*; import java.security.*;
import java.util.*; import java.util.*;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.stream.Collectors;
import javax.annotation.Resource; import javax.annotation.Resource;
import static jdk.internal.org.objectweb.asm.ClassWriter.COMPUTE_FRAMES; import static jdk.internal.org.objectweb.asm.ClassWriter.COMPUTE_FRAMES;
import jdk.internal.org.objectweb.asm.*; import jdk.internal.org.objectweb.asm.*;
@@ -110,10 +109,10 @@ public abstract class Sncp {
} }
} }
public static Class[] getResourceTypes(Service service) { public static Class getResourceType(Service service) {
if (service == null) return null; if (service == null) return null;
ResourceType types = service.getClass().getAnnotation(ResourceType.class); ResourceType type = service.getClass().getAnnotation(ResourceType.class);
return types == null ? new Class[]{getServiceType(service)} : types.value(); return type == null ? getServiceType(service) : type.value();
} }
public static String getSncpGroup(Service service) { public static String getSncpGroup(Service service) {
@@ -224,20 +223,10 @@ public abstract class Sncp {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.append(isRemote(service) ? "RemoteService" : "LocalService "); sb.append(isRemote(service) ? "RemoteService" : "LocalService ");
int len; int len;
Class[] types = getResourceTypes(service); Class type = getResourceType(service);
String name = getResourceName(service); String name = getResourceName(service);
if (types.length == 1) { sb.append("(type= ").append(type.getName());
sb.append("(type= ").append(types[0].getName()); len = maxClassNameLength - type.getName().length();
len = maxClassNameLength - types[0].getName().length();
} else {
StringBuilder s = new StringBuilder();
s.append('[');
s.append(Arrays.asList(types).stream().map((Class t) -> t.getName()).collect(Collectors.joining(",")));
s.append(']');
sb.append("(types=").append(s);
len = maxClassNameLength - s.length();
}
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
sb.append(' '); sb.append(' ');
} }
@@ -272,7 +261,7 @@ public abstract class Sncp {
* <blockquote><pre> * <blockquote><pre>
* &#64;Resource(name = "") * &#64;Resource(name = "")
* &#64;SncpDyn(remote = false) * &#64;SncpDyn(remote = false)
* &#64;ResourceType({TestService.class}) * &#64;ResourceType(TestService.class)
* public final class _DynLocalTestService extends TestService{ * public final class _DynLocalTestService extends TestService{
* *
* private static final Class _redkale_service_type = TestService.class; * private static final Class _redkale_service_type = TestService.class;
@@ -396,18 +385,8 @@ public abstract class Sncp {
} }
{ {
av0 = cw.visitAnnotation(Type.getDescriptor(ResourceType.class), true); av0 = cw.visitAnnotation(Type.getDescriptor(ResourceType.class), true);
{ ResourceType rty = serviceImplClass.getAnnotation(ResourceType.class);
AnnotationVisitor av1 = av0.visitArray("value"); av0.visit("value", Type.getType(Type.getDescriptor(rty == null ? serviceImplClass : rty.value())));
ResourceType rty = serviceImplClass.getAnnotation(ResourceType.class);
if (rty == null) {
av1.visit(null, Type.getType(Type.getDescriptor(serviceImplClass)));
} else {
for (Class cl : rty.value()) {
av1.visit(null, Type.getType(Type.getDescriptor(cl)));
}
}
av1.visitEnd();
}
av0.visitEnd(); av0.visitEnd();
} }
{ {
@@ -898,7 +877,7 @@ public abstract class Sncp {
* @param executor 线程池 * @param executor 线程池
* @param resourceFactory 资源容器 * @param resourceFactory 资源容器
* @param serviceImplClass Service类 * @param serviceImplClass Service类
* @param clientSncpAddress 本地IP地址 * @param clientSncpAddress 本地IP地址
* @param sncpGroup 自身的组节点名 可能为null * @param sncpGroup 自身的组节点名 可能为null
* @param groups 所有的组节点,包含自身 * @param groups 所有的组节点,包含自身
* @param conf 启动配置项 * @param conf 启动配置项
@@ -1043,7 +1022,7 @@ public abstract class Sncp {
* <blockquote><pre> * <blockquote><pre>
* &#64;Resource(name = "") * &#64;Resource(name = "")
* &#64;SncpDyn(remote = true) * &#64;SncpDyn(remote = true)
* &#64;ResourceType({TestService.class}) * &#64;ResourceType(TestService.class)
* public final class _DynRemoteTestService extends TestService{ * public final class _DynRemoteTestService extends TestService{
* *
* private static final Class _redkale_service_type = TestService.class; * private static final Class _redkale_service_type = TestService.class;
@@ -1178,18 +1157,8 @@ public abstract class Sncp {
} }
{ {
av0 = cw.visitAnnotation(Type.getDescriptor(ResourceType.class), true); av0 = cw.visitAnnotation(Type.getDescriptor(ResourceType.class), true);
{ ResourceType rty = serviceTypeOrImplClass.getAnnotation(ResourceType.class);
AnnotationVisitor av1 = av0.visitArray("value"); av0.visit("value", Type.getType(Type.getDescriptor(rty == null ? serviceTypeOrImplClass : rty.value())));
ResourceType rty = serviceTypeOrImplClass.getAnnotation(ResourceType.class);
if (rty == null) {
av1.visit(null, Type.getType(Type.getDescriptor(serviceTypeOrImplClass)));
} else {
for (Class cl : rty.value()) {
av1.visit(null, Type.getType(Type.getDescriptor(cl)));
}
}
av1.visitEnd();
}
av0.visitEnd(); av0.visitEnd();
} }
{ {

View File

@@ -172,7 +172,7 @@ public final class SncpClient {
this.name = serviceName; this.name = serviceName;
Class tn = serviceTypeOrImplClass; Class tn = serviceTypeOrImplClass;
ResourceType rt = (ResourceType) tn.getAnnotation(ResourceType.class); ResourceType rt = (ResourceType) tn.getAnnotation(ResourceType.class);
if (rt != null && rt.value().length > 0) tn = rt.value()[0]; if (rt != null) tn = rt.value();
this.serviceid = Sncp.hash(tn.getName() + ':' + serviceName); this.serviceid = Sncp.hash(tn.getName() + ':' + serviceName);
final List<SncpAction> methodens = new ArrayList<>(); final List<SncpAction> methodens = new ArrayList<>();
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------

View File

@@ -94,19 +94,13 @@ public class SncpServer extends Server<DLong, SncpContext, SncpRequest, SncpResp
* @return SncpServlet * @return SncpServlet
*/ */
public SncpServlet removeSncpServlet(Service sncpService) { public SncpServlet removeSncpServlet(Service sncpService) {
SncpServlet servlet = null;
String resname = Sncp.getResourceName(sncpService); String resname = Sncp.getResourceName(sncpService);
for (Class type : Sncp.getResourceTypes(sncpService)) { return ((SncpPrepareServlet) this.prepare).removeSncpServlet(resname, Sncp.getResourceType(sncpService));
servlet = ((SncpPrepareServlet) this.prepare).removeSncpServlet(resname, type);
}
return servlet;
} }
public void addSncpServlet(Service sncpService) { public void addSncpServlet(Service sncpService) {
for (Class type : Sncp.getResourceTypes(sncpService)) { SncpDynServlet sds = new SncpDynServlet(BsonFactory.root().getConvert(), Sncp.getResourceName(sncpService), Sncp.getResourceType(sncpService), sncpService);
SncpDynServlet sds = new SncpDynServlet(BsonFactory.root().getConvert(), Sncp.getResourceName(sncpService), type, sncpService); this.prepare.addServlet(sds, null, Sncp.getConf(sncpService));
this.prepare.addServlet(sds, null, Sncp.getConf(sncpService));
}
} }
public <T extends Service> void addSncpServlet(Class<T> serviceTypeClass, String name, T service, AnyValue conf) { public <T extends Service> void addSncpServlet(Class<T> serviceTypeClass, String name, T service, AnyValue conf) {

View File

@@ -19,7 +19,7 @@ import org.redkale.util.*;
* @author zhangjx * @author zhangjx
*/ */
@AutoLoad(false) @AutoLoad(false)
@ResourceType({DataCacheListenerService.class, DataCacheListener.class}) @ResourceType(DataCacheListener.class)
public class DataCacheListenerService implements DataCacheListener, Service { public class DataCacheListenerService implements DataCacheListener, Service {
@Resource(name = "$") @Resource(name = "$")

View File

@@ -21,7 +21,7 @@ import org.redkale.util.*;
* @author zhangjx * @author zhangjx
*/ */
@AutoLoad(false) @AutoLoad(false)
@ResourceType({WebSocketNode.class, WebSocketNodeService.class}) @ResourceType(WebSocketNode.class)
public class WebSocketNodeService extends WebSocketNode implements Service { public class WebSocketNodeService extends WebSocketNode implements Service {
@Override @Override

View File

@@ -31,7 +31,7 @@ import org.redkale.util.*;
*/ */
@Local @Local
@AutoLoad(false) @AutoLoad(false)
@ResourceType({CacheSource.class}) @ResourceType(CacheSource.class)
public class CacheMemorySource<K extends Serializable, V extends Object> extends AbstractService implements CacheSource<K, V>, Service, AutoCloseable, Resourcable { public class CacheMemorySource<K extends Serializable, V extends Object> extends AbstractService implements CacheSource<K, V>, Service, AutoCloseable, Resourcable {
@Resource(name = "APP_HOME") @Resource(name = "APP_HOME")

View File

@@ -28,7 +28,7 @@ import org.redkale.util.*;
@Local @Local
@AutoLoad(false) @AutoLoad(false)
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@ResourceType({DataSource.class}) @ResourceType(DataSource.class)
public class DataJdbcSource extends AbstractService implements DataSource, Service, DataCacheListener, Function<Class, EntityInfo>, AutoCloseable, Resourcable { public class DataJdbcSource extends AbstractService implements DataSource, Service, DataCacheListener, Function<Class, EntityInfo>, AutoCloseable, Resourcable {
private static final Flipper FLIPPER_ONE = new Flipper(1); private static final Flipper FLIPPER_ONE = new Flipper(1);

View File

@@ -176,10 +176,8 @@ public final class ResourceFactory {
return (A) register(autoSync, name, claz, rs); return (A) register(autoSync, name, claz, rs);
} else { } else {
A old = null; A old = null;
for (Class cl : rtype.value()) { A t = (A) register(autoSync, name, rtype.value(), rs);
A t = (A) register(autoSync, name, cl, rs); if (t != null) old = t;
if (t != null) old = t;
}
return old; return old;
} }
} }

View File

@@ -25,5 +25,5 @@ import java.lang.annotation.*;
@Retention(RUNTIME) @Retention(RUNTIME)
public @interface ResourceType { public @interface ResourceType {
Class[] value(); Class value();
} }

View File

@@ -18,7 +18,7 @@ import org.redkale.util.*;
* *
* @author zhangjx * @author zhangjx
*/ */
@ResourceType({SncpTestIService.class}) @ResourceType(SncpTestIService.class)
public class SncpTestServiceImpl implements SncpTestIService { public class SncpTestServiceImpl implements SncpTestIService {
@Override @Override
@@ -107,7 +107,7 @@ public class SncpTestServiceImpl implements SncpTestIService {
System.out.println(method); System.out.println(method);
} }
System.out.println("-----------------------------------"); System.out.println("-----------------------------------");
service = Sncp.createSimpleRemoteService("", SncpTestServiceImpl.class, new InetSocketAddress("127.0.0.1", 7070), null); service = Sncp.createSimpleRemoteService("", SncpTestServiceImpl.class, new InetSocketAddress("127.0.0.1", 7070), null);
for (Method method : service.getClass().getDeclaredMethods()) { for (Method method : service.getClass().getDeclaredMethods()) {
System.out.println(method); System.out.println(method);
} }
@@ -116,7 +116,7 @@ public class SncpTestServiceImpl implements SncpTestIService {
System.out.println(method); System.out.println(method);
} }
System.out.println("-----------------------------------"); System.out.println("-----------------------------------");
service = Sncp.createSimpleRemoteService("", SncpTestIService.class, new InetSocketAddress("127.0.0.1", 7070), null); service = Sncp.createSimpleRemoteService("", SncpTestIService.class, new InetSocketAddress("127.0.0.1", 7070), null);
for (Method method : service.getClass().getDeclaredMethods()) { for (Method method : service.getClass().getDeclaredMethods()) {
System.out.println(method); System.out.println(method);
} }