use of org.apache.dubbo.rpc.Result in project dubbo by alibaba.
the class MergeableClusterInvoker method doInvoke.
@Override
protected Result doInvoke(Invocation invocation, List<Invoker<T>> invokers, LoadBalance loadbalance) throws RpcException {
checkInvokers(invokers, invocation);
String merger = getUrl().getMethodParameter(invocation.getMethodName(), 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()) {
try {
return invoker.invoke(invocation);
} catch (RpcException e) {
if (e.isNoInvokerAvailableAfterFilter()) {
log.debug("No available provider for service" + getUrl().getServiceKey() + " on group " + invoker.getUrl().getParameter(GROUP_KEY) + ", will continue to try another group.");
} else {
throw e;
}
}
}
}
return invokers.iterator().next().invoke(invocation);
}
Class<?> returnType;
try {
returnType = getInterface().getMethod(invocation.getMethodName(), invocation.getParameterTypes()).getReturnType();
} catch (NoSuchMethodException e) {
returnType = null;
}
Map<String, Result> results = new HashMap<>();
for (final Invoker<T> invoker : invokers) {
RpcInvocation subInvocation = new RpcInvocation(invocation, invoker);
subInvocation.setAttachment(ASYNC_KEY, "true");
results.put(invoker.getUrl().getServiceKey(), invoker.invoke(subInvocation));
}
Object result = null;
List<Result> resultList = new ArrayList<Result>(results.size());
for (Map.Entry<String, Result> entry : results.entrySet()) {
Result asyncResult = entry.getValue();
try {
Result r = asyncResult.get();
if (r.hasException()) {
log.error("Invoke " + getGroupDescFromServiceKey(entry.getKey()) + " failed: " + r.getException().getMessage(), r.getException());
} else {
resultList.add(r);
}
} catch (Exception e) {
throw new RpcException("Failed to invoke service " + entry.getKey() + ": " + e.getMessage(), e);
}
}
if (resultList.isEmpty()) {
return AsyncRpcResult.newDefaultAsyncResult(invocation);
} else if (resultList.size() == 1) {
return AsyncRpcResult.newDefaultAsyncResult(resultList.get(0).getValue(), invocation);
}
if (returnType == void.class) {
return AsyncRpcResult.newDefaultAsyncResult(invocation);
}
if (merger.startsWith(".")) {
merger = merger.substring(1);
Method method;
try {
method = returnType.getMethod(merger, returnType);
} catch (NoSuchMethodException e) {
throw new RpcException("Can not merge result because missing method [ " + merger + " ] in class [ " + returnType.getName() + " ]");
}
ReflectUtils.makeAccessible(method);
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("Can not merge result: " + e.getMessage(), 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 AsyncRpcResult.newDefaultAsyncResult(result, invocation);
}
use of org.apache.dubbo.rpc.Result in project dubbo by alibaba.
the class FailfastClusterInvokerTest method testInvokeNoException.
@Test()
public void testInvokeNoException() {
resetInvoker1ToNoException();
FailfastClusterInvoker<FailfastClusterInvokerTest> invoker = new FailfastClusterInvoker<FailfastClusterInvokerTest>(dic);
Result ret = invoker.invoke(invocation);
Assertions.assertSame(result, ret);
}
use of org.apache.dubbo.rpc.Result in project dubbo by alibaba.
the class CacheFilter method invoke.
/**
* If cache is configured, dubbo will invoke method on each method call. If cache value is returned by cache store
* then it will return otherwise call the remote method and return value. If remote method's return value has error
* then it will not cache the value.
* @param invoker service
* @param invocation invocation.
* @return Cache returned value if found by the underlying cache store. If cache miss it will call target method.
* @throws RpcException
*/
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
if (cacheFactory != null && ConfigUtils.isNotEmpty(invoker.getUrl().getMethodParameter(invocation.getMethodName(), CACHE_KEY))) {
Cache cache = cacheFactory.getCache(invoker.getUrl(), invocation);
if (cache != null) {
String key = StringUtils.toArgumentString(invocation.getArguments());
Object value = cache.get(key);
if (value != null) {
if (value instanceof ValueWrapper) {
return AsyncRpcResult.newDefaultAsyncResult(((ValueWrapper) value).get(), invocation);
} else {
return AsyncRpcResult.newDefaultAsyncResult(value, invocation);
}
}
Result result = invoker.invoke(invocation);
if (!result.hasException()) {
cache.put(key, new ValueWrapper(result.getValue()));
}
return result;
}
}
return invoker.invoke(invocation);
}
use of org.apache.dubbo.rpc.Result in project dubbo by alibaba.
the class ServerExceptionTest method testServerException.
@Test
public void testServerException() throws Exception {
Assertions.assertThrows(RpcException.class, () -> {
Assertions.assertNotNull(invoker);
RpcInvocation invocation = new RpcInvocation();
invocation.setMethodName("echoString");
invocation.setParameterTypes(new Class<?>[] { String.class });
String arg = "Hello, World!";
invocation.setArguments(new Object[] { arg });
Result result = invoker.invoke(invocation);
System.out.println(result);
});
}
use of org.apache.dubbo.rpc.Result in project brave by openzipkin.
the class ITTracingFilter_Consumer method customParser.
@Test
public void customParser() {
Tag<DubboResponse> javaValue = new Tag<DubboResponse>("dubbo.result_value") {
@Override
protected String parseValue(DubboResponse input, TraceContext context) {
Result result = input.result();
if (result == null)
return null;
return String.valueOf(result.getValue());
}
};
RpcTracing rpcTracing = RpcTracing.newBuilder(tracing).clientResponseParser((res, context, span) -> {
RpcResponseParser.DEFAULT.parse(res, context, span);
if (res instanceof DubboResponse) {
javaValue.tag((DubboResponse) res, span);
}
}).build();
init().setRpcTracing(rpcTracing);
String javaResult = client.get().sayHello("jorge");
assertThat(testSpanHandler.takeRemoteSpan(CLIENT).tags()).containsEntry("dubbo.result_value", javaResult);
}
Aggregations