Search in sources :

Example 11 with RpcInvokeContext

use of com.alipay.sofa.rpc.context.RpcInvokeContext in project sofa-rpc by sofastack.

the class DSampleServiceImpl method hello.

@Override
public String hello() {
    RpcInvokeContext context = RpcInvokeContext.getContext();
    LOGGER.info("----d-----:" + context);
    reqBaggage = context.getRequestBaggage("reqBaggageD");
    if (reqBaggage != null) {
        context.putResponseBaggage("respBaggageD", "d2aaa");
    } else {
        context.putResponseBaggage("respBaggageD_force", "d2aaaff");
    }
    return "hello world d";
}
Also used : RpcInvokeContext(com.alipay.sofa.rpc.context.RpcInvokeContext)

Example 12 with RpcInvokeContext

use of com.alipay.sofa.rpc.context.RpcInvokeContext in project sofa-rpc by sofastack.

the class DSampleServiceImpl method echoObj.

@Override
public EchoResponse echoObj(EchoRequest req) {
    RpcInvokeContext context = RpcInvokeContext.getContext();
    LOGGER.info("----d-----:" + context);
    reqBaggage = context.getRequestBaggage("reqBaggageD");
    if (reqBaggage != null) {
        context.putResponseBaggage("respBaggageD", "d2aaa");
    } else {
        context.putResponseBaggage("respBaggageD_force", "d2aaaff");
    }
    return EchoResponse.newBuilder().setCode(200).setMessage("hello world d").build();
}
Also used : RpcInvokeContext(com.alipay.sofa.rpc.context.RpcInvokeContext)

Example 13 with RpcInvokeContext

use of com.alipay.sofa.rpc.context.RpcInvokeContext in project sofa-rpc by sofastack.

the class RpcContextManager method lastReferenceContext.

/**
 * get the last reference invoke information
 *
 * @param clear true: framework will clear the ThreadLocal when return
 * @return RPC Reference Context, it can be null
 */
public static RpcReferenceContext lastReferenceContext(boolean clear) {
    try {
        RpcInvokeContext invokeCtx = RpcInvokeContext.getContext();
        RpcReferenceContext referenceCtx = (RpcReferenceContext) invokeCtx.get(RemotingConstants.INVOKE_CTX_RPC_REF_CTX);
        if (referenceCtx != null) {
            String resultCode = (String) invokeCtx.get(RemotingConstants.INVOKE_CTX_RPC_RESULT_CODE);
            if (resultCode != null) {
                referenceCtx.setResultCode(ResultCodeEnum.getResultCode(resultCode));
            }
        }
        return referenceCtx;
    } finally {
        if (clear) {
            clearReferenceContext();
        }
    }
}
Also used : RpcInvokeContext(com.alipay.sofa.rpc.context.RpcInvokeContext)

Example 14 with RpcInvokeContext

use of com.alipay.sofa.rpc.context.RpcInvokeContext in project sofa-rpc by sofastack.

the class SofaResponseFuture method getResponse.

/**
 * get response
 * <p>
 * If remoting get exception, framework will wrapped it to SofaRpcException
 *
 * @param timeout get timeout
 * @param clear   true: framework will clear the ThreadLocal when return
 * @return The response
 * @throws SofaRpcException When throw SofaRpcException
 * @throws InterruptedException
 *          if any thread has interrupted the current thread. The
 *          <i>interrupted status</i> of the current thread is
 *          cleared when this exception is thrown.
 */
public static Object getResponse(long timeout, boolean clear) throws SofaRpcException, InterruptedException {
    RpcInvokeContext context = RpcInvokeContext.getContext();
    Future future = context.getFuture();
    if (null == future) {
        throw new SofaRpcException(RpcErrorType.CLIENT_UNDECLARED_ERROR, LogCodes.getLog(LogCodes.ERROR_RESPONSE_FUTURE_NULL, Thread.currentThread()));
    }
    try {
        if (clear) {
            context.setFuture(null);
        }
        return future.get(timeout, TimeUnit.MILLISECONDS);
    } catch (TimeoutException ex) {
        // Future设置为超时
        if (!future.isDone()) {
            throw new SofaTimeOutException("Future is not done when timeout.", ex);
        } else {
            throw new SofaTimeOutException(ex.getMessage(), ex);
        }
    } catch (ExecutionException ex) {
        Throwable cause = ex.getCause();
        if (cause instanceof SofaRpcException) {
            throw (SofaRpcException) cause;
        } else {
            throw new SofaRpcException(RpcErrorType.SERVER_UNDECLARED_ERROR, cause.getMessage(), cause);
        }
    }
}
Also used : RpcInvokeContext(com.alipay.sofa.rpc.context.RpcInvokeContext) SofaTimeOutException(com.alipay.sofa.rpc.core.exception.SofaTimeOutException) Future(java.util.concurrent.Future) ExecutionException(java.util.concurrent.ExecutionException) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) TimeoutException(java.util.concurrent.TimeoutException)

Example 15 with RpcInvokeContext

use of com.alipay.sofa.rpc.context.RpcInvokeContext in project sofa-rpc by sofastack.

the class SofaResponseFuture method getFuture.

/**
 * @param clear 是否清除线程上下文
 * @return 原生 Java Future 对象
 * @throws SofaRpcException 当前线程上下文没有值的时候
 */
public static Future getFuture(boolean clear) throws SofaRpcException {
    RpcInvokeContext context = RpcInvokeContext.getContext();
    Future future = context.getFuture();
    if (future == null) {
        throw new SofaRpcException(RpcErrorType.CLIENT_UNDECLARED_ERROR, LogCodes.getLog(LogCodes.ERROR_RESPONSE_FUTURE_NULL, Thread.currentThread()));
    }
    if (clear) {
        context.setFuture(null);
    }
    return future;
}
Also used : RpcInvokeContext(com.alipay.sofa.rpc.context.RpcInvokeContext) Future(java.util.concurrent.Future) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException)

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