Search in sources :

Example 51 with RpcException

use of org.apache.dubbo.rpc.RpcException in project dubbo by alibaba.

the class GenericImplFilter method onResponse.

@Override
public void onResponse(Result appResponse, Invoker<?> invoker, Invocation invocation) {
    String generic = invoker.getUrl().getParameter(GENERIC_KEY);
    String methodName = invocation.getMethodName();
    Class<?>[] parameterTypes = invocation.getParameterTypes();
    Object genericImplMarker = invocation.get(GENERIC_IMPL_MARKER);
    if (genericImplMarker != null && (boolean) invocation.get(GENERIC_IMPL_MARKER)) {
        if (!appResponse.hasException()) {
            Object value = appResponse.getValue();
            try {
                Class<?> invokerInterface = invoker.getInterface();
                if (!$INVOKE.equals(methodName) && !$INVOKE_ASYNC.equals(methodName) && invokerInterface.isAssignableFrom(GenericService.class)) {
                    try {
                        // find the real interface from url
                        String realInterface = invoker.getUrl().getParameter(Constants.INTERFACE);
                        invokerInterface = ReflectUtils.forName(realInterface);
                    } catch (Throwable e) {
                    // ignore
                    }
                }
                Method method = invokerInterface.getMethod(methodName, parameterTypes);
                if (ProtocolUtils.isBeanGenericSerialization(generic)) {
                    if (value == null) {
                        appResponse.setValue(value);
                    } else if (value instanceof JavaBeanDescriptor) {
                        appResponse.setValue(JavaBeanSerializeUtil.deserialize((JavaBeanDescriptor) value));
                    } else {
                        throw new RpcException("The type of result value is " + value.getClass().getName() + " other than " + JavaBeanDescriptor.class.getName() + ", and the result is " + value);
                    }
                } else {
                    Type[] types = ReflectUtils.getReturnTypes(method);
                    appResponse.setValue(PojoUtils.realize(value, (Class<?>) types[0], types[1]));
                }
            } catch (NoSuchMethodException e) {
                throw new RpcException(e.getMessage(), e);
            }
        } else if (appResponse.getException() instanceof com.alibaba.dubbo.rpc.service.GenericException) {
            com.alibaba.dubbo.rpc.service.GenericException exception = (com.alibaba.dubbo.rpc.service.GenericException) appResponse.getException();
            try {
                String className = exception.getExceptionClass();
                Class<?> clazz = ReflectUtils.forName(className);
                Throwable targetException = null;
                Throwable lastException = null;
                try {
                    targetException = (Throwable) clazz.newInstance();
                } catch (Throwable e) {
                    lastException = e;
                    for (Constructor<?> constructor : clazz.getConstructors()) {
                        try {
                            targetException = (Throwable) constructor.newInstance(new Object[constructor.getParameterTypes().length]);
                            break;
                        } catch (Throwable e1) {
                            lastException = e1;
                        }
                    }
                }
                if (targetException != null) {
                    try {
                        Field field = Throwable.class.getDeclaredField("detailMessage");
                        ReflectUtils.makeAccessible(field);
                        field.set(targetException, exception.getExceptionMessage());
                    } catch (Throwable e) {
                        logger.warn(e.getMessage(), e);
                    }
                    appResponse.setException(targetException);
                } else if (lastException != null) {
                    throw lastException;
                }
            } catch (Throwable e) {
                throw new RpcException("Can not deserialize exception " + exception.getExceptionClass() + ", message: " + exception.getExceptionMessage(), e);
            }
        }
    }
}
Also used : GenericService(org.apache.dubbo.rpc.service.GenericService) Method(java.lang.reflect.Method) Field(java.lang.reflect.Field) Type(java.lang.reflect.Type) JavaBeanDescriptor(org.apache.dubbo.common.beanutil.JavaBeanDescriptor) RpcException(org.apache.dubbo.rpc.RpcException)

Example 52 with RpcException

use of org.apache.dubbo.rpc.RpcException in project dubbo by alibaba.

the class ActiveLimitFilter method invoke.

