Search in sources :

Example 56 with RpcException

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

the class XmlRpcProxyFactoryBean method afterPropertiesSet.

/**
 * {@inheritDoc}
 */
@Override
@SuppressWarnings("unchecked")
public void afterPropertiesSet() {
    super.afterPropertiesSet();
    // create proxy
    proxyObject = ProxyFactory.getProxy(getServiceInterface(), this);
    // create XmlRpcHttpClient
    try {
        xmlRpcClient = new XmlRpcClient();
        XmlRpcClientConfigImpl xmlRpcClientConfig = new XmlRpcClientConfigImpl();
        xmlRpcClientConfig.setServerURL(new URL(getServiceUrl()));
        xmlRpcClient.setConfig(xmlRpcClientConfig);
    } catch (MalformedURLException mue) {
        throw new RpcException(mue);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) XmlRpcClient(org.apache.xmlrpc.client.XmlRpcClient) RpcException(org.apache.dubbo.rpc.RpcException) XmlRpcClientConfigImpl(org.apache.xmlrpc.client.XmlRpcClientConfigImpl) URL(java.net.URL)

Example 57 with RpcException

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

the class ValidationFilterTest method testItWhileThrowoutRpcException.

@Test
public void testItWhileThrowoutRpcException() throws Exception {
    Assertions.assertThrows(RpcException.class, () -> {
        URL url = URL.valueOf("test://test:11/test?validation=true");
        given(validation.getValidator(url)).willThrow(new RpcException("rpc exception"));
        given(invoker.invoke(invocation)).willReturn(new AppResponse("success"));
        given(invoker.getUrl()).willReturn(url);
        given(invocation.getMethodName()).willReturn("echo1");
        given(invocation.getParameterTypes()).willReturn(new Class<?>[] { String.class });
        given(invocation.getArguments()).willReturn(new Object[] { "arg1" });
        validationFilter.setValidation(validation);
        validationFilter.invoke(invoker, invocation);
    });
}
Also used : AppResponse(org.apache.dubbo.rpc.AppResponse) RpcException(org.apache.dubbo.rpc.RpcException) URL(org.apache.dubbo.common.URL) Test(org.junit.jupiter.api.Test)

Example 58 with RpcException

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

the class ActiveLimitFilterTest method testInvokeGreaterActives.

@Test
public void testInvokeGreaterActives() {
    AtomicInteger count = new AtomicInteger(0);
    URL url = URL.valueOf("test://test:11/test?accesslog=true&group=dubbo&version=1.1&actives=1&timeout=1");
    final Invoker<ActiveLimitFilterTest> invoker = new BlockMyInvoker<ActiveLimitFilterTest>(url, 100);
    final Invocation invocation = new MockInvocation();
    final CountDownLatch latch = new CountDownLatch(1);
    for (int i = 0; i < 100; i++) {
        Thread thread = new Thread(new Runnable() {

            public void run() {
                try {
                    latch.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                for (int i = 0; i < 100; i++) {
                    try {
                        activeLimitFilter.invoke(invoker, invocation);
                    } catch (RpcException expected) {
                        count.incrementAndGet();
                    }
                }
            }
        });
        thread.start();
    }
    latch.countDown();
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    assertNotSame(0, count.intValue());
}
Also used : Invocation(org.apache.dubbo.rpc.Invocation) MockInvocation(org.apache.dubbo.rpc.support.MockInvocation) CountDownLatch(java.util.concurrent.CountDownLatch) URL(org.apache.dubbo.common.URL) BlockMyInvoker(org.apache.dubbo.rpc.support.BlockMyInvoker) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RpcException(org.apache.dubbo.rpc.RpcException) MockInvocation(org.apache.dubbo.rpc.support.MockInvocation) Test(org.junit.jupiter.api.Test)

Example 59 with RpcException

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

the class ActiveLimitFilterTest method testInvokeNotTimeOut.

@Test
public void testInvokeNotTimeOut() throws Exception {
    int totalThread = 100;
    int maxActives = 10;
    long timeout = 1000;
    long blockTime = 0;
    AtomicInteger count = new AtomicInteger(0);
    final CountDownLatch latch = new CountDownLatch(1);
    final CountDownLatch latchBlocking = new CountDownLatch(totalThread);
    URL url = URL.valueOf("test://test:11/test?accesslog=true&group=dubbo&version=1.1&actives=" + maxActives + "&timeout=" + timeout);
    final Invoker<ActiveLimitFilterTest> invoker = new BlockMyInvoker<ActiveLimitFilterTest>(url, blockTime);
    final Invocation invocation = new MockInvocation();
    for (int i = 0; i < totalThread; i++) {
        Thread thread = new Thread(new Runnable() {

            public void run() {
                try {
                    try {
                        latch.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    try {
                        Result asyncResult = activeLimitFilter.invoke(invoker, invocation);
                        Result result = asyncResult.get();
                        activeLimitFilter.onResponse(result, invoker, invocation);
                    } catch (RpcException expected) {
                        count.incrementAndGet();
                        activeLimitFilter.onError(expected, invoker, invocation);
                    } catch (Exception e) {
                        fail();
                    }
                } finally {
                    latchBlocking.countDown();
                }
            }
        });
        thread.start();
    }
    latch.countDown();
    try {
        latchBlocking.await();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    assertEquals(0, count.intValue());
}
Also used : Invocation(org.apache.dubbo.rpc.Invocation) MockInvocation(org.apache.dubbo.rpc.support.MockInvocation) CountDownLatch(java.util.concurrent.CountDownLatch) URL(org.apache.dubbo.common.URL) RpcException(org.apache.dubbo.rpc.RpcException) Result(org.apache.dubbo.rpc.Result) BlockMyInvoker(org.apache.dubbo.rpc.support.BlockMyInvoker) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RpcException(org.apache.dubbo.rpc.RpcException) MockInvocation(org.apache.dubbo.rpc.support.MockInvocation) Test(org.junit.jupiter.api.Test)

Example 60 with RpcException

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

the class ExceptionFilter method onResponse.

@Override
public void onResponse(Result appResponse, Invoker<?> invoker, Invocation invocation) {
    if (appResponse.hasException() && GenericService.class != invoker.getInterface()) {
        try {
            Throwable exception = appResponse.getException();
            // directly throw if it's checked exception
            if (!(exception instanceof RuntimeException) && (exception instanceof Exception)) {
                return;
            }
            // directly throw if the exception appears in the signature
            try {
                Method method = invoker.getInterface().getMethod(invocation.getMethodName(), invocation.getParameterTypes());
                Class<?>[] exceptionClasses = method.getExceptionTypes();
                for (Class<?> exceptionClass : exceptionClasses) {
                    if (exception.getClass().equals(exceptionClass)) {
                        return;
                    }
                }
            } catch (NoSuchMethodException e) {
                return;
            }
            // for the exception not found in method's signature, print ERROR message in server's log.
            logger.error("Got unchecked and undeclared exception which called by " + RpcContext.getContext().getRemoteHost() + ". service: " + invoker.getInterface().getName() + ", method: " + invocation.getMethodName() + ", exception: " + exception.getClass().getName() + ": " + exception.getMessage(), exception);
            // directly throw if exception class and interface class are in the same jar file.
            String serviceFile = ReflectUtils.getCodeBase(invoker.getInterface());
            String exceptionFile = ReflectUtils.getCodeBase(exception.getClass());
            if (serviceFile == null || exceptionFile == null || serviceFile.equals(exceptionFile)) {
                return;
            }
            // directly throw if it's JDK exception
            String className = exception.getClass().getName();
            if (className.startsWith("java.") || className.startsWith("javax.")) {
                return;
            }
            // directly throw if it's dubbo exception
            if (exception instanceof RpcException) {
                return;
            }
            // otherwise, wrap with RuntimeException and throw back to the client
            appResponse.setException(new RuntimeException(StringUtils.toString(exception)));
        } catch (Throwable e) {
            logger.warn("Fail to ExceptionFilter when called by " + RpcContext.getContext().getRemoteHost() + ". service: " + invoker.getInterface().getName() + ", method: " + invocation.getMethodName() + ", exception: " + e.getClass().getName() + ": " + e.getMessage(), e);
        }
    }
}
Also used : GenericService(org.apache.dubbo.rpc.service.GenericService) RpcException(org.apache.dubbo.rpc.RpcException) Method(java.lang.reflect.Method) RpcException(org.apache.dubbo.rpc.RpcException)

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