Search in sources :

Example 6 with TurboService

use of rpc.turbo.annotation.TurboService in project turbo-rpc by hank-whu.

the class TurboClientStarter method extractTurboServiceClassList.

private Collection<Class<?>> extractTurboServiceClassList(ConfigurableListableBeanFactory beanFactory) {
    LocalDateTime startTime = LocalDateTime.now();
    Set<Class<?>> turboServiceSet = new HashSet<>();
    String[] beanNames = beanFactory.getBeanDefinitionNames();
    for (int i = 0; i < beanNames.length; i++) {
        String beanName = beanNames[i];
        BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName);
        String beanClassName = beanDefinition.getBeanClassName();
        extractTurboServiceClass(turboServiceSet, beanClassName);
    }
    if (logger.isInfoEnabled()) {
        LocalDateTime finishTime = LocalDateTime.now();
        Duration duration = Duration.between(startTime, finishTime);
        String turboServiceString = // 
        turboServiceSet.stream().map(// 
        clazz -> clazz.getName()).collect(Collectors.joining(",", "[", "]"));
        logger.info("扫描到TurboService: " + turboServiceString);
        logger.info("扫描TurboService耗时: " + duration);
    }
    return turboServiceSet;
}
Also used : LocalDateTime(java.time.LocalDateTime) Ordered(org.springframework.core.Ordered) TurboService(rpc.turbo.annotation.TurboService) Collection(java.util.Collection) BeanFactoryPostProcessor(org.springframework.beans.factory.config.BeanFactoryPostProcessor) LocalDateTime(java.time.LocalDateTime) Set(java.util.Set) BeansException(org.springframework.beans.BeansException) UPPER_CAMEL(com.google.common.base.CaseFormat.UPPER_CAMEL) Field(java.lang.reflect.Field) Collectors(java.util.stream.Collectors) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) LOWER_CAMEL(com.google.common.base.CaseFormat.LOWER_CAMEL) HashSet(java.util.HashSet) BeanPostProcessor(org.springframework.beans.factory.config.BeanPostProcessor) PreDestroy(javax.annotation.PreDestroy) TurboFailover(rpc.turbo.annotation.TurboFailover) TurboClient(rpc.turbo.client.TurboClient) ReflectUtils(rpc.turbo.util.ReflectUtils) Duration(java.time.Duration) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) ClientConfig(rpc.turbo.config.client.ClientConfig) Log(org.apache.commons.logging.Log) LogFactory(org.apache.commons.logging.LogFactory) Duration(java.time.Duration) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) HashSet(java.util.HashSet)

Example 7 with TurboService

use of rpc.turbo.annotation.TurboService in project turbo-rpc by hank-whu.

the class TurboClientStarter method extractTurboServiceClass.

private void extractTurboServiceClass(Set<Class<?>> turboServiceSet, String beanClassName) {
    if (beanClassName == null || beanClassName.startsWith("org.springframework.")) {
        return;
    }
    Class<?> beanClass;
    try {
        beanClass = Class.forName(beanClassName, false, beanFactory.getBeanClassLoader());
    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    }
    Collection<Class<?>> allDependClass = ReflectUtils.getAllDependClass(beanClass, clazz -> {
        if (!clazz.isInterface()) {
            // 只支持接口
            return false;
        }
        TurboService turboService = clazz.getAnnotation(TurboService.class);
        if (turboService == null) {
            return false;
        }
        return true;
    });
    turboServiceSet.addAll(allDependClass);
}
Also used : TurboService(rpc.turbo.annotation.TurboService)

Example 8 with TurboService

use of rpc.turbo.annotation.TurboService in project turbo-rpc by hank-whu.

the class TurboServerStarter method getTurboService.

@SuppressWarnings("rawtypes")
private Tuple2<TurboService, Class> getTurboService(Object bean) {
    if (bean == null) {
        return null;
    }
    Class<?> beanClass = bean.getClass();
    if (beanClass.getAnnotation(TurboFailover.class) != null) {
        return null;
    }
    Class<?>[] interfaces = beanClass.getInterfaces();
    if (interfaces == null || interfaces.length == 0) {
        return null;
    }
    for (int i = 0; i < interfaces.length; i++) {
        Class<?> interfaceClass = interfaces[i];
        TurboService turboService = interfaceClass.getAnnotation(TurboService.class);
        if (turboService != null) {
            return tuple(turboService, interfaceClass);
        }
    }
    return null;
}
Also used : TurboFailover(rpc.turbo.annotation.TurboFailover) TurboService(rpc.turbo.annotation.TurboService) ConditionalOnClass(org.springframework.boot.autoconfigure.condition.ConditionalOnClass)

Example 9 with TurboService

use of rpc.turbo.annotation.TurboService in project turbo-rpc by hank-whu.

the class RemoteServiceFactory method register.

/**
 * 注册一个远程代理
 *
 * @param app
 *            连接组
 *
 * @param clazz
 *            服务接口
 *
 * @param configs
 *            远程方法配置
 *
 * @throws Exception
 */
public synchronized void register(App app, Class<?> clazz, Collection<MethodConfig> configs) throws Exception {
    if (app == null) {
        throw new RuntimeException("app must not be null");
    }
    if (clazz == null) {
        throw new RuntimeException("clazz must not be null");
    }
    if (!app.isSupport(clazz)) {
        throw new RuntimeException("the remote service not support the service " + clazz.getName());
    }
    if (configs == null || configs.isEmpty()) {
        throw new RuntimeException("configs must not be empty");
    }
    if (getService(clazz) != null) {
        return;
    }
    TurboService classConfig = clazz.getAnnotation(TurboService.class);
    if (classConfig != null && classConfig.ignore()) {
        if (logger.isInfoEnabled()) {
            logger.info(clazz + " service ignore");
        }
        return;
    }
    if (logger.isInfoEnabled()) {
        logger.info("service " + clazz.getName() + " register");
    }
    Object service = generateRemoteObject(app, clazz, configs);
    remoteServiceMap.put(clazz, service);
    if (failoverInvokerFactory == null) {
        return;
    }
}
Also used : 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