use of com.alipay.sofa.rpc.context.RpcInternalContext in project sofa-rpc by sofastack.
the class AbstractProxyClientTransport method syncSend.
@Override
public SofaResponse syncSend(SofaRequest request, int timeout) throws SofaRpcException {
RpcInternalContext context = RpcInternalContext.getContext();
SofaResponse response = null;
SofaRpcException throwable = null;
try {
beforeSend(context, request);
if (EventBus.isEnable(ClientBeforeSendEvent.class)) {
EventBus.post(new ClientBeforeSendEvent(request));
}
response = doInvokeSync(request, timeout);
return response;
} catch (InvocationTargetException e) {
throwable = convertToRpcException(e);
throw throwable;
} catch (SofaRpcException e) {
throwable = e;
throw e;
} catch (Exception e) {
throwable = new SofaRpcException(RpcErrorType.CLIENT_UNDECLARED_ERROR, "Failed to send message to remote", e);
throw throwable;
} finally {
afterSend(context, request);
if (EventBus.isEnable(ClientSyncReceiveEvent.class)) {
EventBus.post(new ClientSyncReceiveEvent(transportConfig.getConsumerConfig(), transportConfig.getProviderInfo(), request, response, throwable));
}
}
}
use of com.alipay.sofa.rpc.context.RpcInternalContext in project sofa-rpc by sofastack.
the class ClientProxyInvoker method invoke.
/**
* proxy拦截的调用
*
* @param request 请求消息
* @return 调用结果
*/
@Override
public SofaResponse invoke(SofaRequest request) throws SofaRpcException {
SofaResponse response = null;
Throwable throwable = null;
try {
RpcInternalContext.pushContext();
RpcInternalContext context = RpcInternalContext.getContext();
context.setProviderSide(false);
// 包装请求
decorateRequest(request);
try {
// 产生开始调用事件
if (EventBus.isEnable(ClientStartInvokeEvent.class)) {
EventBus.post(new ClientStartInvokeEvent(request));
}
// 得到结果
response = cluster.invoke(request);
} catch (SofaRpcException e) {
throwable = e;
throw e;
} finally {
// 产生调用结束事件
if (!request.isAsync()) {
if (EventBus.isEnable(ClientEndInvokeEvent.class)) {
EventBus.post(new ClientEndInvokeEvent(request, response, throwable));
}
}
}
// 包装响应
decorateResponse(response);
return response;
} finally {
RpcInternalContext.removeContext();
RpcInternalContext.popContext();
}
}
use of com.alipay.sofa.rpc.context.RpcInternalContext in project sofa-rpc by sofastack.
the class EventBus method post.
/**
* 给事件总线中丢一个事件
*
* @param event 事件
*/
public static void post(final Event event) {
if (!isEnable()) {
return;
}
CopyOnWriteArraySet<Subscriber> subscribers = SUBSCRIBER_MAP.get(event.getClass());
if (CommonUtils.isNotEmpty(subscribers)) {
for (final Subscriber subscriber : subscribers) {
if (subscriber.isSync()) {
handleEvent(subscriber, event);
} else {
// 异步
final RpcInternalContext context = RpcInternalContext.peekContext();
final ThreadPoolExecutor asyncThreadPool = AsyncRuntime.getAsyncThreadPool();
try {
asyncThreadPool.execute(new Runnable() {
@Override
public void run() {
try {
RpcInternalContext.setContext(context);
handleEvent(subscriber, event);
} finally {
RpcInternalContext.removeContext();
}
}
});
} catch (RejectedExecutionException e) {
LOGGER.warn("This queue is full when post event to async execute, queue size is " + asyncThreadPool.getQueue().size() + ", please optimize this async thread pool of eventbus.");
}
}
}
}
}
use of com.alipay.sofa.rpc.context.RpcInternalContext in project sofa-rpc by sofastack.
the class Router method recordRouterWay.
/**
* 记录路由路径记录
*
* @param routerName 路由名字
* @since 5.2.0
*/
protected void recordRouterWay(String routerName) {
if (RpcInternalContext.isAttachmentEnable()) {
RpcInternalContext context = RpcInternalContext.getContext();
String record = (String) context.getAttachment(RpcConstants.INTERNAL_KEY_ROUTER_RECORD);
record = record == null ? routerName : record + ">" + routerName;
context.setAttachment(RpcConstants.INTERNAL_KEY_ROUTER_RECORD, record);
}
}
use of com.alipay.sofa.rpc.context.RpcInternalContext in project sofa-rpc by sofastack.
the class AbstractHttp2ClientTransport method asyncSend.
@Override
public ResponseFuture asyncSend(SofaRequest request, int timeout) throws SofaRpcException {
checkConnection();
RpcInternalContext context = RpcInternalContext.getContext();
try {
beforeSend(context, request);
return doInvokeAsync(request, context, timeout);
} catch (SofaRpcException e) {
throw e;
} catch (Exception e) {
throw new SofaRpcException(RpcErrorType.CLIENT_UNDECLARED_ERROR, e.getMessage(), e);
} finally {
afterSend(context, request);
}
}
Aggregations