Search in sources :

Example 16 with RpcResult

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

the class MockClusterInvoker method doMockInvoke.

@SuppressWarnings({ "unchecked", "rawtypes" })
private Result doMockInvoke(Invocation invocation, RpcException e) {
    Result result = null;
    Invoker<T> minvoker;
    List<Invoker<T>> mockInvokers = selectMockInvoker(invocation);
    if (mockInvokers == null || mockInvokers.size() == 0) {
        minvoker = (Invoker<T>) new MockInvoker(directory.getUrl());
    } else {
        minvoker = mockInvokers.get(0);
    }
    try {
        result = minvoker.invoke(invocation);
    } catch (RpcException me) {
        if (me.isBiz()) {
            result = new RpcResult(me.getCause());
        } else {
            throw new RpcException(me.getCode(), getMockExceptionMessage(e, me), me.getCause());
        }
    //			
    } catch (Throwable me) {
        throw new RpcException(getMockExceptionMessage(e, me), me.getCause());
    }
    return result;
}
Also used : Invoker(com.alibaba.dubbo.rpc.Invoker) MockInvoker(com.alibaba.dubbo.rpc.support.MockInvoker) MockInvoker(com.alibaba.dubbo.rpc.support.MockInvoker) RpcException(com.alibaba.dubbo.rpc.RpcException) RpcResult(com.alibaba.dubbo.rpc.RpcResult) Result(com.alibaba.dubbo.rpc.Result) RpcResult(com.alibaba.dubbo.rpc.RpcResult)

Example 17 with RpcResult

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

the class FailbackClusterInvoker method doInvoke.

protected Result doInvoke(Invocation invocation, List<Invoker<T>> invokers, LoadBalance loadbalance) throws RpcException {
    try {
        checkInvokers(invokers, invocation);
        Invoker<T> invoker = select(loadbalance, invocation, invokers, null);
        return invoker.invoke(invocation);
    } catch (Throwable e) {
        logger.error("Failback to invoke method " + invocation.getMethodName() + ", wait for retry in background. Ignored exception: " + e.getMessage() + ", ", e);
        addFailed(invocation, this);
        // ignore
        return new RpcResult();
    }
}
Also used : RpcResult(com.alibaba.dubbo.rpc.RpcResult)

Example 18 with RpcResult

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

the class MergeableClusterInvokerTest method testAddMenu.

@Test
public void testAddMenu() throws Exception {
    String menu = "first";
    List<String> menuItems = new ArrayList<String>() {

        {
            add("1");
            add("2");
        }
    };
    EasyMock.expect(invocation.getMethodName()).andReturn("addMenu").anyTimes();
    EasyMock.expect(invocation.getParameterTypes()).andReturn(new Class<?>[] { String.class, List.class }).anyTimes();
    EasyMock.expect(invocation.getArguments()).andReturn(new Object[] { menu, menuItems }).anyTimes();
    EasyMock.expect(invocation.getAttachments()).andReturn(new HashMap<String, String>()).anyTimes();
    EasyMock.expect(invocation.getInvoker()).andReturn(firstInvoker).anyTimes();
    EasyMock.replay(invocation);
    EasyMock.expect(firstInvoker.getUrl()).andReturn(url.addParameter(Constants.GROUP_KEY, "first")).anyTimes();
    EasyMock.expect(firstInvoker.getInterface()).andReturn(MenuService.class).anyTimes();
    EasyMock.expect(firstInvoker.invoke(invocation)).andReturn(new RpcResult()).anyTimes();
    EasyMock.expect(firstInvoker.isAvailable()).andReturn(true).anyTimes();
    EasyMock.replay(firstInvoker);
    EasyMock.expect(secondInvoker.getUrl()).andReturn(url.addParameter(Constants.GROUP_KEY, "second")).anyTimes();
    EasyMock.expect(secondInvoker.getInterface()).andReturn(MenuService.class).anyTimes();
    EasyMock.expect(secondInvoker.invoke(invocation)).andReturn(new RpcResult()).anyTimes();
    EasyMock.expect(secondInvoker.isAvailable()).andReturn(true).anyTimes();
    EasyMock.replay(secondInvoker);
    EasyMock.expect(directory.list(invocation)).andReturn(new ArrayList() {

        {
            add(firstInvoker);
            add(secondInvoker);
        }
    }).anyTimes();
    EasyMock.expect(directory.getUrl()).andReturn(url).anyTimes();
    EasyMock.expect(directory.getInterface()).andReturn(MenuService.class).anyTimes();
    EasyMock.replay(directory);
    mergeableClusterInvoker = new MergeableClusterInvoker<MenuService>(directory);
    Result result = mergeableClusterInvoker.invoke(invocation);
    Assert.assertNull(result.getValue());
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) RpcResult(com.alibaba.dubbo.rpc.RpcResult) ArrayList(java.util.ArrayList) List(java.util.List) Result(com.alibaba.dubbo.rpc.Result) RpcResult(com.alibaba.dubbo.rpc.RpcResult) Test(org.junit.Test)

Example 19 with RpcResult

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

the class MergeableClusterInvokerTest method testGetMenuSuccessfully.

