Search in sources :

Example 6 with SofaResponseCallback

use of com.alipay.sofa.rpc.core.invoke.SofaResponseCallback in project sofa-rpc by sofastack.

the class AsyncCallbackTest method testCallbackCallerRuns.

@Test
public void testCallbackCallerRuns() {
    serverConfig = new ServerConfig().setPort(22224).setDaemon(false);
    // RpcServer for C
    CProvider = new ProviderConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setRef(new HelloServiceImpl(0)).setServer(serverConfig);
    CProvider.export();
    // RpcClient For B invoke C
    Filter filter = new TestAsyncFilter();
    BConsumer = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setInvokeType(RpcConstants.INVOKER_TYPE_CALLBACK).setTimeout(3000).setFilterRef(Arrays.asList(filter)).setRejectedExecutionPolicy(RejectedExecutionPolicy.CALLER_RUNS.name()).setDirectUrl("bolt://127.0.0.1:22224");
    HelloService helloService = BConsumer.refer();
    int maxsize = RpcConfigs.getIntValue(RpcOptions.ASYNC_POOL_MAX);
    int queuesize = RpcConfigs.getIntValue(RpcOptions.ASYNC_POOL_QUEUE);
    int invokeCount = (maxsize + queuesize) * 2;
    final CountDownLatch latch = new CountDownLatch(invokeCount);
    AtomicInteger runCount = new AtomicInteger(0);
    SofaResponseCallback callback = new SofaResponseCallback() {

        @Override
        public void onAppResponse(Object appResponse, String methodName, RequestBase request) {
            LOGGER.info("B get result: {}", appResponse);
            try {
                Thread.sleep(100);
            } catch (Exception e) {
            }
            runCount.addAndGet(1);
            latch.countDown();
        }

        @Override
        public void onAppException(Throwable throwable, String methodName, RequestBase request) {
            LOGGER.info("B get app exception: {}", throwable);
            latch.countDown();
        }

        @Override
        public void onSofaException(SofaRpcException sofaException, String methodName, RequestBase request) {
            LOGGER.info("B get sofa exception: {}", sofaException);
            latch.countDown();
        }
    };
    for (int i = 0; i < invokeCount; i++) {
        RpcInvokeContext.getContext().setResponseCallback(callback);
        helloService.sayHello("" + i, 33);
    }
    try {
        latch.await(100L * invokeCount, TimeUnit.MILLISECONDS);
    } catch (InterruptedException ignore) {
    }
    assertEquals(0, latch.getCount());
    // Invoke callbacks are triggered by IO threads after the thread pool is full, so the total number of normal responses received is equal to the total number of calls
    assertEquals(invokeCount, runCount.get());
    RpcInvokeContext.removeContext();
}
Also used : ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) HelloService(com.alipay.sofa.rpc.test.HelloService) SofaResponseCallback(com.alipay.sofa.rpc.core.invoke.SofaResponseCallback) HelloServiceImpl(com.alipay.sofa.rpc.test.HelloServiceImpl) CountDownLatch(java.util.concurrent.CountDownLatch) RequestBase(com.alipay.sofa.rpc.core.request.RequestBase) SofaTimeOutException(com.alipay.sofa.rpc.core.exception.SofaTimeOutException) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) Filter(com.alipay.sofa.rpc.filter.Filter) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest)

Example 7 with SofaResponseCallback

use of com.alipay.sofa.rpc.core.invoke.SofaResponseCallback in project sofa-rpc by sofastack.

the class RpcContextTest method testAll.

