Search in sources :

Example 1 with AsyncMethodInfo

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

the class FutureFilter method getAsyncMethodInfo.

private AsyncMethodInfo getAsyncMethodInfo(Invoker<?> invoker, Invocation invocation) {
    AsyncMethodInfo asyncMethodInfo = (AsyncMethodInfo) invocation.get(ASYNC_METHOD_INFO);
    if (asyncMethodInfo != null) {
        return asyncMethodInfo;
    }
    ConsumerModel consumerModel = ApplicationModel.getConsumerModel(invoker.getUrl().getServiceKey());
    if (consumerModel == null) {
        return null;
    }
    String methodName = invocation.getMethodName();
    if (methodName.equals($INVOKE)) {
        methodName = (String) invocation.getArguments()[0];
    }
    return consumerModel.getAsyncInfo(methodName);
}
Also used : ConsumerModel(org.apache.dubbo.rpc.model.ConsumerModel) AsyncMethodInfo(org.apache.dubbo.rpc.model.AsyncMethodInfo)

Example 2 with AsyncMethodInfo

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

the class FutureFilter method fireInvokeCallback.

private void fireInvokeCallback(final Invoker<?> invoker, final Invocation invocation) {
    final AsyncMethodInfo asyncMethodInfo = getAsyncMethodInfo(invoker, invocation);
    if (asyncMethodInfo == null) {
        return;
    }
    final Method onInvokeMethod = asyncMethodInfo.getOninvokeMethod();
    final Object onInvokeInst = asyncMethodInfo.getOninvokeInstance();
    if (onInvokeMethod == null && onInvokeInst == null) {
        return;
    }
    if (onInvokeMethod == null || onInvokeInst == null) {
        throw new IllegalStateException("service:" + invoker.getUrl().getServiceKey() + " has a oninvoke callback config , but no such " + (onInvokeMethod == null ? "method" : "instance") + " found. url:" + invoker.getUrl());
    }
    ReflectUtils.makeAccessible(onInvokeMethod);
    Object[] params = invocation.getArguments();
    try {
        onInvokeMethod.invoke(onInvokeInst, 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)

Example 3 with AsyncMethodInfo

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

the class FutureFilter method fireThrowCallback.

private void fireThrowCallback(final Invoker<?> invoker, final Invocation invocation, final Throwable exception) {
    final AsyncMethodInfo asyncMethodInfo = getAsyncMethodInfo(invoker, invocation);
    if (asyncMethodInfo == null) {
        return;
    }
    final Method onthrowMethod = asyncMethodInfo.getOnthrowMethod();
    final Object onthrowInst = asyncMethodInfo.getOnthrowInstance();
    // onthrow callback not configured
    if (onthrowMethod == null && onthrowInst == null) {
        return;
    }
    if (onthrowMethod == null || onthrowInst == null) {
        throw new IllegalStateException("service:" + invoker.getUrl().getServiceKey() + " has a onthrow callback config , but no such " + (onthrowMethod == null ? "method" : "instance") + " found. url:" + invoker.getUrl());
    }
    ReflectUtils.makeAccessible(onthrowMethod);
    Class<?>[] rParaTypes = onthrowMethod.getParameterTypes();
    if (rParaTypes[0].isAssignableFrom(exception.getClass())) {
        try {
            Object[] args = invocation.getArguments();
            Object[] params;
            if (rParaTypes.length > 1) {
                if (rParaTypes.length == 2 && rParaTypes[1].isAssignableFrom(Object[].class)) {
                    params = new Object[2];
                    params[0] = exception;
                    params[1] = args;
                } else {
                    params = new Object[args.length + 1];
                    params[0] = exception;
                    System.arraycopy(args, 0, params, 1, args.length);
                }
            } else {
                params = new Object[] { exception };
            }
            onthrowMethod.invoke(onthrowInst, params);
        } catch (Throwable e) {
            logger.error(invocation.getMethodName() + ".call back method invoke error . callback method :" + onthrowMethod + ", url:" + invoker.getUrl(), e);
        }
    } else {
        logger.error(invocation.getMethodName() + ".call back method invoke error . callback method :" + onthrowMethod + ", url:" + invoker.getUrl(), exception);
    }
}
Also used : AsyncMethodInfo(org.apache.dubbo.rpc.model.AsyncMethodInfo) Method(java.lang.reflect.Method)

Example 4 with AsyncMethodInfo

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

the class MethodConfigTest method testConvertMethodConfig2AsyncInfo.

@Test
public void testConvertMethodConfig2AsyncInfo() throws Exception {
    org.apache.dubbo.config.MethodConfig methodConfig = new org.apache.dubbo.config.MethodConfig();
    methodConfig.setOninvokeMethod("setName");
    methodConfig.setOninvoke(new Person());
    AsyncMethodInfo methodInfo = org.apache.dubbo.config.MethodConfig.convertMethodConfig2AsyncInfo(methodConfig);
    assertEquals(methodInfo.getOninvokeMethod(), Person.class.getMethod("setName", String.class));
}
Also used : MethodConfig(com.alibaba.dubbo.config.MethodConfig) AsyncMethodInfo(org.apache.dubbo.rpc.model.AsyncMethodInfo) Person(org.apache.dubbo.service.Person) Test(org.junit.jupiter.api.Test)

Example 5 with AsyncMethodInfo

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

the class AbstractConfig method convertMethodConfig2AsyncInfo.

protected static AsyncMethodInfo convertMethodConfig2AsyncInfo(MethodConfig methodConfig) {
    if (methodConfig == null || (methodConfig.getOninvoke() == null && methodConfig.getOnreturn() == null && methodConfig.getOnthrow() == null)) {
        return null;
    }
    // check config conflict
    if (Boolean.FALSE.equals(methodConfig.isReturn()) && (methodConfig.getOnreturn() != null || methodConfig.getOnthrow() != null)) {
        throw new IllegalStateException("method config error : return attribute must be set true when onreturn or onthrow has been set.");
    }
    AsyncMethodInfo asyncMethodInfo = new AsyncMethodInfo();
    asyncMethodInfo.setOninvokeInstance(methodConfig.getOninvoke());
    asyncMethodInfo.setOnreturnInstance(methodConfig.getOnreturn());
    asyncMethodInfo.setOnthrowInstance(methodConfig.getOnthrow());
    try {
        String oninvokeMethod = methodConfig.getOninvokeMethod();
        if (StringUtils.isNotEmpty(oninvokeMethod)) {
            asyncMethodInfo.setOninvokeMethod(getMethodByName(methodConfig.getOninvoke().getClass(), oninvokeMethod));
        }
        String onreturnMethod = methodConfig.getOnreturnMethod();
        if (StringUtils.isNotEmpty(onreturnMethod)) {
            asyncMethodInfo.setOnreturnMethod(getMethodByName(methodConfig.getOnreturn().getClass(), onreturnMethod));
        }
        String onthrowMethod = methodConfig.getOnthrowMethod();
        if (StringUtils.isNotEmpty(onthrowMethod)) {
            asyncMethodInfo.setOnthrowMethod(getMethodByName(methodConfig.getOnthrow().getClass(), onthrowMethod));
        }
    } catch (Exception e) {
        throw new IllegalStateException(e.getMessage(), e);
    }
    return asyncMethodInfo;
}
Also used : AsyncMethodInfo(org.apache.dubbo.rpc.model.AsyncMethodInfo)

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