Search in sources :

Example 11 with SofaRpcException

use of com.alipay.sofa.rpc.core.exception.SofaRpcException in project sofa-rpc by sofastack.

the class BCallbackSampleServiceImpl method echoObj.

@Override
public EchoResponse echoObj(EchoRequest req) {
    RpcInvokeContext context = RpcInvokeContext.getContext();
    LOGGER.info("--b1-----:" + context);
    reqBaggage = context.getRequestBaggage("reqBaggageB");
    if (reqBaggage != null) {
        context.putResponseBaggage("respBaggageB", "b2aaa");
    } else {
        context.putResponseBaggage("respBaggageB_force", "b2aaaff");
    }
    final EchoResponse[] str = new EchoResponse[2];
    final CountDownLatch latch = new CountDownLatch(2);
    try {
        RpcInvokeContext.getContext().setResponseCallback(new SofaResponseCallback() {

            @Override
            public void onAppResponse(Object appResponse, String methodName, RequestBase request) {
                str[0] = (EchoResponse) appResponse;
                latch.countDown();
            }

            @Override
            public void onAppException(Throwable throwable, String methodName, RequestBase request) {
                latch.countDown();
            }

            @Override
            public void onSofaException(SofaRpcException sofaException, String methodName, RequestBase request) {
                latch.countDown();
            }
        });
        sampleServiceC.hello();
        RpcInvokeContext.getContext().setResponseCallback(new SofaResponseCallback() {

            @Override
            public void onAppResponse(Object appResponse, String methodName, RequestBase request) {
                str[1] = (EchoResponse) appResponse;
                latch.countDown();
            }

            @Override
            public void onAppException(Throwable throwable, String methodName, RequestBase request) {
                latch.countDown();
            }

            @Override
            public void onSofaException(SofaRpcException sofaException, String methodName, RequestBase request) {
                latch.countDown();
            }
        });
        sampleServiceD.hello();
        latch.await(2000, TimeUnit.MILLISECONDS);
    } catch (Exception e) {
        e.printStackTrace();
    }
    EchoResponse s1 = str[0];
    EchoResponse s2 = str[1];
    return EchoResponse.newBuilder().setCode(200).setMessage(s1.getMessage() + s2.getMessage()).build();
}
Also used : EchoResponse(com.alipay.sofa.rpc.server.bolt.pb.EchoResponse) RpcInvokeContext(com.alipay.sofa.rpc.context.RpcInvokeContext) SofaResponseCallback(com.alipay.sofa.rpc.core.invoke.SofaResponseCallback) CountDownLatch(java.util.concurrent.CountDownLatch) RequestBase(com.alipay.sofa.rpc.core.request.RequestBase) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException)

Example 12 with SofaRpcException

use of com.alipay.sofa.rpc.core.exception.SofaRpcException in project sofa-rpc by sofastack.

the class BaggageAsyncChainTest method doTest.