@Test
public void testGetMenuSuccessfully() throws Exception {
    // setup
    url = url.addParameter(Constants.MERGER_KEY, ".merge");
    EasyMock.expect(invocation.getMethodName()).andReturn("getMenu").anyTimes();
    EasyMock.expect(invocation.getParameterTypes()).andReturn(new Class<?>[] {}).anyTimes();
    EasyMock.expect(invocation.getArguments()).andReturn(new Object[] {}).anyTimes();
    EasyMock.expect(invocation.getAttachments()).andReturn(new HashMap<String, String>()).anyTimes();
    EasyMock.expect(invocation.getInvoker()).andReturn(firstInvoker).anyTimes();
    EasyMock.replay(invocation);
    firstInvoker = (Invoker) Proxy.newProxyInstance(getClass().getClassLoader(), new Class<?>[] { Invoker.class }, new InvocationHandler() {

        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            if ("getUrl".equals(method.getName())) {
                return url.addParameter(Constants.GROUP_KEY, "first");
            }
            if ("getInterface".equals(method.getName())) {
                return MenuService.class;
            }
            if ("invoke".equals(method.getName())) {
                return new RpcResult(firstMenu);
            }
            return null;
        }
    });
    secondInvoker = (Invoker) Proxy.newProxyInstance(getClass().getClassLoader(), new Class<?>[] { Invoker.class }, new InvocationHandler() {

        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            if ("getUrl".equals(method.getName())) {
                return url.addParameter(Constants.GROUP_KEY, "second");
            }
            if ("getInterface".equals(method.getName())) {
                return MenuService.class;
            }
            if ("invoke".equals(method.getName())) {
                return new RpcResult(secondMenu);
            }
            return null;
        }
    });
    EasyMock.expect(directory.list(invocation)).andReturn(new ArrayList() {

        {
            add(firstInvoker);
            add(secondInvoker);
        }
    }).anyTimes();
    EasyMock.expect(directory.getUrl()).andReturn(url).anyTimes();
    EasyMock.expect(directory.getInterface()).andReturn(MenuService.class).anyTimes();
    EasyMock.replay(directory);
    mergeableClusterInvoker = new MergeableClusterInvoker<MenuService>(directory);
    // invoke
    Result result = mergeableClusterInvoker.invoke(invocation);
    Assert.assertTrue(result.getValue() instanceof Menu);
    Menu menu = (Menu) result.getValue();
    Map<String, List<String>> expected = new HashMap<String, List<String>>();
    merge(expected, firstMenuMap);
    merge(expected, secondMenuMap);
    TestCase.assertEquals(expected.keySet(), menu.getMenus().keySet());
    for (String key : expected.keySet()) {
        // FIXME: cannot guarantee the sequence of the merge result, check implementation in
        // MergeableClusterInvoker#invoke
        List<String> values1 = new ArrayList<String>(expected.get(key));
        List<String> values2 = new ArrayList<String>(menu.getMenus().get(key));
        Collections.sort(values1);
        Collections.sort(values2);
        TestCase.assertEquals(values1, values2);
    }
}
Also used : HashMap(java.util.HashMap) RpcResult(com.alibaba.dubbo.rpc.RpcResult) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) InvocationHandler(java.lang.reflect.InvocationHandler) Result(com.alibaba.dubbo.rpc.Result) RpcResult(com.alibaba.dubbo.rpc.RpcResult) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 20 with RpcResult

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

the class CacheFilter method invoke.

public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
    if (cacheFactory != null && ConfigUtils.isNotEmpty(invoker.getUrl().getMethodParameter(invocation.getMethodName(), Constants.CACHE_KEY))) {
        Cache cache = cacheFactory.getCache(invoker.getUrl().addParameter(Constants.METHOD_KEY, invocation.getMethodName()));
        if (cache != null) {
            String key = StringUtils.toArgumentString(invocation.getArguments());
            if (cache != null && key != null) {
                Object value = cache.get(key);
                if (value != null) {
                    return new RpcResult(value);
                }
                Result result = invoker.invoke(invocation);
                if (!result.hasException()) {
                    cache.put(key, result.getValue());
                }
                return result;
            }
        }
    }
    return invoker.invoke(invocation);
}
Also used : RpcResult(com.alibaba.dubbo.rpc.RpcResult) Cache(com.alibaba.dubbo.cache.Cache) Result(com.alibaba.dubbo.rpc.Result) RpcResult(com.alibaba.dubbo.rpc.RpcResult)

Aggregations

RpcResult (com.alibaba.dubbo.rpc.RpcResult)34 Result (com.alibaba.dubbo.rpc.Result)19 Test (org.junit.Test)17 URL (com.alibaba.dubbo.common.URL)15 RpcException (com.alibaba.dubbo.rpc.RpcException)14 Invocation (com.alibaba.dubbo.rpc.Invocation)11 Invoker (com.alibaba.dubbo.rpc.Invoker)9 DemoService (com.alibaba.dubbo.rpc.support.DemoService)9 RpcInvocation (com.alibaba.dubbo.rpc.RpcInvocation)8 Method (java.lang.reflect.Method)8 TMessage (org.apache.thrift.protocol.TMessage)6 Request (com.alibaba.dubbo.remoting.exchange.Request)5 Response (com.alibaba.dubbo.remoting.exchange.Response)5 TBinaryProtocol (org.apache.thrift.protocol.TBinaryProtocol)5 TIOStreamTransport (org.apache.thrift.transport.TIOStreamTransport)5 Channel (com.alibaba.dubbo.remoting.Channel)4 ChannelBuffer (com.alibaba.dubbo.remoting.buffer.ChannelBuffer)4 Demo (com.alibaba.dubbo.rpc.gen.thrift.Demo)4 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4