use of com.alipay.sofa.rpc.message.ResponseFuture in project sofa-rpc by sofastack.
the class Http2ClearTextExceptionTest method testProtobuf.
@Test
public void testProtobuf() throws InterruptedException {
// 只有1个线程 执行
ServerConfig serverConfig = new ServerConfig().setStopTimeout(60000).setPort(12333).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C).setDaemon(true);
// 发布一个服务,每个请求要执行1秒
ProviderConfig<HttpService> providerConfig = new ProviderConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setRef(new HttpServiceImpl()).setServer(serverConfig).setApplication(new ApplicationConfig().setAppName("serverApp")).setRegister(false);
providerConfig.export();
{
ConsumerConfig<HttpService> consumerConfig = new ConsumerConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setSerialization(RpcConstants.SERIALIZE_PROTOBUF).setDirectUrl("h2c://127.0.0.1:12333").setApplication(new ApplicationConfig().setAppName("clientApp")).setTimeout(500).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C);
HttpService httpService = consumerConfig.refer();
EchoRequest request = EchoRequest.newBuilder().setGroup(Group.B).setName("xxx").build();
try {
EchoResponse response = httpService.echoPb(request);
Assert.fail();
} catch (SofaRpcException e) {
Assert.assertTrue(e.getErrorType() == RpcErrorType.SERVER_BIZ);
}
}
{
ConsumerConfig<HttpService> consumerConfig2 = new ConsumerConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setSerialization(RpcConstants.SERIALIZE_PROTOBUF).setDirectUrl("h2c://127.0.0.1:12333").setApplication(new ApplicationConfig().setAppName("clientApp")).setTimeout(500).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C).setInvokeType(RpcConstants.INVOKER_TYPE_ONEWAY).setRepeatedReferLimit(-1);
HttpService httpService2 = consumerConfig2.refer();
EchoRequest request = EchoRequest.newBuilder().setGroup(Group.B).setName("xxx").build();
try {
httpService2.echoPb(request);
// NOT SUPPORTED NOW, If want support this, need add key to head.
Assert.fail();
} catch (Exception e) {
Assert.assertTrue(e instanceof SofaRpcException);
}
}
{
ConsumerConfig<HttpService> consumerConfig3 = new ConsumerConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setSerialization(RpcConstants.SERIALIZE_PROTOBUF).setDirectUrl("h2c://127.0.0.1:12333").setApplication(new ApplicationConfig().setAppName("clientApp")).setTimeout(500).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C).setInvokeType(RpcConstants.INVOKER_TYPE_FUTURE).setRepeatedReferLimit(-1);
HttpService httpService3 = consumerConfig3.refer();
EchoRequest request = EchoRequest.newBuilder().setGroup(Group.B).setName("xxx").build();
EchoResponse response = httpService3.echoPb(request);
Assert.assertNull(response);
ResponseFuture future = RpcInvokeContext.getContext().getFuture();
try {
future.get();
Assert.fail();
} catch (ExecutionException e) {
SofaRpcException re = (SofaRpcException) e.getCause();
Assert.assertTrue(re.getErrorType() == RpcErrorType.SERVER_BIZ);
}
}
{
final Object[] result = new Object[1];
final CountDownLatch latch = new CountDownLatch(1);
ConsumerConfig<HttpService> consumerConfig4 = new ConsumerConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setSerialization(RpcConstants.SERIALIZE_PROTOBUF).setTimeout(500).setDirectUrl("h2c://127.0.0.1:12333").setApplication(new ApplicationConfig().setAppName("clientApp")).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C).setInvokeType(RpcConstants.INVOKER_TYPE_CALLBACK).setOnReturn(new SofaResponseCallback() {
@Override
public void onAppResponse(Object appResponse, String methodName, RequestBase request) {
result[0] = appResponse;
latch.countDown();
}
@Override
public void onAppException(Throwable throwable, String methodName, RequestBase request) {
result[0] = throwable;
latch.countDown();
}
@Override
public void onSofaException(SofaRpcException exception, String methodName, RequestBase request) {
result[0] = exception;
latch.countDown();
}
}).setRepeatedReferLimit(-1);
HttpService httpService4 = consumerConfig4.refer();
EchoRequest request = EchoRequest.newBuilder().setGroup(Group.B).setName("xxx").build();
EchoResponse response = httpService4.echoPb(request);
Assert.assertNull(response);
latch.await(2000, TimeUnit.MILLISECONDS);
Throwable e = (Throwable) result[0];
Assert.assertTrue(e instanceof SofaRpcException);
Assert.assertTrue(((SofaRpcException) e).getErrorType() == RpcErrorType.SERVER_BIZ);
}
}
use of com.alipay.sofa.rpc.message.ResponseFuture in project sofa-rpc by sofastack.
the class Http2ClearTextHessianTest method testHessian.
@Test
public void testHessian() {
// 只有1个线程 执行
ServerConfig serverConfig = new ServerConfig().setStopTimeout(60000).setPort(12300).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C).setDaemon(true);
// 发布一个服务,每个请求要执行1秒
ProviderConfig<HttpService> providerConfig = new ProviderConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setRef(new HttpServiceImpl()).setApplication(new ApplicationConfig().setAppName("serverApp")).setServer(serverConfig).setRegister(false);
providerConfig.export();
{
ConsumerConfig<HttpService> consumerConfig = new ConsumerConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setDirectUrl("h2c://127.0.0.1:12300").setApplication(new ApplicationConfig().setAppName("clientApp")).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C);
HttpService httpService = consumerConfig.refer();
ExampleObj request = new ExampleObj();
request.setId(200);
request.setName("yyy");
ExampleObj response = httpService.object(request);
Assert.assertEquals(200, response.getId());
Assert.assertEquals("yyyxx", response.getName());
}
{
ConsumerConfig<HttpService> consumerConfig2 = new ConsumerConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setDirectUrl("h2c://127.0.0.1:12300").setApplication(new ApplicationConfig().setAppName("clientApp")).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C).setInvokeType(RpcConstants.INVOKER_TYPE_ONEWAY).setRepeatedReferLimit(-1);
HttpService httpService2 = consumerConfig2.refer();
EchoRequest request = EchoRequest.newBuilder().setGroup(Group.A).setName("xxx").build();
try {
httpService2.echoPb(request);
// NOT SUPPORTED NOW, If want support this, need add key to head.
Assert.fail();
} catch (Exception e) {
}
}
{
ConsumerConfig<HttpService> consumerConfig3 = new ConsumerConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setDirectUrl("h2c://127.0.0.1:12300").setApplication(new ApplicationConfig().setAppName("clientApp")).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C).setInvokeType(RpcConstants.INVOKER_TYPE_FUTURE).setRepeatedReferLimit(-1);
HttpService httpService3 = consumerConfig3.refer();
ExampleObj request = new ExampleObj();
request.setId(200);
request.setName("yyy");
ExampleObj response = httpService3.object(request);
Assert.assertNull(response);
ResponseFuture<ExampleObj> future = RpcInvokeContext.getContext().getFuture();
try {
response = future.get();
Assert.assertEquals(200, response.getId());
Assert.assertEquals("yyyxx", response.getName());
} catch (Exception e) {
e.printStackTrace();
Assert.fail();
}
}
{
final ExampleObj[] result = new ExampleObj[1];
final CountDownLatch latch = new CountDownLatch(1);
ConsumerConfig<HttpService> consumerConfig4 = new ConsumerConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setDirectUrl("h2c://127.0.0.1:12300").setApplication(new ApplicationConfig().setAppName("clientApp")).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C).setInvokeType(RpcConstants.INVOKER_TYPE_CALLBACK).setOnReturn(new SofaResponseCallback() {
@Override
public void onAppResponse(Object appResponse, String methodName, RequestBase request) {
result[0] = (ExampleObj) appResponse;
latch.countDown();
}
@Override
public void onAppException(Throwable throwable, String methodName, RequestBase request) {
latch.countDown();
}
@Override
public void onSofaException(SofaRpcException sofaException, String methodName, RequestBase request) {
latch.countDown();
}
}).setRepeatedReferLimit(-1);
HttpService httpService4 = consumerConfig4.refer();
ExampleObj request = new ExampleObj();
request.setId(200);
request.setName("yyy");
ExampleObj response = httpService4.object(request);
Assert.assertNull(response);
try {
latch.await(2000, TimeUnit.MILLISECONDS);
response = result[0];
Assert.assertEquals(200, response.getId());
Assert.assertEquals("yyyxx", response.getName());
} catch (Exception e) {
e.printStackTrace();
Assert.fail();
}
}
}
use of com.alipay.sofa.rpc.message.ResponseFuture in project sofa-rpc by sofastack.
the class Http2ClearTextPriorKnowledgeTest method testProtobuf.
@Test
public void testProtobuf() {
// 只有1个线程 执行
ServerConfig serverConfig = new ServerConfig().setStopTimeout(60000).setPort(12300).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C).setDaemon(true);
// 发布一个服务,每个请求要执行1秒
ProviderConfig<HttpService> providerConfig = new ProviderConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setRef(new HttpServiceImpl()).setApplication(new ApplicationConfig().setAppName("serverApp")).setServer(serverConfig).setRegister(false);
providerConfig.export();
{
ConsumerConfig<HttpService> consumerConfig = new ConsumerConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setSerialization(RpcConstants.SERIALIZE_PROTOBUF).setDirectUrl("h2c://127.0.0.1:12300").setApplication(new ApplicationConfig().setAppName("clientApp")).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C);
HttpService httpService = consumerConfig.refer();
EchoRequest request = EchoRequest.newBuilder().setGroup(Group.A).setName("xxx").build();
EchoResponse response = httpService.echoPb(request);
Assert.assertEquals("helloxxx", response.getMessage());
Assert.assertEquals(200, response.getCode());
}
{
ConsumerConfig<HttpService> consumerConfig2 = new ConsumerConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setSerialization(RpcConstants.SERIALIZE_PROTOBUF).setDirectUrl("h2c://127.0.0.1:12300").setApplication(new ApplicationConfig().setAppName("clientApp")).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C).setInvokeType(RpcConstants.INVOKER_TYPE_ONEWAY).setRepeatedReferLimit(-1);
HttpService httpService2 = consumerConfig2.refer();
EchoRequest request = EchoRequest.newBuilder().setGroup(Group.A).setName("xxx").build();
try {
httpService2.echoPb(request);
// NOT SUPPORTED NOW, If want support this, need add key to head.
Assert.fail();
} catch (Exception e) {
}
}
{
ConsumerConfig<HttpService> consumerConfig3 = new ConsumerConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setSerialization(RpcConstants.SERIALIZE_PROTOBUF).setDirectUrl("h2c://127.0.0.1:12300").setApplication(new ApplicationConfig().setAppName("clientApp")).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C).setInvokeType(RpcConstants.INVOKER_TYPE_FUTURE).setRepeatedReferLimit(-1);
HttpService httpService3 = consumerConfig3.refer();
EchoRequest request = EchoRequest.newBuilder().setGroup(Group.A).setName("xxx").build();
EchoResponse response = httpService3.echoPb(request);
Assert.assertNull(response);
ResponseFuture future = RpcInvokeContext.getContext().getFuture();
try {
response = (EchoResponse) future.get();
Assert.assertEquals("helloxxx", response.getMessage());
Assert.assertEquals(200, response.getCode());
} catch (Exception e) {
e.printStackTrace();
Assert.fail();
}
}
{
final EchoResponse[] result = new EchoResponse[1];
final CountDownLatch latch = new CountDownLatch(1);
ConsumerConfig<HttpService> consumerConfig4 = new ConsumerConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setSerialization(RpcConstants.SERIALIZE_PROTOBUF).setDirectUrl("h2c://127.0.0.1:12300").setApplication(new ApplicationConfig().setAppName("clientApp")).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C).setInvokeType(RpcConstants.INVOKER_TYPE_CALLBACK).setOnReturn(new SofaResponseCallback() {
@Override
public void onAppResponse(Object appResponse, String methodName, RequestBase request) {
result[0] = (EchoResponse) appResponse;
latch.countDown();
}
@Override
public void onAppException(Throwable throwable, String methodName, RequestBase request) {
latch.countDown();
}
@Override
public void onSofaException(SofaRpcException sofaException, String methodName, RequestBase request) {
latch.countDown();
}
}).setRepeatedReferLimit(-1);
HttpService httpService4 = consumerConfig4.refer();
EchoRequest request = EchoRequest.newBuilder().setGroup(Group.A).setName("xxx").build();
EchoResponse response = httpService4.echoPb(request);
Assert.assertNull(response);
try {
latch.await(2000, TimeUnit.MILLISECONDS);
response = result[0];
Assert.assertNotNull(response);
Assert.assertEquals("helloxxx", response.getMessage());
Assert.assertEquals(200, response.getCode());
} catch (Exception e) {
e.printStackTrace();
Assert.fail();
}
}
}
use of com.alipay.sofa.rpc.message.ResponseFuture 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.message.ResponseFuture in project sofa-rpc by sofastack.
the class DefaultClientProxyInvoker method decorateResponse.
@Override
protected void decorateResponse(SofaResponse response) {
// 公共的设置
super.decorateResponse(response);
// 上下文内转外
RpcInternalContext context = RpcInternalContext.getContext();
ResponseFuture future = context.getFuture();
RpcInvokeContext invokeCtx = null;
if (future != null) {
invokeCtx = RpcInvokeContext.getContext();
invokeCtx.setFuture(future);
}
if (RpcInvokeContext.isBaggageEnable()) {
BaggageResolver.pickupFromResponse(invokeCtx, response, true);
}
// bad code
if (RpcInternalContext.isAttachmentEnable()) {
String resultCode = (String) context.getAttachment(INTERNAL_KEY_RESULT_CODE);
if (resultCode != null) {
if (invokeCtx == null) {
invokeCtx = RpcInvokeContext.getContext();
}
invokeCtx.put(RemotingConstants.INVOKE_CTX_RPC_RESULT_CODE, resultCode);
}
}
}
Aggregations