Search in sources :

Example 6 with AsyncMethodInfo

use of org.apache.dubbo.rpc.model.AsyncMethodInfo in project dubbo by alibaba.

the class ReferenceConfig method init.

public synchronized void init() {
    if (initialized) {
        return;
    }
    if (bootstrap == null) {
        bootstrap = DubboBootstrap.getInstance();
        // compatible with api call.
        if (null != this.getRegistries()) {
            bootstrap.registries(this.getRegistries());
        }
        bootstrap.initialize();
    }
    checkAndUpdateSubConfigs();
    checkStubAndLocal(interfaceClass);
    ConfigValidationUtils.checkMock(interfaceClass, this);
    Map<String, String> map = new HashMap<String, String>();
    map.put(SIDE_KEY, CONSUMER_SIDE);
    ReferenceConfigBase.appendRuntimeParameters(map);
    if (!ProtocolUtils.isGeneric(generic)) {
        String revision = Version.getVersion(interfaceClass, version);
        if (revision != null && revision.length() > 0) {
            map.put(REVISION_KEY, revision);
        }
        String[] methods = Wrapper.getWrapper(interfaceClass).getMethodNames();
        if (methods.length == 0) {
            logger.warn("No method found in service interface " + interfaceClass.getName());
            map.put(METHODS_KEY, ANY_VALUE);
        } else {
            map.put(METHODS_KEY, StringUtils.join(new HashSet<String>(Arrays.asList(methods)), COMMA_SEPARATOR));
        }
    }
    map.put(INTERFACE_KEY, interfaceName);
    AbstractConfig.appendParameters(map, getMetrics());
    AbstractConfig.appendParameters(map, getApplication());
    AbstractConfig.appendParameters(map, getModule());
    // remove 'default.' prefix for configs from ConsumerConfig
    // appendParameters(map, consumer, Constants.DEFAULT_KEY);
    AbstractConfig.appendParameters(map, consumer);
    AbstractConfig.appendParameters(map, this);
    MetadataReportConfig metadataReportConfig = getMetadataReportConfig();
    if (metadataReportConfig != null && metadataReportConfig.isValid()) {
        map.putIfAbsent(METADATA_KEY, REMOTE_METADATA_STORAGE_TYPE);
    }
    Map<String, AsyncMethodInfo> attributes = null;
    if (CollectionUtils.isNotEmpty(getMethods())) {
        attributes = new HashMap<>();
        for (MethodConfig methodConfig : getMethods()) {
            AbstractConfig.appendParameters(map, methodConfig, methodConfig.getName());
            String retryKey = methodConfig.getName() + ".retry";
            if (map.containsKey(retryKey)) {
                String retryValue = map.remove(retryKey);
                if ("false".equals(retryValue)) {
                    map.put(methodConfig.getName() + ".retries", "0");
                }
            }
            AsyncMethodInfo asyncMethodInfo = AbstractConfig.convertMethodConfig2AsyncInfo(methodConfig);
            if (asyncMethodInfo != null) {
                // consumerModel.getMethodModel(methodConfig.getName()).addAttribute(ASYNC_KEY, asyncMethodInfo);
                attributes.put(methodConfig.getName(), asyncMethodInfo);
            }
        }
    }
    String hostToRegistry = ConfigUtils.getSystemProperty(DUBBO_IP_TO_REGISTRY);
    if (StringUtils.isEmpty(hostToRegistry)) {
        hostToRegistry = NetUtils.getLocalHost();
    } else if (isInvalidLocalHost(hostToRegistry)) {
        throw new IllegalArgumentException("Specified invalid registry ip from property:" + DUBBO_IP_TO_REGISTRY + ", value:" + hostToRegistry);
    }
    map.put(REGISTER_IP_KEY, hostToRegistry);
    serviceMetadata.getAttachments().putAll(map);
    ref = createProxy(map);
    serviceMetadata.setTarget(ref);
    serviceMetadata.addAttribute(PROXY_CLASS_REF, ref);
    ConsumerModel consumerModel = repository.lookupReferredService(serviceMetadata.getServiceKey());
    consumerModel.setProxyObject(ref);
    consumerModel.init(attributes);
    initialized = true;
    checkInvokerAvailable();
    // dispatch a ReferenceConfigInitializedEvent since 2.7.4
    dispatch(new ReferenceConfigInitializedEvent(this, invoker));
}
Also used : HashMap(java.util.HashMap) AsyncMethodInfo(org.apache.dubbo.rpc.model.AsyncMethodInfo) ReferenceConfigInitializedEvent(org.apache.dubbo.config.event.ReferenceConfigInitializedEvent) ConsumerModel(org.apache.dubbo.rpc.model.ConsumerModel) HashSet(java.util.HashSet)

Example 7 with AsyncMethodInfo

use of org.apache.dubbo.rpc.model.AsyncMethodInfo in project dubbo by alibaba.

the class FutureFilter method fireReturnCallback.

private void fireReturnCallback(final Invoker<?> invoker, final Invocation invocation, final Object result) {
    final AsyncMethodInfo asyncMethodInfo = getAsyncMethodInfo(invoker, invocation);
    if (asyncMethodInfo == null) {
        return;
    }
    final Method onReturnMethod = asyncMethodInfo.getOnreturnMethod();
    final Object onReturnInst = asyncMethodInfo.getOnreturnInstance();
    // not set onreturn callback
    if (onReturnMethod == null && onReturnInst == null) {
        return;
    }
    if (onReturnMethod == null || onReturnInst == null) {
        throw new IllegalStateException("service:" + invoker.getUrl().getServiceKey() + " has a onreturn callback config , but no such " + (onReturnMethod == null ? "method" : "instance") + " found. url:" + invoker.getUrl());
    }
    ReflectUtils.makeAccessible(onReturnMethod);
    Object[] args = invocation.getArguments();
    Object[] params;
    Class<?>[] rParaTypes = onReturnMethod.getParameterTypes();
    if (rParaTypes.length > 1) {
        if (rParaTypes.length == 2 && rParaTypes[1].isAssignableFrom(Object[].class)) {
            params = new Object[2];
            params[0] = result;
            params[1] = args;
        } else {
            params = new Object[args.length + 1];
            params[0] = result;
            System.arraycopy(args, 0, params, 1, args.length);
        }
    } else {
        params = new Object[] { result };
    }
    try {
        onReturnMethod.invoke(onReturnInst, params);
    } catch (InvocationTargetException e) {
        fireThrowCallback(invoker, invocation, e.getTargetException());
    } catch (Throwable e) {
        fireThrowCallback(invoker, invocation, e);
    }
}
Also used : AsyncMethodInfo(org.apache.dubbo.rpc.model.AsyncMethodInfo) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

AsyncMethodInfo (org.apache.dubbo.rpc.model.AsyncMethodInfo)7 Method (java.lang.reflect.Method)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 ConsumerModel (org.apache.dubbo.rpc.model.ConsumerModel)2 MethodConfig (com.alibaba.dubbo.config.MethodConfig)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 ReferenceConfigInitializedEvent (org.apache.dubbo.config.event.ReferenceConfigInitializedEvent)1 Person (org.apache.dubbo.service.Person)1 Test (org.junit.jupiter.api.Test)1