Search in sources :

Example 46 with RpcException

use of com.alibaba.dubbo.rpc.RpcException in project dubbo by alibaba.

the class AbstractDirectory method list.

public List<Invoker<T>> list(Invocation invocation) throws RpcException {
    if (destroyed) {
        throw new RpcException("Directory already destroyed .url: " + getUrl());
    }
    List<Invoker<T>> invokers = doList(invocation);
    // local reference
    List<Router> localRouters = this.routers;
    if (localRouters != null && !localRouters.isEmpty()) {
        for (Router router : localRouters) {
            try {
                if (router.getUrl() == null || router.getUrl().getParameter(Constants.RUNTIME_KEY, false)) {
                    invokers = router.route(invokers, getConsumerUrl(), invocation);
                }
            } catch (Throwable t) {
                logger.error("Failed to execute router: " + getUrl() + ", cause: " + t.getMessage(), t);
            }
        }
    }
    return invokers;
}
Also used : Invoker(com.alibaba.dubbo.rpc.Invoker) RpcException(com.alibaba.dubbo.rpc.RpcException) Router(com.alibaba.dubbo.rpc.cluster.Router)

Example 47 with RpcException

use of com.alibaba.dubbo.rpc.RpcException in project dubbo by alibaba.

the class ConfigTest method test_RpcContext_getUrls.

// DUBBO-147 find all invoker instances which have been tried from RpcContext
@Test
public void test_RpcContext_getUrls() throws Exception {
    ClassPathXmlApplicationContext providerContext = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/demo-provider-long-waiting.xml");
    providerContext.start();
    try {
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/init-reference-getUrls.xml");
        ctx.start();
        try {
            DemoService demoService = (DemoService) ctx.getBean("demoService");
            try {
                demoService.sayName("Haha");
                fail();
            } catch (RpcException expected) {
                assertThat(expected.getMessage(), containsString("Tried 3 times"));
            }
            assertEquals(3, RpcContext.getContext().getUrls().size());
        } finally {
            ctx.stop();
            ctx.close();
        }
    } finally {
        providerContext.stop();
        providerContext.close();
    }
}
Also used : ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) RpcException(com.alibaba.dubbo.rpc.RpcException) DemoService(com.alibaba.dubbo.config.spring.api.DemoService) Test(org.junit.Test)

Example 48 with RpcException

use of com.alibaba.dubbo.rpc.RpcException in project dubbo by alibaba.

the class ConfigTest method test_retrySettingFail.

// BUG: DUBBO-846 in version 2.0.9, config retry="false" on provider's method doesn't work
@Test
public void test_retrySettingFail() throws Exception {
    ClassPathXmlApplicationContext providerContext = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/demo-provider-long-waiting.xml");
    providerContext.start();
    try {
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/init-reference-retry-false.xml");
        ctx.start();
        try {
            DemoService demoService = (DemoService) ctx.getBean("demoService");
            try {
                demoService.sayName("Haha");
                fail();
            } catch (RpcException expected) {
                assertThat(expected.getMessage(), containsString("Tried 1 times"));
            }
            assertEquals(1, RpcContext.getContext().getUrls().size());
        } finally {
            ctx.stop();
            ctx.close();
        }
    } finally {
        providerContext.stop();
        providerContext.close();
    }
}
Also used : ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) RpcException(com.alibaba.dubbo.rpc.RpcException) DemoService(com.alibaba.dubbo.config.spring.api.DemoService) Test(org.junit.Test)

Example 49 with RpcException

use of com.alibaba.dubbo.rpc.RpcException in project dubbo by alibaba.

the class DubboInvoker method doInvoke.

