Search in sources :

Example 1 with RpcInvokeContext

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;
}
Also used : RpcInvokeContext(com.alipay.sofa.rpc.context.RpcInvokeContext) BoltSendableResponseCallback(com.alipay.sofa.rpc.message.bolt.BoltSendableResponseCallback) RequestBase(com.alipay.sofa.rpc.core.request.RequestBase)

Example 2 with RpcInvokeContext

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;
}
Also used : EchoResponse(com.alipay.sofa.rpc.server.bolt.pb.EchoResponse) RpcInvokeContext(com.alipay.sofa.rpc.context.RpcInvokeContext) BoltSendableResponseCallback(com.alipay.sofa.rpc.message.bolt.BoltSendableResponseCallback) CountDownLatch(java.util.concurrent.CountDownLatch) RequestBase(com.alipay.sofa.rpc.core.request.RequestBase)

Example 3 with RpcInvokeContext

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();
}
Also used : EchoResponse(com.alipay.sofa.rpc.server.bolt.pb.EchoResponse) RpcInvokeContext(com.alipay.sofa.rpc.context.RpcInvokeContext) SofaResponseCallback(com.alipay.sofa.rpc.core.invoke.SofaResponseCallback) CountDownLatch(java.util.concurrent.CountDownLatch) RequestBase(com.alipay.sofa.rpc.core.request.RequestBase) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException)

Example 4 with RpcInvokeContext

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();
}
Also used : EchoResponse(com.alipay.sofa.rpc.server.bolt.pb.EchoResponse) RpcInvokeContext(com.alipay.sofa.rpc.context.RpcInvokeContext)

Example 5 with RpcInvokeContext

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;
}
Also used : RpcInvokeContext(com.alipay.sofa.rpc.context.RpcInvokeContext)

Aggregations

RpcInvokeContext (com.alipay.sofa.rpc.context.RpcInvokeContext)37 SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)12 RequestBase (com.alipay.sofa.rpc.core.request.RequestBase)10 ConsumerConfig (com.alipay.sofa.rpc.config.ConsumerConfig)7 ProviderConfig (com.alipay.sofa.rpc.config.ProviderConfig)7 RpcInternalContext (com.alipay.sofa.rpc.context.RpcInternalContext)7 ApplicationConfig (com.alipay.sofa.rpc.config.ApplicationConfig)6 ServerConfig (com.alipay.sofa.rpc.config.ServerConfig)6 SofaResponseCallback (com.alipay.sofa.rpc.core.invoke.SofaResponseCallback)6 CountDownLatch (java.util.concurrent.CountDownLatch)6 BoltSendableResponseCallback (com.alipay.sofa.rpc.message.bolt.BoltSendableResponseCallback)5 EchoResponse (com.alipay.sofa.rpc.server.bolt.pb.EchoResponse)5 MethodConfig (com.alipay.sofa.rpc.config.MethodConfig)4 Future (java.util.concurrent.Future)4 SofaTraceContext (com.alipay.common.tracer.core.context.trace.SofaTraceContext)3 SofaTracerSpan (com.alipay.common.tracer.core.span.SofaTracerSpan)3 HashMap (java.util.HashMap)3 SofaResponseFuture (com.alipay.sofa.rpc.api.future.SofaResponseFuture)2 SofaTimeOutException (com.alipay.sofa.rpc.core.exception.SofaTimeOutException)2 SofaResponse (com.alipay.sofa.rpc.core.response.SofaResponse)2