@Override
public void doTest() throws Exception {
    ServerConfig serverConfig = new ServerConfig().setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT).setPort(12299);
    // C服务的服务端
    // C服务的服务端
    CSampleServiceImpl refC = new CSampleServiceImpl();
    ProviderConfig<SampleService> serviceBeanC = new ProviderConfig<SampleService>();
    serviceBeanC.setInterfaceId(SampleService.class.getName());
    serviceBeanC.setApplication(new ApplicationConfig().setAppName("CCC"));
    serviceBeanC.setUniqueId("C5");
    serviceBeanC.setRef(refC);
    serviceBeanC.setServer(serverConfig);
    serviceBeanC.setRegister(false);
    serviceBeanC.export();
    // D服务的服务端
    DSampleServiceImpl refD = new DSampleServiceImpl();
    ProviderConfig<SampleService> serviceBeanD = new ProviderConfig<SampleService>();
    serviceBeanD.setInterfaceId(SampleService.class.getName());
    serviceBeanD.setApplication(new ApplicationConfig().setAppName("DDD"));
    serviceBeanD.setUniqueId("D5");
    serviceBeanD.setRef(refD);
    serviceBeanD.setServer(serverConfig);
    serviceBeanD.setRegister(false);
    serviceBeanD.export();
    // B服务里的C服务客户端
    ConsumerConfig referenceBeanC = new ConsumerConfig();
    referenceBeanC.setApplication(new ApplicationConfig().setAppName("BBB"));
    referenceBeanC.setInterfaceId(SampleService.class.getName());
    referenceBeanC.setUniqueId("C5");
    referenceBeanC.setDirectUrl("localhost:12299");
    referenceBeanC.setTimeout(1000);
    MethodConfig methodConfigC = new MethodConfig().setName("hello").setInvokeType(RpcConstants.INVOKER_TYPE_CALLBACK);
    referenceBeanC.setMethods(Collections.singletonList(methodConfigC));
    SampleService sampleServiceC = (SampleService) referenceBeanC.refer();
    // B服务里的D服务客户端
    ConsumerConfig referenceBeanD = new ConsumerConfig();
    referenceBeanD.setApplication(new ApplicationConfig().setAppName("BBB"));
    referenceBeanD.setInterfaceId(SampleService.class.getName());
    referenceBeanD.setUniqueId("D5");
    referenceBeanD.setDirectUrl("localhost:12299?p=1&v=4.0");
    referenceBeanD.setTimeout(1000);
    SampleService sampleServiceD = (SampleService) referenceBeanD.refer();
    // B服务的服务端
    BAsyncChainSampleServiceImpl refB = new BAsyncChainSampleServiceImpl(sampleServiceC, sampleServiceD);
    ProviderConfig<SampleService> ServiceBeanB = new ProviderConfig<SampleService>();
    ServiceBeanB.setInterfaceId(SampleService.class.getName());
    ServiceBeanB.setApplication(new ApplicationConfig().setAppName("BBB"));
    ServiceBeanB.setUniqueId("B5");
    ServiceBeanB.setRef(refB);
    ServiceBeanB.setServer(serverConfig);
    ServiceBeanB.setRegister(false);
    ServiceBeanB.export();
    // A 服务
    final String[] str = new String[1];
    final CountDownLatch[] latch = new CountDownLatch[] { new CountDownLatch(1) };
    final RpcInvokeContext[] contexts = new RpcInvokeContext[1];
    ConsumerConfig referenceBeanA = new ConsumerConfig();
    referenceBeanA.setApplication(new ApplicationConfig().setAppName("AAA"));
    referenceBeanA.setUniqueId("B5");
    referenceBeanA.setInterfaceId(SampleService.class.getName());
    referenceBeanA.setDirectUrl("localhost:12299");
    referenceBeanA.setTimeout(3000);
    MethodConfig methodConfigA = new MethodConfig().setName("hello").setInvokeType(RpcConstants.INVOKER_TYPE_CALLBACK);
    final Exception[] exp = new Exception[1];
    methodConfigA.setOnReturn(new SofaResponseCallback() {

        @Override
        public void onAppResponse(Object appResponse, String methodName, RequestBase request) {
            try {
                // 主线程和子线程中的 Context 必须不是同一个,否则有并发问题
                Assert.assertNotSame(RpcInvokeContext.getContext(), contexts[0]);
                str[0] = (String) appResponse;
                latch[0].countDown();
            } catch (Exception e) {
                exp[0] = e;
            }
        }

        @Override
        public void onAppException(Throwable t, String methodName, RequestBase request) {
            Assert.assertEquals("sampleService", t.getMessage());
            Assert.assertEquals("sayException", methodName);
        }

        @Override
        public void onSofaException(SofaRpcException sofaException, String methodName, RequestBase request) {
            // never go to this
            Assert.assertEquals("sampleService", sofaException.getMessage());
            Assert.assertEquals("sayException", methodName);
        }
    });
    referenceBeanA.setMethods(Collections.singletonList(methodConfigA));
    SampleService service = (SampleService) referenceBeanA.refer();
    // 开始测试
    RpcInvokeContext context = RpcInvokeContext.getContext();
    contexts[0] = context;
    context.putRequestBaggage("reqBaggageB", "a2bbb");
    context.putRequestBaggage("reqBaggageC", "a2ccc");
    context.putRequestBaggage("reqBaggageD", "a2ddd");
    LOGGER.info("-----A1-----" + RpcInvokeContext.getContext());
    // 测试传递数据
    String ret = service.hello();
    Assert.assertEquals(ret, null);
    latch[0].await(5000, TimeUnit.MILLISECONDS);
    if (exp[0] != null) {
        throw exp[0];
    }
    ret = str[0];
    Assert.assertEquals(ret, "hello world chello world d");
    Assert.assertEquals(refB.getReqBaggage(), "a2bbb");
    Assert.assertEquals(refC.getReqBaggage(), "a2ccc");
    Assert.assertEquals(refD.getReqBaggage(), "a2ddd");
    Assert.assertNotNull(context.getResponseBaggage("respBaggageB_useful1"));
    Assert.assertNotNull(context.getResponseBaggage("respBaggageB_useful2"));
    Assert.assertNull(context.getResponseBaggage("respBaggageB_useless1"));
    Assert.assertNull(context.getResponseBaggage("respBaggageB_useless2"));
    // 支持
    Assert.assertEquals(context.getResponseBaggage("respBaggageB"), "b2aaa");
    // 支持
    Assert.assertEquals(context.getResponseBaggage("respBaggageC"), "c2aaa");
    // 支持
    Assert.assertEquals(context.getResponseBaggage("respBaggageD"), "d2aaa");
    Assert.assertNull(context.getResponseBaggage("respBaggageB_force"));
    Assert.assertNull(context.getResponseBaggage("respBaggageC_force"));
    Assert.assertNull(context.getResponseBaggage("respBaggageD_force"));
    RpcInvokeContext.removeContext();
    context = RpcInvokeContext.getContext();
    contexts[0] = context;
    latch[0] = new CountDownLatch(1);
    str[0] = null;
    ret = null;
    LOGGER.info("-----A3-----" + RpcInvokeContext.getContext());
    // 测试不传数据
    ret = service.hello();
    Assert.assertEquals(ret, null);
    latch[0].await(5000, TimeUnit.MILLISECONDS);
    ret = str[0];
    Assert.assertEquals(ret, "hello world chello world d");
    Assert.assertNull(refB.getReqBaggage());
    Assert.assertNull(refC.getReqBaggage());
    Assert.assertNull(refD.getReqBaggage());
    Assert.assertNotNull(context.getResponseBaggage("respBaggageB_useful1"));
    Assert.assertNotNull(context.getResponseBaggage("respBaggageB_useful2"));
    Assert.assertNull(context.getResponseBaggage("respBaggageB_useless1"));
    Assert.assertNull(context.getResponseBaggage("respBaggageB_useless2"));
    Assert.assertNull(context.getResponseBaggage("respBaggageB"));
    Assert.assertNull(context.getResponseBaggage("respBaggageC"));
    Assert.assertNull(context.getResponseBaggage("respBaggageD"));
    Assert.assertEquals(context.getResponseBaggage("respBaggageB_force"), "b2aaaff");
    Assert.assertEquals(context.getResponseBaggage("respBaggageC_force"), "c2aaaff");
    Assert.assertEquals(context.getResponseBaggage("respBaggageD_force"), "d2aaaff");
}
Also used : RpcInvokeContext(com.alipay.sofa.rpc.context.RpcInvokeContext) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) SofaResponseCallback(com.alipay.sofa.rpc.core.invoke.SofaResponseCallback) CountDownLatch(java.util.concurrent.CountDownLatch) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) RequestBase(com.alipay.sofa.rpc.core.request.RequestBase) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) MethodConfig(com.alipay.sofa.rpc.config.MethodConfig) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig)