@Override
protected Result doInvoke(final Invocation invocation) throws Throwable {
    RpcInvocation inv = (RpcInvocation) invocation;
    final String methodName = RpcUtils.getMethodName(invocation);
    inv.setAttachment(Constants.PATH_KEY, getUrl().getPath());
    inv.setAttachment(Constants.VERSION_KEY, version);
    ExchangeClient currentClient;
    if (clients.length == 1) {
        currentClient = clients[0];
    } else {
        currentClient = clients[index.getAndIncrement() % clients.length];
    }
    try {
        boolean isAsync = RpcUtils.isAsync(getUrl(), invocation);
        boolean isOneway = RpcUtils.isOneway(getUrl(), invocation);
        int timeout = getUrl().getMethodParameter(methodName, Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT);
        if (isOneway) {
            boolean isSent = getUrl().getMethodParameter(methodName, Constants.SENT_KEY, false);
            currentClient.send(inv, isSent);
            RpcContext.getContext().setFuture(null);
            return new RpcResult();
        } else if (isAsync) {
            ResponseFuture future = currentClient.request(inv, timeout);
            RpcContext.getContext().setFuture(new FutureAdapter<Object>(future));
            return new RpcResult();
        } else {
            RpcContext.getContext().setFuture(null);
            return (Result) currentClient.request(inv, timeout).get();
        }
    } catch (TimeoutException e) {
        throw new RpcException(RpcException.TIMEOUT_EXCEPTION, "Invoke remote method timeout. method: " + invocation.getMethodName() + ", provider: " + getUrl() + ", cause: " + e.getMessage(), e);
    } catch (RemotingException e) {
        throw new RpcException(RpcException.NETWORK_EXCEPTION, "Failed to invoke remote method: " + invocation.getMethodName() + ", provider: " + getUrl() + ", cause: " + e.getMessage(), e);
    }
}
Also used : RpcInvocation(com.alibaba.dubbo.rpc.RpcInvocation) ExchangeClient(com.alibaba.dubbo.remoting.exchange.ExchangeClient) RpcException(com.alibaba.dubbo.rpc.RpcException) RemotingException(com.alibaba.dubbo.remoting.RemotingException) RpcResult(com.alibaba.dubbo.rpc.RpcResult) ResponseFuture(com.alibaba.dubbo.remoting.exchange.ResponseFuture) TimeoutException(com.alibaba.dubbo.remoting.TimeoutException)

Example 50 with RpcException

use of com.alibaba.dubbo.rpc.RpcException in project dubbo by alibaba.

the class DubboProtocol method createServer.

private ExchangeServer createServer(URL url) {
    // send readonly event when server closes, it's enabled by default
    url = url.addParameterIfAbsent(Constants.CHANNEL_READONLYEVENT_SENT_KEY, Boolean.TRUE.toString());
    // enable heartbeat by default
    url = url.addParameterIfAbsent(Constants.HEARTBEAT_KEY, String.valueOf(Constants.DEFAULT_HEARTBEAT));
    String str = url.getParameter(Constants.SERVER_KEY, Constants.DEFAULT_REMOTING_SERVER);
    if (str != null && str.length() > 0 && !ExtensionLoader.getExtensionLoader(Transporter.class).hasExtension(str))
        throw new RpcException("Unsupported server type: " + str + ", url: " + url);
    url = url.addParameter(Constants.CODEC_KEY, DubboCodec.NAME);
    ExchangeServer server;
    try {
        server = Exchangers.bind(url, requestHandler);
    } catch (RemotingException e) {
        throw new RpcException("Fail to start server(url: " + url + ") " + e.getMessage(), e);
    }
    str = url.getParameter(Constants.CLIENT_KEY);
    if (str != null && str.length() > 0) {
        Set<String> supportedTypes = ExtensionLoader.getExtensionLoader(Transporter.class).getSupportedExtensions();
        if (!supportedTypes.contains(str)) {
            throw new RpcException("Unsupported client type: " + str);
        }
    }
    return server;
}
Also used : RpcException(com.alibaba.dubbo.rpc.RpcException) RemotingException(com.alibaba.dubbo.remoting.RemotingException) ExchangeServer(com.alibaba.dubbo.remoting.exchange.ExchangeServer) Transporter(com.alibaba.dubbo.remoting.Transporter)

Aggregations

RpcException (com.alibaba.dubbo.rpc.RpcException)72 Test (org.junit.Test)25 RpcInvocation (com.alibaba.dubbo.rpc.RpcInvocation)23 Result (com.alibaba.dubbo.rpc.Result)22 URL (com.alibaba.dubbo.common.URL)20 RpcResult (com.alibaba.dubbo.rpc.RpcResult)17 ArrayList (java.util.ArrayList)11 Invoker (com.alibaba.dubbo.rpc.Invoker)10 Method (java.lang.reflect.Method)10 Invocation (com.alibaba.dubbo.rpc.Invocation)8 RemotingException (com.alibaba.dubbo.remoting.RemotingException)7 IOException (java.io.IOException)5 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 HashMap (java.util.HashMap)4 List (java.util.List)4 Map (java.util.Map)4 ExchangeClient (com.alibaba.dubbo.remoting.exchange.ExchangeClient)3 PluginTestVerifier (com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier)3 TApplicationException (org.apache.thrift.TApplicationException)3 TMessage (org.apache.thrift.protocol.TMessage)3