Search in sources :

Example 26 with RpcException

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

the class RpcExceptionMapperTest method testNormalException.

@Test
public void testNormalException() {
    RpcException rpcException = new RpcException();
    Response response = exceptionMapper.toResponse(rpcException);
    assertThat(response, not(nullValue()));
    assertThat(response.getEntity(), instanceOf(String.class));
}
Also used : Response(javax.ws.rs.core.Response) RpcException(org.apache.dubbo.rpc.RpcException) Test(org.junit.jupiter.api.Test)

Example 27 with RpcException

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

the class RpcExceptionMapperTest method testConstraintViolationException.

@Test
public void testConstraintViolationException() {
    ConstraintViolationException violationException = mock(ConstraintViolationException.class);
    ConstraintViolation violation = mock(ConstraintViolation.class, Answers.RETURNS_DEEP_STUBS);
    given(violationException.getConstraintViolations()).willReturn(Sets.<ConstraintViolation<?>>newSet(violation));
    RpcException rpcException = new RpcException("violation", violationException);
    Response response = exceptionMapper.toResponse(rpcException);
    assertThat(response, not(nullValue()));
    assertThat(response.getEntity(), instanceOf(ViolationReport.class));
}
Also used : Response(javax.ws.rs.core.Response) ConstraintViolation(javax.validation.ConstraintViolation) RpcException(org.apache.dubbo.rpc.RpcException) ConstraintViolationException(javax.validation.ConstraintViolationException) Test(org.junit.jupiter.api.Test)

Example 28 with RpcException

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

the class RmiProtocol method doRefer.

@Override
@SuppressWarnings("unchecked")
protected <T> T doRefer(final Class<T> serviceType, final URL url) throws RpcException {
    final RmiProxyFactoryBean rmiProxyFactoryBean = new RmiProxyFactoryBean();
    final String generic = url.getParameter(GENERIC_KEY);
    final boolean isGeneric = ProtocolUtils.isGeneric(generic) || serviceType.equals(GenericService.class);
    /*
          RMI needs extra parameter since it uses customized remote invocation object

          The customized RemoteInvocation was firstly introduced in v2.6.3; The package was renamed to 'org.apache.*' since v2.7.0
          Considering the above two conditions, we need to check before sending customized RemoteInvocation:
          1. if the provider version is v2.7.0 or higher, send 'org.apache.dubbo.rpc.protocol.rmi.RmiRemoteInvocation'.
          2. if the provider version is v2.6.3 or higher, send 'com.alibaba.dubbo.rpc.protocol.rmi.RmiRemoteInvocation'.
          3. if the provider version is lower than v2.6.3, does not use customized RemoteInvocation.
         */
    if (isRelease270OrHigher(url.getParameter(RELEASE_KEY))) {
        rmiProxyFactoryBean.setRemoteInvocationFactory(methodInvocation -> {
            RemoteInvocation invocation = new RmiRemoteInvocation(methodInvocation);
            if (isGeneric) {
                invocation.addAttribute(GENERIC_KEY, generic);
            }
            return invocation;
        });
    } else if (isRelease263OrHigher(url.getParameter(DUBBO_VERSION_KEY))) {
        rmiProxyFactoryBean.setRemoteInvocationFactory(methodInvocation -> {
            RemoteInvocation invocation = new com.alibaba.dubbo.rpc.protocol.rmi.RmiRemoteInvocation(methodInvocation);
            if (isGeneric) {
                invocation.addAttribute(GENERIC_KEY, generic);
            }
            return invocation;
        });
    }
    String serviceUrl = url.toIdentityString();
    if (isGeneric) {
        serviceUrl = serviceUrl + "/" + GENERIC_KEY;
    }
    rmiProxyFactoryBean.setServiceUrl(serviceUrl);
    rmiProxyFactoryBean.setServiceInterface(serviceType);
    rmiProxyFactoryBean.setCacheStub(true);
    rmiProxyFactoryBean.setLookupStubOnStartup(true);
    rmiProxyFactoryBean.setRefreshStubOnConnectFailure(true);
    rmiProxyFactoryBean.afterPropertiesSet();
    return (T) rmiProxyFactoryBean.getObject();
}
Also used : RemoteInvocation(org.springframework.remoting.support.RemoteInvocation) Version.isRelease263OrHigher(org.apache.dubbo.common.Version.isRelease263OrHigher) Version.isRelease270OrHigher(org.apache.dubbo.common.Version.isRelease270OrHigher) AbstractProxyProtocol(org.apache.dubbo.rpc.protocol.AbstractProxyProtocol) RemoteInvocation(org.springframework.remoting.support.RemoteInvocation) RmiProxyFactoryBean(org.springframework.remoting.rmi.RmiProxyFactoryBean) IOException(java.io.IOException) RpcException(org.apache.dubbo.rpc.RpcException) RemoteAccessException(org.springframework.remoting.RemoteAccessException) RemoteException(java.rmi.RemoteException) ProtocolUtils(org.apache.dubbo.rpc.support.ProtocolUtils) DUBBO_VERSION_KEY(org.apache.dubbo.common.constants.CommonConstants.DUBBO_VERSION_KEY) URL(org.apache.dubbo.common.URL) RmiServiceExporter(org.springframework.remoting.rmi.RmiServiceExporter) RELEASE_KEY(org.apache.dubbo.common.constants.CommonConstants.RELEASE_KEY) GENERIC_KEY(org.apache.dubbo.rpc.Constants.GENERIC_KEY) GenericService(org.apache.dubbo.rpc.service.GenericService) SocketTimeoutException(java.net.SocketTimeoutException) GenericService(org.apache.dubbo.rpc.service.GenericService) RmiProxyFactoryBean(org.springframework.remoting.rmi.RmiProxyFactoryBean)

