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;
}
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();
}
}
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());
}
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);
}
}
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);
}
Aggregations