@Test
public void testAll() {
    // 只有1个线程 执行
    ServerConfig serverConfig = new ServerConfig().setPort(22222).setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT).setQueues(100).setCoreThreads(1).setMaxThreads(2);
    // 发布一个服务,每个请求要执行1秒
    CtxHelloServiceImpl helloServiceImpl = new CtxHelloServiceImpl();
    ProviderConfig<HelloService> providerConfig = new ProviderConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setApplication(new ApplicationConfig().setAppName("test-server")).setRef(helloServiceImpl).setServer(serverConfig).setRegister(false);
    providerConfig.export();
    {
        ConsumerConfig<HelloService> consumerConfig = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setApplication(new ApplicationConfig().setAppName("test-client")).setDirectUrl("bolt://127.0.0.1:22222?appName=test-server").setTimeout(30000).setRegister(false);
        final HelloService helloService = consumerConfig.refer();
        String str = helloService.sayHello("xxx", 123);
        RpcServiceContext serviceContext = RpcContextManager.currentServiceContext(false);
        RpcReferenceContext referenceContext = RpcContextManager.lastReferenceContext(false);
        Assert.assertNull(serviceContext);
        Assert.assertNotNull(referenceContext);
        serviceContext = helloServiceImpl.serviceContext;
        Assert.assertNotNull(serviceContext);
        Assert.assertEquals(serviceContext.getCallerAppName(), "test-client");
        Assert.assertEquals(referenceContext.getTargetAppName(), "test-server");
        Assert.assertNotNull(referenceContext.getClientIP());
        Assert.assertTrue(referenceContext.getClientPort() > 0);
    }
    {
        final CountDownLatch latch = new CountDownLatch(1);
        final String[] ret = { null };
        ConsumerConfig<HelloService> consumerConfig2 = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setApplication(new ApplicationConfig().setAppName("test-client")).setDirectUrl("bolt://127.0.0.1:22222?appName=test-server").setTimeout(2000).setInvokeType("callback").setOnReturn(new SofaResponseCallback() {

            @Override
            public void onAppResponse(Object appResponse, String methodName, RequestBase request) {
                ret[0] = (String) 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();
            }
        }).setRegister(false);
        final HelloService helloServiceCallback = consumerConfig2.refer();
        String ret0 = helloServiceCallback.sayHello("xxx", 22);
        // 第一次返回null
        Assert.assertNull(ret0);
        RpcServiceContext serviceContext = RpcContextManager.currentServiceContext(false);
        RpcReferenceContext referenceContext = RpcContextManager.lastReferenceContext(false);
        Assert.assertNull(serviceContext);
        Assert.assertNotNull(referenceContext);
        serviceContext = helloServiceImpl.serviceContext;
        Assert.assertNotNull(serviceContext);
        Assert.assertEquals(serviceContext.getCallerAppName(), "test-client");
        Assert.assertEquals(referenceContext.getTargetAppName(), "test-server");
        Assert.assertNotNull(referenceContext.getClientIP());
        Assert.assertTrue(referenceContext.getClientPort() > 0);
        try {
            latch.await(5000, TimeUnit.MILLISECONDS);
        } catch (InterruptedException ignore) {
        }
        Assert.assertNotNull(ret[0]);
    }
}
Also used : HelloService(com.alipay.sofa.rpc.test.HelloService) SofaResponseCallback(com.alipay.sofa.rpc.core.invoke.SofaResponseCallback) CountDownLatch(java.util.concurrent.CountDownLatch) RpcServiceContext(com.alipay.sofa.rpc.api.context.RpcServiceContext) 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) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) RpcReferenceContext(com.alipay.sofa.rpc.api.context.RpcReferenceContext) Test(org.junit.Test) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest)

Example 8 with SofaResponseCallback

use of com.alipay.sofa.rpc.core.invoke.SofaResponseCallback 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 9 with SofaResponseCallback

use of com.alipay.sofa.rpc.core.invoke.SofaResponseCallback 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 10 with SofaResponseCallback

use of com.alipay.sofa.rpc.core.invoke.SofaResponseCallback 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)

Aggregations

SofaResponseCallback (com.alipay.sofa.rpc.core.invoke.SofaResponseCallback)34 SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)27 RequestBase (com.alipay.sofa.rpc.core.request.RequestBase)25 CountDownLatch (java.util.concurrent.CountDownLatch)16 ConsumerConfig (com.alipay.sofa.rpc.config.ConsumerConfig)14 ServerConfig (com.alipay.sofa.rpc.config.ServerConfig)14 ApplicationConfig (com.alipay.sofa.rpc.config.ApplicationConfig)13 Test (org.junit.Test)13 ActivelyDestroyTest (com.alipay.sofa.rpc.test.ActivelyDestroyTest)12 HelloService (com.alipay.sofa.rpc.test.HelloService)10 Filter (com.alipay.sofa.rpc.filter.Filter)8 MethodConfig (com.alipay.sofa.rpc.config.MethodConfig)7 ProviderConfig (com.alipay.sofa.rpc.config.ProviderConfig)7 ResponseFuture (com.alipay.sofa.rpc.message.ResponseFuture)7 RpcInvokeContext (com.alipay.sofa.rpc.context.RpcInvokeContext)6 SofaTimeOutException (com.alipay.sofa.rpc.core.exception.SofaTimeOutException)6 SofaRequest (com.alipay.sofa.rpc.core.request.SofaRequest)5 EchoRequest (com.alipay.sofa.rpc.server.bolt.pb.EchoRequest)5 EchoResponse (com.alipay.sofa.rpc.server.bolt.pb.EchoResponse)5 HttpService (com.alipay.sofa.rpc.server.http.HttpService)5