use of com.alibaba.dubbo.rpc.RpcException in project dubbo by alibaba.
the class MockClusterInvoker method invoke.
public Result invoke(Invocation invocation) throws RpcException {
Result result = null;
String value = directory.getUrl().getMethodParameter(invocation.getMethodName(), Constants.MOCK_KEY, Boolean.FALSE.toString()).trim();
if (value.length() == 0 || value.equalsIgnoreCase("false")) {
// no mock
result = this.invoker.invoke(invocation);
} else if (value.startsWith("force")) {
if (logger.isWarnEnabled()) {
logger.info("force-mock: " + invocation.getMethodName() + " force-mock enabled , url : " + directory.getUrl());
}
// force:direct mock
result = doMockInvoke(invocation, null);
} else {
// fail-mock
try {
result = this.invoker.invoke(invocation);
} catch (RpcException e) {
if (e.isBiz()) {
throw e;
} else {
if (logger.isWarnEnabled()) {
logger.info("fail-mock: " + invocation.getMethodName() + " fail-mock enabled , url : " + directory.getUrl(), e);
}
result = doMockInvoke(invocation, e);
}
}
}
return result;
}
use of com.alibaba.dubbo.rpc.RpcException in project dubbo by alibaba.
the class AbstractClusterInvokerTest method testTimeoutExceptionCode.
@Test()
public void testTimeoutExceptionCode() {
List<Invoker<DemoService>> invokers = new ArrayList<Invoker<DemoService>>();
invokers.add(new Invoker<DemoService>() {
public Class<DemoService> getInterface() {
return DemoService.class;
}
public URL getUrl() {
return URL.valueOf("dubbo://" + NetUtils.getLocalHost() + ":20880/" + DemoService.class.getName());
}
public boolean isAvailable() {
return false;
}
public Result invoke(Invocation invocation) throws RpcException {
throw new RpcException(RpcException.TIMEOUT_EXCEPTION, "test timeout");
}
public void destroy() {
}
});
Directory<DemoService> directory = new StaticDirectory<DemoService>(invokers);
FailoverClusterInvoker<DemoService> failoverClusterInvoker = new FailoverClusterInvoker<DemoService>(directory);
try {
failoverClusterInvoker.invoke(new RpcInvocation("sayHello", new Class<?>[0], new Object[0]));
Assert.fail();
} catch (RpcException e) {
Assert.assertEquals(RpcException.TIMEOUT_EXCEPTION, e.getCode());
}
ForkingClusterInvoker<DemoService> forkingClusterInvoker = new ForkingClusterInvoker<DemoService>(directory);
try {
forkingClusterInvoker.invoke(new RpcInvocation("sayHello", new Class<?>[0], new Object[0]));
Assert.fail();
} catch (RpcException e) {
Assert.assertEquals(RpcException.TIMEOUT_EXCEPTION, e.getCode());
}
FailfastClusterInvoker<DemoService> failfastClusterInvoker = new FailfastClusterInvoker<DemoService>(directory);
try {
failfastClusterInvoker.invoke(new RpcInvocation("sayHello", new Class<?>[0], new Object[0]));
Assert.fail();
} catch (RpcException e) {
Assert.assertEquals(RpcException.TIMEOUT_EXCEPTION, e.getCode());
}
}
use of com.alibaba.dubbo.rpc.RpcException in project dubbo by alibaba.
the class AbstractClusterInvokerTest method setUp.
@SuppressWarnings({ "unchecked" })
@Before
public void setUp() throws Exception {
invocation.setMethodName("sayHello");
invoker1 = EasyMock.createMock(Invoker.class);
invoker2 = EasyMock.createMock(Invoker.class);
invoker3 = EasyMock.createMock(Invoker.class);
invoker4 = EasyMock.createMock(Invoker.class);
invoker5 = EasyMock.createMock(Invoker.class);
mockedInvoker1 = EasyMock.createMock(Invoker.class);
URL turl = URL.valueOf("test://test:11/test");
EasyMock.expect(invoker1.isAvailable()).andReturn(false).anyTimes();
EasyMock.expect(invoker1.getInterface()).andReturn(IHelloService.class).anyTimes();
EasyMock.expect(invoker1.getUrl()).andReturn(turl.addParameter("name", "invoker1")).anyTimes();
EasyMock.expect(invoker2.isAvailable()).andReturn(true).anyTimes();
EasyMock.expect(invoker2.getInterface()).andReturn(IHelloService.class).anyTimes();
EasyMock.expect(invoker2.getUrl()).andReturn(turl.addParameter("name", "invoker2")).anyTimes();
EasyMock.expect(invoker3.isAvailable()).andReturn(false).anyTimes();
EasyMock.expect(invoker3.getInterface()).andReturn(IHelloService.class).anyTimes();
EasyMock.expect(invoker3.getUrl()).andReturn(turl.addParameter("name", "invoker3")).anyTimes();
EasyMock.expect(invoker4.isAvailable()).andReturn(true).anyTimes();
EasyMock.expect(invoker4.getInterface()).andReturn(IHelloService.class).anyTimes();
EasyMock.expect(invoker4.getUrl()).andReturn(turl.addParameter("name", "invoker4")).anyTimes();
EasyMock.expect(invoker5.isAvailable()).andReturn(false).anyTimes();
EasyMock.expect(invoker5.getInterface()).andReturn(IHelloService.class).anyTimes();
EasyMock.expect(invoker5.getUrl()).andReturn(turl.addParameter("name", "invoker5")).anyTimes();
EasyMock.expect(mockedInvoker1.isAvailable()).andReturn(false).anyTimes();
EasyMock.expect(mockedInvoker1.getInterface()).andReturn(IHelloService.class).anyTimes();
EasyMock.expect(mockedInvoker1.getUrl()).andReturn(turl.setProtocol("mock")).anyTimes();
EasyMock.replay(invoker1, invoker2, invoker3, invoker4, invoker5, mockedInvoker1);
invokers.add(invoker1);
dic = new StaticDirectory<IHelloService>(url, invokers, null);
cluster = new AbstractClusterInvoker(dic) {
@Override
protected Result doInvoke(Invocation invocation, List invokers, LoadBalance loadbalance) throws RpcException {
return null;
}
};
cluster_nocheck = new AbstractClusterInvoker(dic, url.addParameterIfAbsent(Constants.CLUSTER_AVAILABLE_CHECK_KEY, Boolean.FALSE.toString())) {
@Override
protected Result doInvoke(Invocation invocation, List invokers, LoadBalance loadbalance) throws RpcException {
return null;
}
};
}
use of com.alibaba.dubbo.rpc.RpcException in project dubbo-faker by moyada.
the class MethodHandleProxy method getProxy.
public MethodProxy getProxy(MethodInvokeDO invokeInfo) {
// , int poolSize) {
MethodProxy proxy;
log.info("init method proxy info.");
// 检测是否已存在
Integer id = invokeInfo.getId();
SoftReference<MethodProxy> ref = proxyMap.get(id);
if (null != ref) {
proxy = ref.get();
if (null != proxy) {
// && proxy.getService().length == poolSize) {
return proxy;
}
}
// 获取参数类型
Class<?>[] paramTypes;
String[] argsType = invokeInfo.getParamType().split(",");
int length = argsType.length;
if (0 == length) {
paramTypes = new Class[0];
} else {
paramTypes = new Class[length];
for (int index = 0; index < length; index++) {
try {
paramTypes[index] = ReflectUtil.getClassType(argsType[index]);
} catch (ClassNotFoundException e) {
throw new InitializeInvokerException("获取参数类型失败: " + argsType[index]);
}
}
}
// 获取方法具柄
MethodHandle methodHandle = handle.fetchHandleInfo(invokeInfo.getClassName(), invokeInfo.getMethodName(), invokeInfo.getReturnType(), paramTypes);
// 获取接口
Class classType;
try {
classType = ReflectUtil.getClassType(invokeInfo.getClassName());
} catch (ClassNotFoundException e) {
throw new InitializeInvokerException("获取结果失败: " + invokeInfo.getClassName());
}
// 获取接口实例
Object serviceAssembly;
// Object[] serviceAssembly = new Object[poolSize];
try {
serviceAssembly = beanHolder.getBean(classType);
// for (int index = 0; index < poolSize; index++) {
// serviceAssembly[index] = beanHelper.getBean(classType);
// }
} catch (BeansException e) {
throw new RpcException("获取接口实例失败: " + invokeInfo.getClassName() + ".", e);
}
// for (Object service : serviceAssembly) {
try {
methodHandle.invoke(serviceAssembly, null);
} catch (Throwable throwable) {
}
// }
proxy = new MethodProxy();
proxy.setParamTypes(paramTypes);
proxy.setMethodHandle(methodHandle);
proxy.setService(serviceAssembly);
// 缓存调用代理
ref = new SoftReference<>(proxy);
proxyMap.put(id, ref);
return proxy;
}
use of com.alibaba.dubbo.rpc.RpcException in project dubbo by alibaba.
the class MergeableClusterInvoker method invoke.
@SuppressWarnings("rawtypes")
public Result invoke(final Invocation invocation) throws RpcException {
List<Invoker<T>> invokers = directory.list(invocation);
String merger = getUrl().getMethodParameter(invocation.getMethodName(), Constants.MERGER_KEY);
if (ConfigUtils.isEmpty(merger)) {
// If a method doesn't have a merger, only invoke one Group
for (final Invoker<T> invoker : invokers) {
if (invoker.isAvailable()) {
return invoker.invoke(invocation);
}
}
return invokers.iterator().next().invoke(invocation);
}
Class<?> returnType;
try {
returnType = getInterface().getMethod(invocation.getMethodName(), invocation.getParameterTypes()).getReturnType();
} catch (NoSuchMethodException e) {
returnType = null;
}
Map<String, Future<Result>> results = new HashMap<String, Future<Result>>();
for (final Invoker<T> invoker : invokers) {
Future<Result> future = executor.submit(new Callable<Result>() {
public Result call() throws Exception {
return invoker.invoke(new RpcInvocation(invocation, invoker));
}
});
results.put(invoker.getUrl().getServiceKey(), future);
}
Object result = null;
List<Result> resultList = new ArrayList<Result>(results.size());
int timeout = getUrl().getMethodParameter(invocation.getMethodName(), Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT);
for (Map.Entry<String, Future<Result>> entry : results.entrySet()) {
Future<Result> future = entry.getValue();
try {
Result r = future.get(timeout, TimeUnit.MILLISECONDS);
if (r.hasException()) {
log.error(new StringBuilder(32).append("Invoke ").append(getGroupDescFromServiceKey(entry.getKey())).append(" failed: ").append(r.getException().getMessage()).toString(), r.getException());
} else {
resultList.add(r);
}
} catch (Exception e) {
throw new RpcException(new StringBuilder(32).append("Failed to invoke service ").append(entry.getKey()).append(": ").append(e.getMessage()).toString(), e);
}
}
if (resultList.isEmpty()) {
return new RpcResult((Object) null);
} else if (resultList.size() == 1) {
return resultList.iterator().next();
}
if (returnType == void.class) {
return new RpcResult((Object) null);
}
if (merger.startsWith(".")) {
merger = merger.substring(1);
Method method;
try {
method = returnType.getMethod(merger, returnType);
} catch (NoSuchMethodException e) {
throw new RpcException(new StringBuilder(32).append("Can not merge result because missing method [ ").append(merger).append(" ] in class [ ").append(returnType.getClass().getName()).append(" ]").toString());
}
if (!Modifier.isPublic(method.getModifiers())) {
method.setAccessible(true);
}
result = resultList.remove(0).getValue();
try {
if (method.getReturnType() != void.class && method.getReturnType().isAssignableFrom(result.getClass())) {
for (Result r : resultList) {
result = method.invoke(result, r.getValue());
}
} else {
for (Result r : resultList) {
method.invoke(result, r.getValue());
}
}
} catch (Exception e) {
throw new RpcException(new StringBuilder(32).append("Can not merge result: ").append(e.getMessage()).toString(), e);
}
} else {
Merger resultMerger;
if (ConfigUtils.isDefault(merger)) {
resultMerger = MergerFactory.getMerger(returnType);
} else {
resultMerger = ExtensionLoader.getExtensionLoader(Merger.class).getExtension(merger);
}
if (resultMerger != null) {
List<Object> rets = new ArrayList<Object>(resultList.size());
for (Result r : resultList) {
rets.add(r.getValue());
}
result = resultMerger.merge(rets.toArray((Object[]) Array.newInstance(returnType, 0)));
} else {
throw new RpcException("There is no merger to merge result.");
}
}
return new RpcResult(result);
}
Aggregations