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