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));
}
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));
}
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();
}
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();
}
}
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);
}
}
Aggregations