use of com.alipay.sofa.rpc.core.exception.SofaTimeOutException in project sofa-rpc by sofastack.
the class AsyncCallbackTest method testNoProviderException.
@Test
public void testNoProviderException() {
// use bolt, so callback will throw connection closed exception
serverConfig = new ServerConfig().setPort(22222).setDaemon(false).setProtocol("rest");
serverConfig.buildIfAbsent().start();
// B调C的客户端
Filter filter = new TestAsyncFilter();
BConsumer = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setInvokeType(RpcConstants.INVOKER_TYPE_CALLBACK).setTimeout(1000).setFilterRef(Arrays.asList(filter)).setDirectUrl("bolt://127.0.0.1:22222");
HelloService helloService = BConsumer.refer();
final CountDownLatch latch = new CountDownLatch(1);
final String[] ret = { null };
final boolean[] hasExp = { false };
RpcInvokeContext.getContext().setResponseCallback(new SofaResponseCallback() {
@Override
public void onAppResponse(Object appResponse, String methodName, RequestBase request) {
LOGGER.info("B get result: {}", appResponse);
latch.countDown();
}
@Override
public void onAppException(Throwable throwable, String methodName, RequestBase request) {
LOGGER.info("B get app exception: {}", throwable);
latch.countDown();
}
@Override
public void onSofaException(SofaRpcException sofaException, String methodName, RequestBase request) {
LOGGER.info("B get sofa exception: {}", sofaException);
if ((sofaException instanceof SofaTimeOutException)) {
hasExp[0] = false;
} else {
hasExp[0] = true;
}
latch.countDown();
}
});
String ret0 = helloService.sayHello("xxx", 22);
// 第一次返回null
Assert.assertNull(ret0);
try {
latch.await(1500, TimeUnit.MILLISECONDS);
} catch (InterruptedException ignore) {
}
// 一定是一个超时异常
assertTrue(hasExp[0]);
RpcInvokeContext.removeContext();
}
use of com.alipay.sofa.rpc.core.exception.SofaTimeOutException in project sofa-rpc by sofastack.
the class AsyncCallbackTest method testTimeoutException.
@Test
public void testTimeoutException() {
serverConfig = new ServerConfig().setPort(22221).setDaemon(false);
// C服务的服务端
CProvider = new ProviderConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setRef(new HelloServiceImpl(500)).setServer(serverConfig);
CProvider.export();
// B调C的客户端
Filter filter = new TestAsyncFilter();
BConsumer = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setInvokeType(RpcConstants.INVOKER_TYPE_CALLBACK).setTimeout(1).setFilterRef(Arrays.asList(filter)).setDirectUrl("bolt://127.0.0.1:22221");
HelloService helloService = BConsumer.refer();
final CountDownLatch latch = new CountDownLatch(1);
final String[] ret = { null };
final boolean[] hasExp = { false };
RpcInvokeContext.getContext().setResponseCallback(new SofaResponseCallback() {
@Override
public void onAppResponse(Object appResponse, String methodName, RequestBase request) {
LOGGER.info("B get result: {}", appResponse);
latch.countDown();
}
@Override
public void onAppException(Throwable throwable, String methodName, RequestBase request) {
LOGGER.info("B get app exception: {}", throwable);
latch.countDown();
}
@Override
public void onSofaException(SofaRpcException sofaException, String methodName, RequestBase request) {
LOGGER.info("B get sofa exception: {}", sofaException);
if (sofaException instanceof SofaTimeOutException) {
hasExp[0] = true;
}
latch.countDown();
}
});
String ret0 = helloService.sayHello("xxx", 22);
// 第一次返回null
Assert.assertNull(ret0);
try {
latch.await(2000, TimeUnit.MILLISECONDS);
} catch (InterruptedException ignore) {
}
// 一定是一个超时异常
assertTrue(hasExp[0]);
RpcInvokeContext.removeContext();
}
use of com.alipay.sofa.rpc.core.exception.SofaTimeOutException in project sofa-rpc by sofastack.
the class BoltClientTransport method convertToRpcException.
/**
* 转换调用出现的异常为RPC异常
*
* @param e 异常
* @return RPC异常
*/
protected SofaRpcException convertToRpcException(Exception e) {
SofaRpcException exception;
if (e instanceof SofaRpcException) {
exception = (SofaRpcException) e;
} else // 超时
if (e instanceof InvokeTimeoutException) {
exception = new SofaTimeOutException(e);
} else // 服务器忙
if (e instanceof InvokeServerBusyException) {
exception = new SofaRpcException(RpcErrorType.SERVER_BUSY, e);
} else // 序列化
if (e instanceof SerializationException) {
boolean isServer = ((SerializationException) e).isServerSide();
exception = isServer ? new SofaRpcException(RpcErrorType.SERVER_SERIALIZE, e) : new SofaRpcException(RpcErrorType.CLIENT_SERIALIZE, e);
} else // 反序列化
if (e instanceof DeserializationException) {
boolean isServer = ((DeserializationException) e).isServerSide();
exception = isServer ? new SofaRpcException(RpcErrorType.SERVER_DESERIALIZE, e) : new SofaRpcException(RpcErrorType.CLIENT_DESERIALIZE, e);
} else // 长连接断连
if (e instanceof ConnectionClosedException) {
exception = new SofaRpcException(RpcErrorType.CLIENT_NETWORK, e);
} else // 客户端发送失败
if (e instanceof InvokeSendFailedException) {
exception = new SofaRpcException(RpcErrorType.CLIENT_NETWORK, e);
} else // 服务端未知异常
if (e instanceof InvokeServerException) {
exception = new SofaRpcException(RpcErrorType.SERVER_UNDECLARED_ERROR, e.getCause());
} else // 客户端未知
{
exception = new SofaRpcException(RpcErrorType.CLIENT_UNDECLARED_ERROR, e);
}
return exception;
}
use of com.alipay.sofa.rpc.core.exception.SofaTimeOutException in project sofa-rpc by sofastack.
the class BoltInvokerCallback method onException.
@Override
public void onException(Throwable e) {
if (callback == null) {
return;
}
ClassLoader cl = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(this.classLoader);
RpcInternalContext.setContext(context);
if (EventBus.isEnable(ClientAsyncReceiveEvent.class)) {
EventBus.post(new ClientAsyncReceiveEvent(consumerConfig, providerInfo, request, null, e));
}
// do async filter after respond server
FilterChain chain = consumerConfig.getConsumerBootstrap().getCluster().getFilterChain();
if (chain != null) {
chain.onAsyncResponse(consumerConfig, request, null, e);
}
recordClientElapseTime();
if (EventBus.isEnable(ClientEndInvokeEvent.class)) {
EventBus.post(new ClientEndInvokeEvent(request, null, e));
}
// judge is timeout or others
SofaRpcException sofaRpcException = null;
if (e instanceof InvokeTimeoutException) {
sofaRpcException = new SofaTimeOutException(e);
} else {
sofaRpcException = new SofaRpcException(RpcErrorType.SERVER_UNDECLARED_ERROR, e.getMessage(), e);
}
callback.onSofaException(sofaRpcException, request.getMethodName(), request);
} finally {
Thread.currentThread().setContextClassLoader(cl);
RpcInvokeContext.removeContext();
RpcInternalContext.removeAllContext();
}
}
Aggregations