@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
    URL url = invoker.getUrl();
    String methodName = invocation.getMethodName();
    int max = invoker.getUrl().getMethodParameter(methodName, ACTIVES_KEY, 0);
    final RpcStatus rpcStatus = RpcStatus.getStatus(invoker.getUrl(), invocation.getMethodName());
    if (!RpcStatus.beginCount(url, methodName, max)) {
        long timeout = invoker.getUrl().getMethodParameter(invocation.getMethodName(), TIMEOUT_KEY, 0);
        long start = System.currentTimeMillis();
        long remain = timeout;
        synchronized (rpcStatus) {
            while (!RpcStatus.beginCount(url, methodName, max)) {
                try {
                    rpcStatus.wait(remain);
                } catch (InterruptedException e) {
                // ignore
                }
                long elapsed = System.currentTimeMillis() - start;
                remain = timeout - elapsed;
                if (remain <= 0) {
                    throw new RpcException(RpcException.LIMIT_EXCEEDED_EXCEPTION, "Waiting concurrent invoke timeout in client-side for service:  " + invoker.getInterface().getName() + ", method: " + invocation.getMethodName() + ", elapsed: " + elapsed + ", timeout: " + timeout + ". concurrent invokes: " + rpcStatus.getActive() + ". max concurrent invoke limit: " + max);
                }
            }
        }
    }
    invocation.put(ACTIVELIMIT_FILTER_START_TIME, System.currentTimeMillis());
    return invoker.invoke(invocation);
}
Also used : RpcException(org.apache.dubbo.rpc.RpcException) RpcStatus(org.apache.dubbo.rpc.RpcStatus) URL(org.apache.dubbo.common.URL)

Example 53 with RpcException

use of org.apache.dubbo.rpc.RpcException in project dubbo by alibaba.

the class ActiveLimitFilter method onError.

@Override
public void onError(Throwable t, Invoker<?> invoker, Invocation invocation) {
    String methodName = invocation.getMethodName();
    URL url = invoker.getUrl();
    int max = invoker.getUrl().getMethodParameter(methodName, ACTIVES_KEY, 0);
    if (t instanceof RpcException) {
        RpcException rpcException = (RpcException) t;
        if (rpcException.isLimitExceed()) {
            return;
        }
    }
    RpcStatus.endCount(url, methodName, getElapsed(invocation), false);
    notifyFinish(RpcStatus.getStatus(url, methodName), max);
}
Also used : RpcException(org.apache.dubbo.rpc.RpcException) URL(org.apache.dubbo.common.URL)

Example 54 with RpcException

use of org.apache.dubbo.rpc.RpcException in project dubbo by alibaba.

the class ThriftCodecTest method testDecodeExceptionResponse.

@Test
public void testDecodeExceptionResponse() throws Exception {
    int port = NetUtils.getAvailablePort();
    URL url = URL.valueOf(ThriftProtocol.NAME + "://127.0.0.1:" + port + "/" + Demo.class.getName());
    Channel channel = new MockedChannel(url);
    RandomAccessByteArrayOutputStream bos = new RandomAccessByteArrayOutputStream(128);
    Request request = createRequest();
    DefaultFuture future = DefaultFuture.newFuture(channel, request, 10, null);
    TMessage message = new TMessage("echoString", TMessageType.EXCEPTION, ThriftCodec.getSeqId());
    TTransport transport = new TIOStreamTransport(bos);
    TBinaryProtocol protocol = new TBinaryProtocol(transport);
    TApplicationException exception = new TApplicationException();
    int messageLength, headerLength;
    // prepare
    protocol.writeI16(ThriftCodec.MAGIC);
    protocol.writeI32(Integer.MAX_VALUE);
    protocol.writeI16(Short.MAX_VALUE);
    protocol.writeByte(ThriftCodec.VERSION);
    protocol.writeString(Demo.class.getName());
    // path
    protocol.writeString(Demo.class.getName());
    protocol.writeI64(request.getId());
    protocol.getTransport().flush();
    headerLength = bos.size();
    protocol.writeMessageBegin(message);
    exception.write(protocol);
    protocol.writeMessageEnd();
    protocol.getTransport().flush();
    int oldIndex = messageLength = bos.size();
    try {
        bos.setWriteIndex(ThriftCodec.MESSAGE_LENGTH_INDEX);
        protocol.writeI32(messageLength);
        bos.setWriteIndex(ThriftCodec.MESSAGE_HEADER_LENGTH_INDEX);
        protocol.writeI16((short) (0xffff & headerLength));
    } finally {
        bos.setWriteIndex(oldIndex);
    }
    // prepare
    ChannelBuffer bis = ChannelBuffers.wrappedBuffer(encodeFrame(bos.toByteArray()));
    Object obj = codec.decode((Channel) null, bis);
    Assertions.assertNotNull(obj);
    Assertions.assertTrue(obj instanceof Response);
    Response response = (Response) obj;
    Assertions.assertTrue(response.getResult() instanceof AppResponse);
    AppResponse result = (AppResponse) response.getResult();
    Assertions.assertTrue(result.hasException());
    Assertions.assertTrue(result.getException() instanceof RpcException);
}
Also used : Demo(org.apache.dubbo.rpc.gen.thrift.Demo) Channel(org.apache.dubbo.remoting.Channel) Request(org.apache.dubbo.remoting.exchange.Request) TIOStreamTransport(org.apache.thrift.transport.TIOStreamTransport) URL(org.apache.dubbo.common.URL) DefaultFuture(org.apache.dubbo.remoting.exchange.support.DefaultFuture) TApplicationException(org.apache.thrift.TApplicationException) ChannelBuffer(org.apache.dubbo.remoting.buffer.ChannelBuffer) AppResponse(org.apache.dubbo.rpc.AppResponse) Response(org.apache.dubbo.remoting.exchange.Response) RandomAccessByteArrayOutputStream(org.apache.dubbo.rpc.protocol.thrift.io.RandomAccessByteArrayOutputStream) TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) TMessage(org.apache.thrift.protocol.TMessage) AppResponse(org.apache.dubbo.rpc.AppResponse) RpcException(org.apache.dubbo.rpc.RpcException) TTransport(org.apache.thrift.transport.TTransport) Test(org.junit.jupiter.api.Test)

