use of rpc.turbo.annotation.TurboService in project turbo-rpc by hank-whu.
the class MethodConfig method version.
private String version(Method method) {
String version = TurboService.DEFAULT_VERSION;
TurboService config = method.getDeclaringClass().getAnnotation(TurboService.class);
if (config == null) {
config = method.getAnnotation(TurboService.class);
}
if (config != null) {
version = config.version();
}
int delimterIndex = version.indexOf('.');
if (delimterIndex > 0) {
version = version.substring(0, delimterIndex);
}
return version;
}
use of rpc.turbo.annotation.TurboService in project turbo-rpc by hank-whu.
the class MethodConfig method timeout.
private long timeout(Method method) {
long timeout = TurboService.DEFAULT_TIME_OUT;
TurboService config = method.getDeclaringClass().getAnnotation(TurboService.class);
if (config == null) {
config = method.getAnnotation(TurboService.class);
}
if (config != null) {
timeout = config.timeout();
}
if (timeout < 1) {
timeout = TurboService.DEFAULT_TIME_OUT;
}
return timeout;
}
use of rpc.turbo.annotation.TurboService in project turbo-rpc by hank-whu.
the class ServerInvokerFactory method convertToInvokerStream.
private <T> Stream<JavassistInvoker<T>> convertToInvokerStream(Class<?> clazz, Object service, AtomicInteger serviceIdCounter) {
if (clazz == null) {
throw new InvokeException("clazz cannot be null");
}
if (service == null) {
throw new InvokeException("service cannot be null");
}
if (!clazz.isInstance(service)) {
throw new InvokeException("clazz is the interface, service is the implemention");
}
if (!clazz.isInterface()) {
throw new InvokeException("the clazz must be interface");
}
if (!Modifier.isPublic(clazz.getModifiers())) {
throw new InvokeException("the clazz must be public");
}
TurboService classConfig = clazz.getAnnotation(TurboService.class);
if (classConfig != null && classConfig.ignore()) {
if (logger.isInfoEnabled()) {
logger.info(clazz + " ignore");
}
return Stream.empty();
}
if (classRegisterMap.containsKey(InvokerUtils.getServiceClassName(group, app, clazz))) {
return Stream.empty();
}
Method[] allMethods = clazz.getMethods();
for (Method method : allMethods) {
if (!CompletableFuture.class.equals(method.getReturnType())) {
throw new RuntimeException("method return-type must be CompletableFuture, " + InvokerUtils.getServiceMethodName(group, app, method));
}
}
classRegisterMap.put(InvokerUtils.getServiceClassName(group, app, clazz), Boolean.TRUE);
if (logger.isInfoEnabled()) {
logger.info("service " + clazz + " register");
}
Stream<Method> methodStream = //
Stream.of(//
allMethods).filter(//
m -> Modifier.isPublic(m.getModifiers())).filter(//
m -> !Modifier.isStatic(m.getModifiers())).peek(m -> {
if (!CompletableFuture.class.equals(m.getReturnType())) {
throw new RuntimeException("method return-type must be CompletableFuture, " + InvokerUtils.getServiceMethodName(group, app, m));
}
}).filter(m -> {
TurboService methodConfig = m.getAnnotation(TurboService.class);
if (methodConfig != null && methodConfig.ignore()) {
if (logger.isInfoEnabled()) {
logger.info(InvokerUtils.getServiceMethodName(group, app, m) + " ignore");
return false;
}
}
return true;
});
// 特殊处理,保证编号正确性
if (TurboConnectService.class.equals(clazz)) {
methodStream = methodStream.sorted((m0, m1) -> {
int sort0 = TurboConnectService.serviceOrderMap.get(m0.getName());
int sort1 = TurboConnectService.serviceOrderMap.get(m1.getName());
return sort0 - sort1;
});
}
Stream<JavassistInvoker<T>> invokerStream = //
methodStream.map(m -> {
int serviceId = serviceIdCounter.getAndIncrement();
if (logger.isInfoEnabled()) {
logger.info(InvokerUtils.getServiceMethodName(group, app, m) + " serviceId:" + serviceId);
}
return new JavassistInvoker<>(serviceId, service, m.getDeclaringClass(), m);
});
return invokerStream;
}
use of rpc.turbo.annotation.TurboService in project turbo-rpc by hank-whu.
the class MethodConfig method ignore.
private boolean ignore(Method method) {
boolean ignore = TurboService.DEFAULT_IGNORE;
TurboService config = method.getDeclaringClass().getAnnotation(TurboService.class);
if (config != null) {
ignore = config.ignore();
}
if (!ignore) {
config = method.getAnnotation(TurboService.class);
if (config != null) {
ignore = config.ignore();
}
}
return ignore;
}
use of rpc.turbo.annotation.TurboService in project turbo-rpc by hank-whu.
the class TurboClientStarter method tryInjectTurboServiceField.
private void tryInjectTurboServiceField(Class<?> beanClass, Object bean) {
Field[] fields = beanClass.getDeclaredFields();
for (int j = 0; j < fields.length; j++) {
Field field = fields[j];
Class<?> fieldClass = field.getType();
if (!fieldClass.isInterface()) {
// 只支持接口
continue;
}
TurboService turboService = fieldClass.getAnnotation(TurboService.class);
if (turboService == null) {
continue;
}
Object serviceBean = turboClient.getService(fieldClass);
if (serviceBean == null) {
turboClient.register(fieldClass);
turboClient.setFailover(fieldClass, null);
serviceBean = turboClient.getService(fieldClass);
}
try {
field.setAccessible(true);
field.set(bean, serviceBean);
} catch (Throwable t) {
if (logger.isErrorEnabled()) {
logger.error("手动注入TurboService失败," + field, t);
}
continue;
}
}
}
Aggregations