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

The following examples show how to use com.alipay.sofa.rpc.context.RpcInternalContext#setContext() . 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: SyncInvokeClientHandler.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
@Override
public void doOnResponse(Object result) {
    if (rpcFuture == null) {
        return;
    }
    ClassLoader oldCl = Thread.currentThread().getContextClassLoader();
    SofaResponse response = (SofaResponse) result;
    try {
        Thread.currentThread().setContextClassLoader(this.classLoader);
        RpcInternalContext.setContext(context);
        decode(response);
        rpcFuture.setSuccess(response);
    } finally {
        Thread.currentThread().setContextClassLoader(oldCl);
        RpcInvokeContext.removeContext();
        RpcInternalContext.removeAllContext();
    }
}
 
Example 2
Source File: SyncInvokeClientHandler.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
@Override
public void doOnException(Throwable e) {
    if (rpcFuture == null) {
        return;
    }
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(this.classLoader);
        RpcInternalContext.setContext(context);
        rpcFuture.setFailure(e);
    } finally {
        Thread.currentThread().setContextClassLoader(cl);
        RpcInvokeContext.removeContext();
        RpcInternalContext.removeAllContext();
    }
}
 
Example 3
Source File: SofaAsyncHystrixCommand.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
@Override
protected Object run() throws Exception {
    events.add(SofaAsyncHystrixEvent.EMIT);
    RpcInternalContext.setContext(rpcInternalContext);
    RpcInvokeContext.setContext(rpcInvokeContext);

    this.sofaResponse = invoker.invoke(request);
    ResponseFuture responseFuture = RpcInternalContext.getContext().getFuture();
    lock.countDown();
    events.add(SofaAsyncHystrixEvent.INVOKE_UNLOCKED);
    try {
        return responseFuture.get();
    } finally {
        events.add(SofaAsyncHystrixEvent.INVOKE_SUCCESS);
    }
}
 
Example 4
Source File: CallbackInvokeClientHandler.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@Override
public void doOnException(Throwable e) {
    if (callback == null) {
        return;
    }
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(this.classLoader);
        RpcInternalContext.setContext(context);

        if (EventBus.isEnable(ClientAsyncReceiveEvent.class)) {
            EventBus.post(new ClientAsyncReceiveEvent(consumerConfig, providerInfo,
                request, null, e));
        }

        // do async filter after respond server
        FilterChain chain = consumerConfig.getConsumerBootstrap().getCluster().getFilterChain();
        if (chain != null) {
            chain.onAsyncResponse(consumerConfig, request, null, e);
        }

        recordClientElapseTime();
        if (EventBus.isEnable(ClientEndInvokeEvent.class)) {
            EventBus.post(new ClientEndInvokeEvent(request, null, e));
        }

        SofaRpcException sofaRpcException = e instanceof SofaRpcException ? (SofaRpcException) e :
            new SofaRpcException(RpcErrorType.SERVER_UNDECLARED_ERROR, e.getMessage(), e);
        callback.onSofaException(sofaRpcException, request.getMethodName(), request);
    } finally {
        Thread.currentThread().setContextClassLoader(cl);
        RpcInvokeContext.removeContext();
        RpcInternalContext.removeAllContext();
    }
}
 
Example 5
Source File: FutureInvokeClientHandler.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@Override
public void doOnResponse(Object result) {
    if (rpcFuture == null) {
        return;
    }
    ClassLoader oldCl = Thread.currentThread().getContextClassLoader();
    SofaResponse response = (SofaResponse) result;
    try {
        Thread.currentThread().setContextClassLoader(this.classLoader);
        RpcInternalContext.setContext(context);

        if (EventBus.isEnable(ClientAsyncReceiveEvent.class)) {
            EventBus.post(new ClientAsyncReceiveEvent(consumerConfig, providerInfo,
                request, response, null));
        }

        pickupBaggage(response);

        // do async filter after respond server
        FilterChain chain = consumerConfig.getConsumerBootstrap().getCluster().getFilterChain();
        if (chain != null) {
            chain.onAsyncResponse(consumerConfig, request, response, null);
        }

        recordClientElapseTime();
        if (EventBus.isEnable(ClientEndInvokeEvent.class)) {
            EventBus.post(new ClientEndInvokeEvent(request, response, null));
        }
        decode(response);
        rpcFuture.setSuccess(response);
    } finally {
        Thread.currentThread().setContextClassLoader(oldCl);
        RpcInvokeContext.removeContext();
        RpcInternalContext.removeAllContext();
    }
}
 
