use of com.alipay.sofa.rpc.context.RpcInvokeContext 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.context.RpcInvokeContext 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.context.RpcInvokeContext in project sofa-rpc by sofastack.
the class BCallbackSampleServiceImpl method echoObj.
@Override
public EchoResponse echoObj(EchoRequest req) {
RpcInvokeContext context = RpcInvokeContext.getContext();
LOGGER.info("--b1-----:" + context);
reqBaggage = context.getRequestBaggage("reqBaggageB");
if (reqBaggage != null) {
context.putResponseBaggage("respBaggageB", "b2aaa");
} else {
context.putResponseBaggage("respBaggageB_force", "b2aaaff");
}
final EchoResponse[] str = new EchoResponse[2];
final CountDownLatch latch = new CountDownLatch(2);
try {
RpcInvokeContext.getContext().setResponseCallback(new SofaResponseCallback() {
@Override
public void onAppResponse(Object appResponse, String methodName, RequestBase request) {
str[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();
}
});
sampleServiceC.hello();
RpcInvokeContext.getContext().setResponseCallback(new SofaResponseCallback() {
@Override
public void onAppResponse(Object appResponse, String methodName, RequestBase request) {
str[1] = (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();
}
});
sampleServiceD.hello();
latch.await(2000, TimeUnit.MILLISECONDS);
} catch (Exception e) {
e.printStackTrace();
}
EchoResponse s1 = str[0];
EchoResponse s2 = str[1];
return EchoResponse.newBuilder().setCode(200).setMessage(s1.getMessage() + s2.getMessage()).build();
}
use of com.alipay.sofa.rpc.context.RpcInvokeContext in project sofa-rpc by sofastack.
the class BSampleServiceImpl method echoObj.
@Override
public EchoResponse echoObj(EchoRequest req) {
RpcInvokeContext context = RpcInvokeContext.getContext();
LOGGER.info("-----b-----:" + context);
reqBaggage = context.getRequestBaggage("reqBaggageB");
if (reqBaggage != null) {
context.putResponseBaggage("respBaggageB", "b2aaa");
} else {
context.putResponseBaggage("respBaggageB_force", "b2aaaff");
}
EchoResponse s1 = sampleServiceC.echoObj(req);
EchoResponse s2 = sampleServiceD.echoObj(req);
return EchoResponse.newBuilder().setCode(200).setMessage(s1.getMessage() + s2.getMessage()).build();
}
use of com.alipay.sofa.rpc.context.RpcInvokeContext in project sofa-rpc by sofastack.
the class BSampleServiceImpl method hello.
@Override
public String hello() {
RpcInvokeContext context = RpcInvokeContext.getContext();
LOGGER.info("-----b-----:" + context);
reqBaggage = context.getRequestBaggage("reqBaggageB");
if (reqBaggage != null) {
context.putResponseBaggage("respBaggageB", "b2aaa");
} else {
context.putResponseBaggage("respBaggageB_force", "b2aaaff");
}
String s1 = sampleServiceC.hello();
String s2 = sampleServiceD.hello();
return s1 + s2;
}
Aggregations