use of com.alipay.sofa.rpc.core.response.SofaResponse in project sofa-rpc by sofastack.
the class BaggageResolverTest method testPickupFromResponse.
@Test
public void testPickupFromResponse() {
SofaResponse sofaResponse = new SofaResponse();
RpcInvokeContext context = RpcInvokeContext.getContext();
context.putResponseBaggage(KEY, VALUE);
sofaResponse.addResponseProp(RemotingConstants.RPC_RESPONSE_BAGGAGE + ".key", VALUE);
sofaResponse.addResponseProp(RemotingConstants.RPC_RESPONSE_BAGGAGE + ".key2", null);
BaggageResolver.pickupFromResponse(context, sofaResponse);
assertEquals(VALUE, RpcInvokeContext.getContext().getAllResponseBaggage().get(KEY));
assertEquals(null, RpcInvokeContext.getContext().getAllResponseBaggage().get("key2"));
assertEquals(1, RpcInvokeContext.getContext().getAllResponseBaggage().size());
assertEquals(SafeConcurrentHashMap.class, RpcInvokeContext.getContext().getAllResponseBaggage().getClass());
}
use of com.alipay.sofa.rpc.core.response.SofaResponse in project sofa-rpc by sofastack.
the class BaggageResolverTest method testCarryWithResponse.
@Test
public void testCarryWithResponse() {
SofaResponse sofaResponse = new SofaResponse();
RpcInvokeContext context = RpcInvokeContext.getContext();
context.putResponseBaggage(KEY, VALUE);
BaggageResolver.carryWithResponse(context, sofaResponse);
String baggage = (String) sofaResponse.getResponseProp(RemotingConstants.RPC_RESPONSE_BAGGAGE + "." + KEY);
assertNotNull(baggage);
assertEquals(VALUE, baggage);
}
use of com.alipay.sofa.rpc.core.response.SofaResponse in project sofa-rpc by sofastack.
the class ProviderProxyInvoker method invoke.
/**
* proxy拦截的调用
*
* @param request 请求消息
* @return 调用结果
* @throws SofaRpcException rpc异常
*/
@Override
public SofaResponse invoke(SofaRequest request) throws SofaRpcException {
RpcInvokeContext.getContext().put(RpcConstants.INTERNAL_KEY_PROVIDER_FILTER_START_TIME_NANO, System.nanoTime());
SofaResponse sofaResponse = filterChain.invoke(request);
RpcInvokeContext.getContext().put(RpcConstants.INTERNAL_KEY_PROVIDER_FILTER_END_TIME_NANO, System.nanoTime());
calculateProviderFilterTime();
return sofaResponse;
}
use of com.alipay.sofa.rpc.core.response.SofaResponse in project sofa-rpc by sofastack.
the class MessageBuilder method buildSofaErrorResponse.
/**
* 构建rpc错误结果
*
* @param errorMsg 错误消息
* @return rpc结果
*/
public static SofaResponse buildSofaErrorResponse(String errorMsg) {
SofaResponse sofaResponse = new SofaResponse();
sofaResponse.setErrorMsg(errorMsg);
return sofaResponse;
}
use of com.alipay.sofa.rpc.core.response.SofaResponse in project sofa-rpc by sofastack.
the class TripleClientInvoker method invoke.
@Override
public SofaResponse invoke(SofaRequest sofaRequest, int timeout) throws Exception {
if (!useGeneric) {
SofaResponse sofaResponse = new SofaResponse();
Object stub = sofaStub.invoke(null, channel, buildCustomCallOptions(sofaRequest, timeout), timeout);
final Method method = sofaRequest.getMethod();
Object appResponse = method.invoke(stub, sofaRequest.getMethodArgs()[0]);
sofaResponse.setAppResponse(appResponse);
return sofaResponse;
} else {
String serviceName = sofaRequest.getInterfaceName();
String methodName = sofaRequest.getMethodName();
MethodDescriptor.Marshaller<?> requestMarshaller = null;
MethodDescriptor.Marshaller<?> responseMarshaller = null;
requestMarshaller = io.grpc.protobuf.ProtoUtils.marshaller(Request.getDefaultInstance());
responseMarshaller = io.grpc.protobuf.ProtoUtils.marshaller(Response.getDefaultInstance());
String fullMethodName = generateFullMethodName(serviceName, methodName);
MethodDescriptor methodDescriptor = io.grpc.MethodDescriptor.newBuilder().setType(io.grpc.MethodDescriptor.MethodType.UNARY).setFullMethodName(fullMethodName).setSampledToLocalTracing(true).setRequestMarshaller((MethodDescriptor.Marshaller<Object>) requestMarshaller).setResponseMarshaller((MethodDescriptor.Marshaller<Object>) responseMarshaller).build();
Request request = getRequest(sofaRequest, serialization, serializer);
Response response = (Response) ClientCalls.blockingUnaryCall(channel, methodDescriptor, buildCustomCallOptions(sofaRequest, timeout), request);
SofaResponse sofaResponse = new SofaResponse();
byte[] responseDate = response.getData().toByteArray();
Class returnType = sofaRequest.getMethod().getReturnType();
if (returnType != void.class) {
if (responseDate != null && responseDate.length > 0) {
Serializer responseSerializer = SerializerFactory.getSerializer(response.getSerializeType());
Object appResponse = responseSerializer.decode(new ByteArrayWrapperByteBuf(responseDate), returnType, null);
sofaResponse.setAppResponse(appResponse);
}
}
return sofaResponse;
}
}
Aggregations