Example 6
Source File: FutureInvokeClientHandler.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@Override
public void doOnException(Throwable e) {
    if (rpcFuture == null) {
        return;
    }
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(this.classLoader);
        RpcInternalContext.setContext(context);

        if (EventBus.isEnable(ClientAsyncReceiveEvent.class)) {
            EventBus.post(new ClientAsyncReceiveEvent(consumerConfig, providerInfo,
                request, null, e));
        }

        // do async filter after respond server
        FilterChain chain = consumerConfig.getConsumerBootstrap().getCluster().getFilterChain();
        if (chain != null) {
            chain.onAsyncResponse(consumerConfig, request, null, e);
        }

        recordClientElapseTime();
        if (EventBus.isEnable(ClientEndInvokeEvent.class)) {
            EventBus.post(new ClientEndInvokeEvent(request, null, e));
        }

        rpcFuture.setFailure(e);
    } finally {
        Thread.currentThread().setContextClassLoader(cl);
        RpcInvokeContext.removeContext();
        RpcInternalContext.removeAllContext();
    }
}
 
Example 7
Source File: BoltFutureInvokeCallback.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@Override
public void onException(Throwable e) {
    if (rpcFuture == null) {
        return;
    }
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(this.classLoader);
        RpcInternalContext.setContext(context);

        if (EventBus.isEnable(ClientAsyncReceiveEvent.class)) {
            EventBus.post(new ClientAsyncReceiveEvent(consumerConfig, providerInfo,
                request, null, e));
        }

        // do async filter after respond server
        FilterChain chain = consumerConfig.getConsumerBootstrap().getCluster().getFilterChain();
        if (chain != null) {
            chain.onAsyncResponse(consumerConfig, request, null, e);
        }

        recordClientElapseTime();
        if (EventBus.isEnable(ClientEndInvokeEvent.class)) {
            EventBus.post(new ClientEndInvokeEvent(request, null, e));
        }

        rpcFuture.setFailure(e);
    } finally {
        Thread.currentThread().setContextClassLoader(cl);
        RpcInvokeContext.removeContext();
        RpcInternalContext.removeAllContext();
    }
}
 
Example 8
Source File: SofaHystrixCommand.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@Override
protected SofaResponse run() throws Exception {
    RpcInternalContext.setContext(rpcInternalContext);
    RpcInvokeContext.setContext(rpcInvokeContext);

    SofaResponse sofaResponse = invoker.invoke(request);
    if (!sofaResponse.isError()) {
        return sofaResponse;
    }
    return getFallback(sofaResponse, null);
}
 
Example 9
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 10
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 11
Source File: CallbackInvokeClientHandler.java    From sofa-rpc with Apache License 2.0 4 votes vote down vote up
@Override
public void doOnResponse(Object result) {
    if (callback == null) {
        return;
    }
    ClassLoader oldCl = Thread.currentThread().getContextClassLoader();
    SofaResponse response = (SofaResponse) result;
    Throwable throwable = null;
    try {
        Thread.currentThread().setContextClassLoader(this.classLoader);
        RpcInternalContext.setContext(context);

        if (EventBus.isEnable(ClientAsyncReceiveEvent.class)) {
            EventBus.post(new ClientAsyncReceiveEvent(consumerConfig, providerInfo,
                request, response, null));
        }

        pickupBaggage(response);

        // do async filter after respond server
        FilterChain chain = consumerConfig.getConsumerBootstrap().getCluster().getFilterChain();
        if (chain != null) {
            chain.onAsyncResponse(consumerConfig, request, response, null);
        }

        recordClientElapseTime();
        if (EventBus.isEnable(ClientEndInvokeEvent.class)) {
            EventBus.post(new ClientEndInvokeEvent(request, response, null));
        }

        decode(response);

        Object appResp = response.getAppResponse();
        if (response.isError()) { // rpc层异常
            SofaRpcException sofaRpcException = new SofaRpcException(
                RpcErrorType.SERVER_UNDECLARED_ERROR, response.getErrorMsg());
            callback.onSofaException(sofaRpcException, request.getMethodName(), request);
        } else if (appResp instanceof Throwable) { // 业务层异常
            throwable = (Throwable) appResp;
            callback.onAppException(throwable, request.getMethodName(), request);
        } else {
            callback.onAppResponse(appResp, request.getMethodName(), request);
        }
    } finally {
        Thread.currentThread().setContextClassLoader(oldCl);
        RpcInvokeContext.removeContext();
        RpcInternalContext.removeAllContext();
    }
}
 