Example 13 with SofaRpcException

use of com.alipay.sofa.rpc.core.exception.SofaRpcException in project sofa-rpc by sofastack.

the class BizThrowExceptionTest method testAll.

@Test
public void testAll() {
    // 只有2个线程 执行
    ServerConfig serverConfig = new ServerConfig().setStopTimeout(0).setPort(22222).setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT).setQueues(100).setCoreThreads(5).setMaxThreads(5);
    // 发布一个服务,每个请求要执行1秒
    ProviderConfig<TestExceptionService> providerConfig = new ProviderConfig<TestExceptionService>().setInterfaceId(TestExceptionService.class.getName()).setRef(new TestExceptionServiceImpl()).setServer(serverConfig).setRegister(false);
    providerConfig.export();
    for (String proxy : new String[] { "jdk", "javassist" }) {
        ConsumerConfig<TestExceptionService> consumerConfig = new ConsumerConfig<TestExceptionService>().setInterfaceId(TestExceptionService.class.getName()).setDirectUrl("bolt://127.0.0.1:22222").setTimeout(1000).setProxy(proxy).setRepeatedReferLimit(-1).setRegister(false);
        final TestExceptionService service = consumerConfig.refer();
        try {
            service.throwRuntimeException();
            Assert.fail();
        } catch (Throwable e) {
            Assert.assertTrue(e instanceof RuntimeException);
            Assert.assertEquals(e.getMessage(), "RuntimeException");
        }
        try {
            service.throwException();
            Assert.fail();
        } catch (Throwable e) {
            Assert.assertTrue(e instanceof Exception);
            Assert.assertEquals(e.getMessage(), "Exception");
        }
        try {
            service.throwSofaException();
            Assert.fail();
        } catch (Throwable e) {
            Assert.assertTrue(e instanceof SofaRpcException);
            Assert.assertEquals(e.getMessage(), "SofaRpcException");
        }
        try {
            service.throwDeclaredException();
            Assert.fail();
        } catch (Throwable e) {
            Assert.assertTrue(e instanceof TestException);
            Assert.assertEquals(e.getMessage(), "TestException");
        }
        try {
            service.throwDeclaredExceptionWithoutReturn();
            Assert.fail();
        } catch (Throwable e) {
            Assert.assertTrue(e instanceof TestException);
            Assert.assertEquals(e.getMessage(), "DeclaredExceptionWithoutReturn");
        }
    }
}
Also used : ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest) Test(org.junit.Test)

