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";
}
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();
}
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();
}
}
}
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);
}
}
}
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;
}
Aggregations