Example 12
Source File: BoltFutureInvokeCallback.java    From sofa-rpc with Apache License 2.0 4 votes vote down vote up
@Override
public void onResponse(Object result) {
    if (rpcFuture == null) {
        return;
    }
    ClassLoader oldCl = Thread.currentThread().getContextClassLoader();
    SofaResponse response = (SofaResponse) result;
    Throwable throwable = null;
    try {
        Thread.currentThread().setContextClassLoader(this.classLoader);
        RpcInternalContext.setContext(context);

        if (EventBus.isEnable(ClientAsyncReceiveEvent.class)) {
            EventBus.post(new ClientAsyncReceiveEvent(consumerConfig, providerInfo,
                request, response, null));
        }

        pickupBaggage(response);

        // do async filter after respond server
        FilterChain chain = consumerConfig.getConsumerBootstrap().getCluster().getFilterChain();
        if (chain != null) {
            chain.onAsyncResponse(consumerConfig, request, response, null);
        }

        recordClientElapseTime();
        if (EventBus.isEnable(ClientEndInvokeEvent.class)) {
            EventBus.post(new ClientEndInvokeEvent(request, response, null));
        }

        Object appResp = response.getAppResponse();
        if (response.isError()) { // rpc层异常
            SofaRpcException sofaRpcException = new SofaRpcException(
                RpcErrorType.SERVER_UNDECLARED_ERROR, response.getErrorMsg());
            rpcFuture.setFailure(sofaRpcException);
        } else if (appResp instanceof Throwable) { // 业务层异常
            throwable = (Throwable) appResp;
            rpcFuture.setFailure(throwable);
        } else {
            rpcFuture.setSuccess(appResp);
        }
    } finally {
        Thread.currentThread().setContextClassLoader(oldCl);
        RpcInvokeContext.removeContext();
        RpcInternalContext.removeAllContext();
    }
}
 
Example 13
Source File: BoltInvokerCallback.java    From sofa-rpc with Apache License 2.0 4 votes vote down vote up
@Override
public void onResponse(Object result) {
    if (callback == null) {
        return;
    }
    ClassLoader oldCl = Thread.currentThread().getContextClassLoader();
    SofaResponse response = (SofaResponse) result;
    Throwable throwable = null;
    try {
        Thread.currentThread().setContextClassLoader(this.classLoader);
        RpcInternalContext.setContext(context);

        if (EventBus.isEnable(ClientAsyncReceiveEvent.class)) {
            EventBus.post(new ClientAsyncReceiveEvent(consumerConfig, providerInfo,
                request, response, null));
        }

        pickupBaggage(response);

        // do async filter after respond server
        FilterChain chain = consumerConfig.getConsumerBootstrap().getCluster().getFilterChain();
        if (chain != null) {
            chain.onAsyncResponse(consumerConfig, request, response, null);
        }

        recordClientElapseTime();
        if (EventBus.isEnable(ClientEndInvokeEvent.class)) {
            EventBus.post(new ClientEndInvokeEvent(request, response, null));
        }

        Object appResp = response.getAppResponse();
        if (response.isError()) { // rpc层异常
            SofaRpcException sofaRpcException = new SofaRpcException(
                RpcErrorType.SERVER_UNDECLARED_ERROR, response.getErrorMsg());
            callback.onSofaException(sofaRpcException, request.getMethodName(), request);
        } else if (appResp instanceof Throwable) { // 业务层异常
            throwable = (Throwable) appResp;
            callback.onAppException(throwable, request.getMethodName(), request);
        } else {
            callback.onAppResponse(appResp, request.getMethodName(), request);
        }
    } finally {
        Thread.currentThread().setContextClassLoader(oldCl);
        RpcInvokeContext.removeContext();
        RpcInternalContext.removeAllContext();
    }
}