Example 14 with SofaRpcException

use of com.alipay.sofa.rpc.core.exception.SofaRpcException in project sofa-rpc by sofastack.

the class Http2ClearTextPriorKnowledgeTest method testProtobuf.

@Test
public void testProtobuf() {
    // 只有1个线程 执行
    ServerConfig serverConfig = new ServerConfig().setStopTimeout(60000).setPort(12300).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C).setDaemon(true);
    // 发布一个服务,每个请求要执行1秒
    ProviderConfig<HttpService> providerConfig = new ProviderConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setRef(new HttpServiceImpl()).setApplication(new ApplicationConfig().setAppName("serverApp")).setServer(serverConfig).setRegister(false);
    providerConfig.export();
    {
        ConsumerConfig<HttpService> consumerConfig = new ConsumerConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setSerialization(RpcConstants.SERIALIZE_PROTOBUF).setDirectUrl("h2c://127.0.0.1:12300").setApplication(new ApplicationConfig().setAppName("clientApp")).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C);
        HttpService httpService = consumerConfig.refer();
        EchoRequest request = EchoRequest.newBuilder().setGroup(Group.A).setName("xxx").build();
        EchoResponse response = httpService.echoPb(request);
        Assert.assertEquals("helloxxx", response.getMessage());
        Assert.assertEquals(200, response.getCode());
    }
    {
        ConsumerConfig<HttpService> consumerConfig2 = new ConsumerConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setSerialization(RpcConstants.SERIALIZE_PROTOBUF).setDirectUrl("h2c://127.0.0.1:12300").setApplication(new ApplicationConfig().setAppName("clientApp")).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C).setInvokeType(RpcConstants.INVOKER_TYPE_ONEWAY).setRepeatedReferLimit(-1);
        HttpService httpService2 = consumerConfig2.refer();
        EchoRequest request = EchoRequest.newBuilder().setGroup(Group.A).setName("xxx").build();
        try {
            httpService2.echoPb(request);
            // NOT SUPPORTED NOW, If want support this, need add key to head.
            Assert.fail();
        } catch (Exception e) {
        }
    }
    {
        ConsumerConfig<HttpService> consumerConfig3 = new ConsumerConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setSerialization(RpcConstants.SERIALIZE_PROTOBUF).setDirectUrl("h2c://127.0.0.1:12300").setApplication(new ApplicationConfig().setAppName("clientApp")).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C).setInvokeType(RpcConstants.INVOKER_TYPE_FUTURE).setRepeatedReferLimit(-1);
        HttpService httpService3 = consumerConfig3.refer();
        EchoRequest request = EchoRequest.newBuilder().setGroup(Group.A).setName("xxx").build();
        EchoResponse response = httpService3.echoPb(request);
        Assert.assertNull(response);
        ResponseFuture future = RpcInvokeContext.getContext().getFuture();
        try {
            response = (EchoResponse) future.get();
            Assert.assertEquals("helloxxx", response.getMessage());
            Assert.assertEquals(200, response.getCode());
        } catch (Exception e) {
            e.printStackTrace();
            Assert.fail();
        }
    }
    {
        final EchoResponse[] result = new EchoResponse[1];
        final CountDownLatch latch = new CountDownLatch(1);
        ConsumerConfig<HttpService> consumerConfig4 = new ConsumerConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setSerialization(RpcConstants.SERIALIZE_PROTOBUF).setDirectUrl("h2c://127.0.0.1:12300").setApplication(new ApplicationConfig().setAppName("clientApp")).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C).setInvokeType(RpcConstants.INVOKER_TYPE_CALLBACK).setOnReturn(new SofaResponseCallback() {

            @Override
            public void onAppResponse(Object appResponse, String methodName, RequestBase request) {
                result[0] = (EchoResponse) appResponse;
                latch.countDown();
            }

            @Override
            public void onAppException(Throwable throwable, String methodName, RequestBase request) {
                latch.countDown();
            }

            @Override
            public void onSofaException(SofaRpcException sofaException, String methodName, RequestBase request) {
                latch.countDown();
            }
        }).setRepeatedReferLimit(-1);
        HttpService httpService4 = consumerConfig4.refer();
        EchoRequest request = EchoRequest.newBuilder().setGroup(Group.A).setName("xxx").build();
        EchoResponse response = httpService4.echoPb(request);
        Assert.assertNull(response);
        try {
            latch.await(2000, TimeUnit.MILLISECONDS);
            response = result[0];
            Assert.assertNotNull(response);
            Assert.assertEquals("helloxxx", response.getMessage());
            Assert.assertEquals(200, response.getCode());
        } catch (Exception e) {
            e.printStackTrace();
            Assert.fail();
        }
    }
}
Also used : EchoResponse(com.alipay.sofa.rpc.server.bolt.pb.EchoResponse) EchoRequest(com.alipay.sofa.rpc.server.bolt.pb.EchoRequest) SofaResponseCallback(com.alipay.sofa.rpc.core.invoke.SofaResponseCallback) ResponseFuture(com.alipay.sofa.rpc.message.ResponseFuture) CountDownLatch(java.util.concurrent.CountDownLatch) HttpServiceImpl(com.alipay.sofa.rpc.server.http.HttpServiceImpl) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) RequestBase(com.alipay.sofa.rpc.core.request.RequestBase) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) HttpService(com.alipay.sofa.rpc.server.http.HttpService) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) Test(org.junit.Test) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest)