Example 29 with RpcException

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

the class HessianProtocolTest method testTimeOut.

@Test
public void testTimeOut() {
    HessianServiceImpl server = new HessianServiceImpl();
    ProxyFactory proxyFactory = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension();
    Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension();
    int port = NetUtils.getAvailablePort();
    URL url = URL.valueOf("hessian://127.0.0.1:" + port + "/" + HessianService.class.getName() + "?version=1.0.0&timeout=10");
    Exporter<HessianService> exporter = protocol.export(proxyFactory.getInvoker(server, HessianService.class, url));
    Invoker<HessianService> invoker = protocol.refer(HessianService.class, url);
    HessianService client = proxyFactory.getProxy(invoker);
    try {
        client.timeOut(6000);
        fail();
    } catch (RpcException expected) {
        Assertions.assertTrue(expected.isTimeout());
    } finally {
        invoker.destroy();
        exporter.unexport();
    }
}
Also used : ProxyFactory(org.apache.dubbo.rpc.ProxyFactory) RpcException(org.apache.dubbo.rpc.RpcException) Protocol(org.apache.dubbo.rpc.Protocol) URL(org.apache.dubbo.common.URL) Test(org.junit.jupiter.api.Test)

Example 30 with RpcException

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

the class GrpcInvoker method doInvoke.

@Override
protected Result doInvoke(Invocation invocation) throws Throwable {
    try {
        Result result = target.invoke(invocation);
        // FIXME result is an AsyncRpcResult instance.
        Throwable e = result.getException();
        if (e != null) {
            throw getRpcException(getInterface(), getUrl(), invocation, e);
        }
        return result;
    } catch (RpcException e) {
        if (e.getCode() == RpcException.UNKNOWN_EXCEPTION) {
            e.setCode(getErrorCode(e.getCause()));
        }
        throw e;
    } catch (Throwable e) {
        throw getRpcException(getInterface(), getUrl(), invocation, e);
    }
}
Also used : RpcException(org.apache.dubbo.rpc.RpcException) Result(org.apache.dubbo.rpc.Result)

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