use of com.alipay.sofa.rpc.core.response.SofaResponse in project sofa-rpc by sofastack.
the class TestInvoker method invoke.
@Override
public SofaResponse invoke(SofaRequest request) throws SofaRpcException {
this.request = request;
if ("throwRPC".equals(request.getMethodName())) {
throw new SofaRpcException(RpcErrorType.SERVER_UNDECLARED_ERROR, "xxxx");
}
SofaResponse response = new SofaResponse();
try {
Method method = TestInterface.class.getMethod(request.getMethodName(), ClassTypeUtils.getClasses(request.getMethodArgSigs()));
Object ret = method.invoke(testInterface, request.getMethodArgs());
response = new SofaResponse();
response.setAppResponse(ret);
} catch (Exception e) {
response.setErrorMsg(e.getMessage());
}
return response;
}
use of com.alipay.sofa.rpc.core.response.SofaResponse in project sofa-rpc by sofastack.
the class AbstractCluster method doSendMsg.
/**
* 调用客户端
*
* @param transport 客户端连接
* @param request Request对象
* @return 调用结果
* @throws SofaRpcException rpc异常
*/
protected SofaResponse doSendMsg(ProviderInfo providerInfo, ClientTransport transport, SofaRequest request) throws SofaRpcException {
RpcInternalContext context = RpcInternalContext.getContext();
// 添加调用的服务端远程地址
RpcInternalContext.getContext().setRemoteAddress(providerInfo.getHost(), providerInfo.getPort());
try {
// 根据服务端版本特殊处理
checkProviderVersion(providerInfo, request);
String invokeType = request.getInvokeType();
int timeout = resolveTimeout(request, consumerConfig, providerInfo);
SofaResponse response = null;
// 同步调用
if (RpcConstants.INVOKER_TYPE_SYNC.equals(invokeType)) {
long start = RpcRuntimeContext.now();
try {
response = transport.syncSend(request, timeout);
} finally {
if (RpcInternalContext.isAttachmentEnable()) {
long elapsed = RpcRuntimeContext.now() - start;
context.setAttachment(RpcConstants.INTERNAL_KEY_CLIENT_ELAPSE, elapsed);
}
}
} else // 单向调用
if (RpcConstants.INVOKER_TYPE_ONEWAY.equals(invokeType)) {
long start = RpcRuntimeContext.now();
try {
transport.oneWaySend(request, timeout);
response = buildEmptyResponse(request);
} finally {
if (RpcInternalContext.isAttachmentEnable()) {
long elapsed = RpcRuntimeContext.now() - start;
context.setAttachment(RpcConstants.INTERNAL_KEY_CLIENT_ELAPSE, elapsed);
}
}
} else // Callback调用
if (RpcConstants.INVOKER_TYPE_CALLBACK.equals(invokeType)) {
// 调用级别回调监听器
SofaResponseCallback sofaResponseCallback = request.getSofaResponseCallback();
if (sofaResponseCallback == null) {
SofaResponseCallback methodResponseCallback = consumerConfig.getMethodOnreturn(request.getMethodName());
if (methodResponseCallback != null) {
// 方法的Callback
request.setSofaResponseCallback(methodResponseCallback);
}
}
// 记录发送开始时间
context.setAttachment(RpcConstants.INTERNAL_KEY_CLIENT_SEND_TIME, RpcRuntimeContext.now());
// 开始调用
transport.asyncSend(request, timeout);
response = buildEmptyResponse(request);
} else // Future调用
if (RpcConstants.INVOKER_TYPE_FUTURE.equals(invokeType)) {
// 记录发送开始时间
context.setAttachment(RpcConstants.INTERNAL_KEY_CLIENT_SEND_TIME, RpcRuntimeContext.now());
// 开始调用
ResponseFuture future = transport.asyncSend(request, timeout);
// 放入线程上下文
RpcInternalContext.getContext().setFuture(future);
response = buildEmptyResponse(request);
} else {
throw new SofaRpcException(RpcErrorType.CLIENT_UNDECLARED_ERROR, "Unknown invoke type:" + invokeType);
}
return response;
} catch (SofaRpcException e) {
throw e;
} catch (Throwable e) {
// 客户端其它异常
throw new SofaRpcException(RpcErrorType.CLIENT_UNDECLARED_ERROR, e);
}
}
use of com.alipay.sofa.rpc.core.response.SofaResponse in project sofa-rpc by sofastack.
the class AbstractCluster method invoke.
@Override
public SofaResponse invoke(SofaRequest request) throws SofaRpcException {
SofaResponse response = null;
try {
// 为什么要放在这里,因为走了filter的话,就要求有地址了
if (consumerConfig.isMock()) {
return doMockInvoke(request);
}
// 做一些初始化检查,例如未连接可以连接
checkClusterState();
// 开始调用
// 计数+1
countOfInvoke.incrementAndGet();
response = doInvoke(request);
return response;
} catch (SofaRpcException e) {
// 客户端收到异常(客户端自己的异常)
throw e;
} finally {
// 计数-1
countOfInvoke.decrementAndGet();
}
}
use of com.alipay.sofa.rpc.core.response.SofaResponse in project sofa-rpc by sofastack.
the class SofaResponseHessianSerializer method decodeObjectByTemplate.
@Override
public void decodeObjectByTemplate(AbstractByteBuf data, Map<String, String> context, SofaResponse template) throws SofaRpcException {
try {
UnsafeByteArrayInputStream inputStream = new UnsafeByteArrayInputStream(data.array());
Hessian2Input input = new Hessian2Input(inputStream);
// 根据SerializeType信息决定序列化器
boolean genericSerialize = context != null && isGenericResponse(context.get(RemotingConstants.HEAD_GENERIC_TYPE));
if (genericSerialize) {
input.setSerializerFactory(genericSerializerFactory);
GenericObject genericObject = (GenericObject) input.readObject();
template.setErrorMsg((String) genericObject.getField("errorMsg"));
template.setAppResponse(genericObject.getField("appResponse"));
template.setResponseProps((Map<String, String>) genericObject.getField("responseProps"));
} else {
input.setSerializerFactory(serializerFactory);
SofaResponse tmp = (SofaResponse) input.readObject();
// copy values to template
template.setErrorMsg(tmp.getErrorMsg());
template.setAppResponse(tmp.getAppResponse());
template.setResponseProps(tmp.getResponseProps());
}
input.close();
} catch (IOException e) {
throw buildDeserializeError(e.getMessage(), e);
}
}
use of com.alipay.sofa.rpc.core.response.SofaResponse in project sofa-rpc by sofastack.
the class NotFoundInvokerTest method testAll.
@Test
public void testAll() {
ServerConfig serverConfig = new ServerConfig().setStopTimeout(0).setPort(22222).setQueues(100).setCoreThreads(10).setMaxThreads(10);
// 发布一个服务,每个请求要执行1秒
ProviderConfig<HelloService> providerConfig = new ProviderConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setRef(new HelloServiceImpl(1500)).setServer(serverConfig).setRegister(false);
providerConfig.export();
ConsumerConfig<HelloService> consumerConfig = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setDirectUrl("bolt://127.0.0.1:22222").setTimeout(30000).setFilterRef(Collections.<Filter>singletonList(new Filter() {
@Override
public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException {
// 造一个假方法
request.setMethodName(request.getMethodName() + "_unknown");
return invoker.invoke(request);
}
})).setRegister(false);
HelloService helloService = consumerConfig.refer();
boolean ok = true;
try {
// 找不到方法
helloService.sayHello("xxx", 22);
} catch (Exception e) {
if (e instanceof SofaRpcException) {
Assert.assertEquals(((SofaRpcException) e).getErrorType(), RpcErrorType.SERVER_UNDECLARED_ERROR);
} else if (e instanceof UndeclaredThrowableException) {
Assert.assertEquals(((SofaRpcException) e.getCause()).getErrorType(), RpcErrorType.SERVER_UNDECLARED_ERROR);
}
ok = false;
}
Assert.assertFalse(ok);
ConsumerConfig<EchoService> consumerConfig2 = new ConsumerConfig<EchoService>().setInterfaceId(EchoService.class.getName()).setDirectUrl("bolt://127.0.0.1:22222").setTimeout(30000).setRegister(false);
EchoService echoService = consumerConfig2.refer();
ok = true;
try {
echoService.echoStr("xx");
} catch (Exception e) {
if (e instanceof SofaRpcException) {
Assert.assertEquals(((SofaRpcException) e).getErrorType(), RpcErrorType.SERVER_UNDECLARED_ERROR);
} else if (e instanceof UndeclaredThrowableException) {
Assert.assertEquals(((SofaRpcException) e.getCause()).getErrorType(), RpcErrorType.SERVER_UNDECLARED_ERROR);
}
ok = false;
}
Assert.assertFalse(ok);
}
Aggregations