Example 55 with RpcException

use of org.apache.dubbo.rpc.RpcException in project dubbo by alibaba.

the class XmlRpcProtocol method doExport.

@Override
protected <T> Runnable doExport(T impl, Class<T> type, URL url) throws RpcException {
    final URL httpUrl = url.setProtocol("http");
    String addr = httpUrl.getIp() + ":" + httpUrl.getPort();
    ProtocolServer protocolServer = serverMap.get(addr);
    if (protocolServer == null) {
        RemotingServer remotingServer = httpBinder.bind(httpUrl, new InternalHandler(httpUrl.getParameter("cors", false)));
        serverMap.put(addr, new ProxyProtocolServer(remotingServer));
    }
    final String path = httpUrl.getAbsolutePath();
    XmlRpcServletServer xmlRpcServer = new XmlRpcServletServer();
    PropertyHandlerMapping propertyHandlerMapping = new PropertyHandlerMapping();
    try {
        propertyHandlerMapping.setRequestProcessorFactoryFactory(new RequestProcessorFactoryFactory() {

            @Override
            public RequestProcessorFactory getRequestProcessorFactory(Class pClass) throws XmlRpcException {
                return new RequestProcessorFactory() {

                    @Override
                    public Object getRequestProcessor(XmlRpcRequest pRequest) throws XmlRpcException {
                        return impl;
                    }
                };
            }
        });
        propertyHandlerMapping.addHandler(XmlRpcProxyFactoryBean.replace(type.getName()), type);
    } catch (Exception e) {
        throw new RpcException(e);
    }
    xmlRpcServer.setHandlerMapping(propertyHandlerMapping);
    XmlRpcServerConfigImpl xmlRpcServerConfig = (XmlRpcServerConfigImpl) xmlRpcServer.getConfig();
    xmlRpcServerConfig.setEnabledForExceptions(true);
    xmlRpcServerConfig.setContentLengthOptional(false);
    skeletonMap.put(path, xmlRpcServer);
    return new Runnable() {

        @Override
        public void run() {
            skeletonMap.remove(path);
        }
    };
}
Also used : RequestProcessorFactoryFactory(org.apache.xmlrpc.server.RequestProcessorFactoryFactory) RemotingServer(org.apache.dubbo.remoting.RemotingServer) URL(org.apache.dubbo.common.URL) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) RpcException(org.apache.dubbo.rpc.RpcException) RemoteAccessException(org.springframework.remoting.RemoteAccessException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) SocketTimeoutException(java.net.SocketTimeoutException) PropertyHandlerMapping(org.apache.xmlrpc.server.PropertyHandlerMapping) ProtocolServer(org.apache.dubbo.rpc.ProtocolServer) XmlRpcServerConfigImpl(org.apache.xmlrpc.server.XmlRpcServerConfigImpl) RpcException(org.apache.dubbo.rpc.RpcException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) XmlRpcRequest(org.apache.xmlrpc.XmlRpcRequest) XmlRpcServletServer(org.apache.xmlrpc.webserver.XmlRpcServletServer) XmlRpcException(org.apache.xmlrpc.XmlRpcException)

Aggregations

RpcException (org.apache.dubbo.rpc.RpcException)102 URL (org.apache.dubbo.common.URL)37 Test (org.junit.jupiter.api.Test)29 RpcInvocation (org.apache.dubbo.rpc.RpcInvocation)28 Result (org.apache.dubbo.rpc.Result)21 Invocation (org.apache.dubbo.rpc.Invocation)17 ArrayList (java.util.ArrayList)15 AppResponse (org.apache.dubbo.rpc.AppResponse)13 AsyncRpcResult (org.apache.dubbo.rpc.AsyncRpcResult)13 Invoker (org.apache.dubbo.rpc.Invoker)13 IOException (java.io.IOException)9 List (java.util.List)9 Method (java.lang.reflect.Method)8 Gson (com.google.gson.Gson)6 InvocationTargetException (java.lang.reflect.InvocationTargetException)6 HashMap (java.util.HashMap)5 RemotingException (org.apache.dubbo.remoting.RemotingException)5 TException (org.apache.thrift.TException)5 SocketTimeoutException (java.net.SocketTimeoutException)4 CountDownLatch (java.util.concurrent.CountDownLatch)4