Java Code Examples for com.alipay.sofa.rpc.context.RpcInternalContext#peekContext()

The following examples show how to use com.alipay.sofa.rpc.context.RpcInternalContext#peekContext() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SofaHystrixCommand.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
public SofaHystrixCommand(FilterInvoker invoker, SofaRequest request) {
    super(SofaHystrixConfig.loadSetterFactory((ConsumerConfig) invoker.getConfig()).createSetter(invoker, request));
    this.rpcInternalContext = RpcInternalContext.peekContext();
    this.rpcInvokeContext = RpcInvokeContext.peekContext();
    this.invoker = invoker;
    this.request = request;
}
 
Example 2
Source File: SofaAsyncHystrixCommand.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
public SofaAsyncHystrixCommand(FilterInvoker invoker, SofaRequest request) {
    super(SofaHystrixConfig.loadSetterFactory((ConsumerConfig) invoker.getConfig()).createSetter(invoker,
        request));
    this.rpcInternalContext = RpcInternalContext.peekContext();
    this.rpcInvokeContext = RpcInvokeContext.peekContext();
    this.invoker = invoker;
    this.request = request;
}
 
Example 3
Source File: EventBus.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
/**
 * 给事件总线中丢一个事件
 *
 * @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.");
                }
            }
        }
    }
}
 
Example 4
Source File: AbstractSerializerTest.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@Test
public void buildSerializeError() {
    RpcInternalContext old = RpcInternalContext.peekContext();
    try {
        RpcInternalContext.removeContext();
        SofaRpcException exception = serializer.buildSerializeError("xx");
        Assert.assertEquals(RpcErrorType.UNKNOWN, exception.getErrorType());

        RpcInternalContext.getContext().setProviderSide(true);
        exception = serializer.buildSerializeError("xx");
        Assert.assertEquals(RpcErrorType.SERVER_SERIALIZE, exception.getErrorType());

        RpcInternalContext.getContext().setProviderSide(false);
        exception = serializer.buildSerializeError("xx");
        Assert.assertEquals(RpcErrorType.CLIENT_SERIALIZE, exception.getErrorType());

        RpcInternalContext.removeContext();
        exception = serializer.buildSerializeError("xx", new RuntimeException());
        Assert.assertEquals(RpcErrorType.UNKNOWN, exception.getErrorType());

        RpcInternalContext.getContext().setProviderSide(true);
        exception = serializer.buildSerializeError("xx", new RuntimeException());
        Assert.assertEquals(RpcErrorType.SERVER_SERIALIZE, exception.getErrorType());

        RpcInternalContext.getContext().setProviderSide(false);
        exception = serializer.buildSerializeError("xx", new RuntimeException());
        Assert.assertEquals(RpcErrorType.CLIENT_SERIALIZE, exception.getErrorType());
    } finally {
        RpcInternalContext.setContext(old);
    }
}
 
Example 5
Source File: AbstractSerializerTest.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@Test
public void buildDeserializeError() {
    RpcInternalContext old = RpcInternalContext.peekContext();
    try {
        RpcInternalContext.removeContext();
        SofaRpcException exception = serializer.buildDeserializeError("xx");
        Assert.assertEquals(RpcErrorType.UNKNOWN, exception.getErrorType());

        RpcInternalContext.getContext().setProviderSide(true);
        exception = serializer.buildDeserializeError("xx");
        Assert.assertEquals(RpcErrorType.SERVER_DESERIALIZE, exception.getErrorType());

        RpcInternalContext.getContext().setProviderSide(false);
        exception = serializer.buildDeserializeError("xx");
        Assert.assertEquals(RpcErrorType.CLIENT_DESERIALIZE, exception.getErrorType());

        RpcInternalContext.removeContext();
        exception = serializer.buildDeserializeError("xx", new RuntimeException());
        Assert.assertEquals(RpcErrorType.UNKNOWN, exception.getErrorType());

        RpcInternalContext.getContext().setProviderSide(true);
        exception = serializer.buildDeserializeError("xx", new RuntimeException());
        Assert.assertEquals(RpcErrorType.SERVER_DESERIALIZE, exception.getErrorType());

        RpcInternalContext.getContext().setProviderSide(false);
        exception = serializer.buildDeserializeError("xx", new RuntimeException());
        Assert.assertEquals(RpcErrorType.CLIENT_DESERIALIZE, exception.getErrorType());
    } finally {
        RpcInternalContext.setContext(old);
    }
}
 
Example 6
Source File: AllConnectConnectionHolder.java    From sofa-rpc with Apache License 2.0 4 votes vote down vote up
@Override
public ClientTransport getAvailableClientTransport(ProviderInfo providerInfo) {
    // 先去存活列表
    ClientTransport transport = aliveConnections.get(providerInfo);
    if (transport != null) {
        return transport;
    }
    // 再去亚健康列表
    transport = subHealthConnections.get(providerInfo);
    if (transport != null) {
        return transport;
    }
    // 最后看看是否第一次调用未初始化
    transport = uninitializedConnections.get(providerInfo);
    if (transport != null) {
        // 未初始化则初始化
        synchronized (this) {
            transport = uninitializedConnections.get(providerInfo);
            if (transport != null) {
                initClientTransport(consumerConfig.getInterfaceId(), providerInfo, transport);
                uninitializedConnections.remove(providerInfo);
            }
            return getAvailableClientTransport(providerInfo);
        }
    }

    if (createConnWhenAbsent) {
        RpcInternalContext context = RpcInternalContext.peekContext();
        String targetIP = (context == null) ? null : (String) context
            .getAttachment(RpcConstants.HIDDEN_KEY_PINPOINT);
        /**
         * RpcInvokeContext.getContext().setTargetUrl() 设置了地址,初始化tcp连接
         */
        if (StringUtils.isNotBlank(targetIP)) {
            ClientTransportConfig transportConfig = providerToClientConfig(providerInfo);
            transport = ClientTransportFactory.getClientTransport(transportConfig);
            initClientTransport(consumerConfig.getInterfaceId(), providerInfo, transport);
        }
    }

    return transport;
}