Search in sources :

Example 1 with TurboService

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;
}
Also used : TurboService(rpc.turbo.annotation.TurboService)

Example 2 with TurboService

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;
}
Also used : TurboService(rpc.turbo.annotation.TurboService)

Example 3 with TurboService

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;
}
Also used : ConcurrentArrayList(rpc.turbo.util.concurrent.ConcurrentArrayList) TurboService(rpc.turbo.annotation.TurboService) ConcurrentIntToObjectArrayMap(rpc.turbo.util.concurrent.ConcurrentIntToObjectArrayMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Streams(com.google.common.collect.Streams) Collectors(java.util.stream.Collectors) TurboConnectServiceServerImpl(rpc.turbo.server.TurboConnectServiceServerImpl) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) Strings(com.google.common.base.Strings) FastMap(rpc.turbo.util.FastMap) List(java.util.List) Stream(java.util.stream.Stream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Modifier(java.lang.reflect.Modifier) Map(java.util.Map) Log(org.apache.commons.logging.Log) LogFactory(org.apache.commons.logging.LogFactory) Method(java.lang.reflect.Method) TurboConnectService(rpc.turbo.common.TurboConnectService) TurboService(rpc.turbo.annotation.TurboService) CompletableFuture(java.util.concurrent.CompletableFuture) Method(java.lang.reflect.Method)

Example 4 with TurboService

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;
}
Also used : TurboService(rpc.turbo.annotation.TurboService)

Example 5 with TurboService

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;
        }
    }
}
Also used : Field(java.lang.reflect.Field) TurboService(rpc.turbo.annotation.TurboService)

Aggregations

TurboService (rpc.turbo.annotation.TurboService)9 Field (java.lang.reflect.Field)2 Collectors (java.util.stream.Collectors)2 Log (org.apache.commons.logging.Log)2 LogFactory (org.apache.commons.logging.LogFactory)2 TurboFailover (rpc.turbo.annotation.TurboFailover)2 LOWER_CAMEL (com.google.common.base.CaseFormat.LOWER_CAMEL)1 UPPER_CAMEL (com.google.common.base.CaseFormat.UPPER_CAMEL)1 Strings (com.google.common.base.Strings)1 Streams (com.google.common.collect.Streams)1 Method (java.lang.reflect.Method)1 Modifier (java.lang.reflect.Modifier)1 Duration (java.time.Duration)1 LocalDateTime (java.time.LocalDateTime)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1