use of com.alibaba.dubbo.remoting.exchange.ResponseFuture in project dubbo by alibaba.
the class ClientToServerTest method testFuture.
@Test
public void testFuture() throws Exception {
ResponseFuture future = client.request(new World("world"));
Hello result = (Hello) future.get();
Assert.assertEquals("hello,world", result.getName());
}
use of com.alibaba.dubbo.remoting.exchange.ResponseFuture in project brave by openzipkin.
the class TracingFilter method invoke.
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
if (!isInit)
return invoker.invoke(invocation);
TraceContext invocationContext = currentTraceContext.get();
RpcContext rpcContext = RpcContext.getContext();
Kind kind = rpcContext.isProviderSide() ? Kind.SERVER : Kind.CLIENT;
Span span;
DubboRequest request;
if (kind.equals(Kind.CLIENT)) {
// When A service invoke B service, then B service then invoke C service, the parentId of the
// C service span is A when read from invocation.getAttachments(). This is because
// AbstractInvoker adds attachments via RpcContext.getContext(), not the invocation.
// See com.alibaba.dubbo.rpc.protocol.AbstractInvoker(line 138) from v2.6.7
Map<String, String> attachments = RpcContext.getContext().getAttachments();
DubboClientRequest clientRequest = new DubboClientRequest(invoker, invocation, attachments);
request = clientRequest;
span = clientHandler.handleSendWithParent(clientRequest, invocationContext);
} else {
DubboServerRequest serverRequest = new DubboServerRequest(invoker, invocation);
request = serverRequest;
span = serverHandler.handleReceive(serverRequest);
}
boolean isSynchronous = true;
Scope scope = currentTraceContext.newScope(span.context());
Result result = null;
Throwable error = null;
try {
result = invoker.invoke(invocation);
error = result.getException();
// the case on async client invocation
Future<Object> future = rpcContext.getFuture();
if (future != null) {
if (!(future instanceof FutureAdapter)) {
assert false : "we can't defer the span finish unless we can access the ResponseFuture";
return result;
}
isSynchronous = false;
ResponseFuture original = ((FutureAdapter<Object>) future).getFuture();
// See instrumentation/RATIONALE.md for why the below response callbacks are invocation context
TraceContext callbackContext = kind == Kind.CLIENT ? invocationContext : span.context();
ResponseFuture wrapped = new FinishSpanResponseFuture(original, this, request, result, span, callbackContext);
RpcContext.getContext().setFuture(new FutureAdapter<>(wrapped));
}
return result;
} catch (Throwable e) {
propagateIfFatal(e);
error = e;
throw e;
} finally {
if (isSynchronous)
FinishSpan.finish(this, request, result, error, span);
scope.close();
}
}
use of com.alibaba.dubbo.remoting.exchange.ResponseFuture 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);
}
}
use of com.alibaba.dubbo.remoting.exchange.ResponseFuture in project dubbo by alibaba.
the class FutureFilter method asyncCallback.
private void asyncCallback(final Invoker<?> invoker, final Invocation invocation) {
Future<?> f = RpcContext.getContext().getFuture();
if (f instanceof FutureAdapter) {
ResponseFuture future = ((FutureAdapter<?>) f).getFuture();
future.setCallback(new ResponseCallback() {
public void done(Object rpcResult) {
if (rpcResult == null) {
logger.error(new IllegalStateException("invalid result value : null, expected " + Result.class.getName()));
return;
}
// /must be rpcResult
if (!(rpcResult instanceof Result)) {
logger.error(new IllegalStateException("invalid result type :" + rpcResult.getClass() + ", expected " + Result.class.getName()));
return;
}
Result result = (Result) rpcResult;
if (result.hasException()) {
fireThrowCallback(invoker, invocation, result.getException());
} else {
fireReturnCallback(invoker, invocation, result.getValue());
}
}
public void caught(Throwable exception) {
fireThrowCallback(invoker, invocation, exception);
}
});
}
}
use of com.alibaba.dubbo.remoting.exchange.ResponseFuture in project dubbo by alibaba.
the class ClientToServerTest method testFuture.
@Test
public void testFuture() throws Exception {
ResponseFuture future = client.request(new World("world"));
Hello result = (Hello) future.get();
Assert.assertEquals("hello,world", result.getName());
}
Aggregations