Example 15 with SofaRpcException

use of com.alipay.sofa.rpc.core.exception.SofaRpcException in project sofa-rpc by sofastack.

the class AbstractProxyClientTransport method convertToRpcException.

/**
 * 转换调用出现的异常为RPC异常
 *
 * @param e 代理类包装的原因
 * @return RPC异常
 */
protected SofaRpcException convertToRpcException(InvocationTargetException e) {
    SofaRpcException exception;
    // 包装的原因
    Throwable ie = e.getCause();
    if (ie != null) {
        // 真正的原因
        Throwable realException = ie.getCause();
        if (realException != null) {
            if (realException instanceof SocketTimeoutException) {
                exception = new SofaRpcException(RpcErrorType.CLIENT_TIMEOUT, "Client read timeout!", realException);
            } else if (realException instanceof ConnectException) {
                open = false;
                exception = new SofaRpcException(RpcErrorType.CLIENT_NETWORK, "Connect to remote " + transportConfig.getProviderInfo() + " error!", realException);
            } else {
                exception = new SofaRpcException(RpcErrorType.CLIENT_UNDECLARED_ERROR, "Send message to remote catch error: " + realException.getMessage(), realException);
            }
        } else {
            exception = new SofaRpcException(RpcErrorType.CLIENT_UNDECLARED_ERROR, "Send message to remote catch error: " + ie.getMessage(), ie);
        }
    } else {
        exception = new SofaRpcException(RpcErrorType.CLIENT_UNDECLARED_ERROR, "Send message to remote catch error: " + e.getMessage(), e);
    }
    return exception;
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) ConnectException(java.net.ConnectException)

Aggregations

SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)91 Test (org.junit.Test)35 RequestBase (com.alipay.sofa.rpc.core.request.RequestBase)28 SofaResponseCallback (com.alipay.sofa.rpc.core.invoke.SofaResponseCallback)27 ServerConfig (com.alipay.sofa.rpc.config.ServerConfig)24 ActivelyDestroyTest (com.alipay.sofa.rpc.test.ActivelyDestroyTest)23 SofaTimeOutException (com.alipay.sofa.rpc.core.exception.SofaTimeOutException)22 ConsumerConfig (com.alipay.sofa.rpc.config.ConsumerConfig)21 CountDownLatch (java.util.concurrent.CountDownLatch)20 SofaResponse (com.alipay.sofa.rpc.core.response.SofaResponse)19 ProviderConfig (com.alipay.sofa.rpc.config.ProviderConfig)16 HelloService (com.alipay.sofa.rpc.test.HelloService)16 RpcInvokeContext (com.alipay.sofa.rpc.context.RpcInvokeContext)15 ApplicationConfig (com.alipay.sofa.rpc.config.ApplicationConfig)14 RpcInternalContext (com.alipay.sofa.rpc.context.RpcInternalContext)14 SofaRequest (com.alipay.sofa.rpc.core.request.SofaRequest)11 HelloServiceImpl (com.alipay.sofa.rpc.test.HelloServiceImpl)11 HashMap (java.util.HashMap)9 Filter (com.alipay.sofa.rpc.filter.Filter)8 InvokeTimeoutException (com.alipay.remoting.rpc.exception.InvokeTimeoutException)7