use of com.alipay.sofa.rpc.message.bolt.BoltSendableResponseCallback in project sofa-rpc by sofastack.
the class AsyncHelloServiceImpl method appException.
@Override
public String appException(String name) {
RpcInvokeContext context = RpcInvokeContext.getContext();
context.setTimeout(2000);
context.setResponseCallback(new BoltSendableResponseCallback() {
@Override
public void onAppResponse(Object appResponse, String methodName, RequestBase request) {
sendAppException(new RuntimeException("1234"));
}
});
// B-异步调用->C
helloService.sayHello(name, 1);
return null;
}
use of com.alipay.sofa.rpc.message.bolt.BoltSendableResponseCallback in project sofa-rpc by sofastack.
the class BAsyncChainSampleServiceImpl method echoObj.
@Override
public EchoResponse echoObj(final EchoRequest req) {
RpcInvokeContext context = RpcInvokeContext.getContext();
LOGGER.info("--b1---:" + context);
// 读取一定要在这里读取
reqBaggage = context.getRequestBaggage("reqBaggageB");
context.putResponseBaggage("respBaggageB_useful1", "在返A之前写入有用");
final CountDownLatch latch = new CountDownLatch(1);
try {
RpcInvokeContext.getContext().setResponseCallback(new BoltSendableResponseCallback() {
@Override
public void onAppResponse(Object appResponse, String methodName, RequestBase request) {
// 返回一定要写在这里
RpcInvokeContext context = RpcInvokeContext.getContext();
LOGGER.info("--b3---:" + context);
if (reqBaggage != null) {
context.putResponseBaggage("respBaggageB", "b2aaa");
} else {
context.putResponseBaggage("respBaggageB_force", "b2aaaff");
}
EchoResponse s1 = (EchoResponse) appResponse;
// 这里已经取不到值了
String reqBaggageD = context.getRequestBaggage("reqBaggageD");
LOGGER.info("----reqBaggageD---:" + reqBaggageD);
EchoResponse s2 = sampleServiceD.echoObj(req);
sendAppResponse(EchoResponse.newBuilder().setCode(200).setMessage(s1.getMessage() + s2.getMessage()).build());
LOGGER.info("--b4---:" + RpcInvokeContext.getContext());
// 返回写在这里可能没用
context.putResponseBaggage("respBaggageB_useless2", "在返A之前写后没用");
latch.countDown();
}
});
sampleServiceC.echoObj(req);
context.putResponseBaggage("respBaggageB_useful2", "在返A之前写入有用");
LOGGER.info("--b2---:" + RpcInvokeContext.getContext());
// 模拟Callback更早回来的行为
latch.await(5000, TimeUnit.MILLISECONDS);
// 返回写在这里可能没用
context.putResponseBaggage("respBaggageB_useless2", "在返A之前写后没用");
LOGGER.info("--b3---:" + RpcInvokeContext.getContext());
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
use of com.alipay.sofa.rpc.message.bolt.BoltSendableResponseCallback in project sofa-rpc by sofastack.
the class ServiceBImpl method getInt.
@Override
public int getInt(int num) {
RpcInvokeContext.getContext().setResponseCallback(new BoltSendableResponseCallback() {
@Override
public void onAppResponse(Object appResponse, String methodName, RequestBase request) {
// 此时C-异步返回->B
LOGGER.info("b get resp from c :" + appResponse);
int respToA = random.nextInt(1000);
// 调这个方法B-异步返回->A
sendAppResponse(respToA);
// 如果A是异步调用,则拿到这个appResponse返回值
}
});
String s = serviceC.getStr("xx");
return -1;
}
use of com.alipay.sofa.rpc.message.bolt.BoltSendableResponseCallback in project sofa-rpc by sofastack.
the class AsyncHelloServiceImpl method sayHello.
@Override
public String sayHello(String name, int age) {
LOGGER.info("[2]----B get req :{}, {}", name, age);
// 模拟A-->B-->C场景
RpcInvokeContext context = RpcInvokeContext.getContext();
context.setTimeout(2000);
context.setResponseCallback(new BoltSendableResponseCallback() {
@Override
public void onAppResponse(Object appResponse, String methodName, RequestBase request) {
// 此时C-异步返回->B
LOGGER.info("[5]----B get result: {}", appResponse);
// 调这个方法B-异步返回->A
sendAppResponse(appResponse);
// 如果A是异步调用,则拿到这个appResponse返回值
}
});
// B-异步调用->C
String c0 = helloService.sayHello(name, age);
if (c0 != null) {
LOGGER.error("--------c0 is not null");
}
// 如果设置了AsyncProxyResponseCallback,则此处返回其实是无效。
return "hello async无效返回";
}
use of com.alipay.sofa.rpc.message.bolt.BoltSendableResponseCallback in project sofa-rpc by sofastack.
the class AsyncHelloServiceImpl method rpcException.
@Override
public String rpcException(String name) {
RpcInvokeContext context = RpcInvokeContext.getContext();
context.setTimeout(2000);
context.setResponseCallback(new BoltSendableResponseCallback() {
@Override
public void onAppResponse(Object appResponse, String methodName, RequestBase request) {
sendSofaException(new SofaRpcException(RpcErrorType.SERVER_BUSY, "bbb"));
}
});
// B-异步调用->C
helloService.sayHello(name, 1);
return null;
}
Aggregations