Search in sources :

Example 1 with ResponseCallback

use of com.alibaba.dubbo.remoting.exchange.ResponseCallback in project dubbo by alibaba.

the class DefaultFuture method invokeCallback.

private void invokeCallback(ResponseCallback c) {
    ResponseCallback callbackCopy = c;
    if (callbackCopy == null) {
        throw new NullPointerException("callback cannot be null.");
    }
    c = null;
    Response res = response;
    if (res == null) {
        throw new IllegalStateException("response cannot be null. url:" + channel.getUrl());
    }
    if (res.getStatus() == Response.OK) {
        try {
            callbackCopy.done(res.getResult());
        } catch (Exception e) {
            logger.error("callback invoke error .reasult:" + res.getResult() + ",url:" + channel.getUrl(), e);
        }
    } else if (res.getStatus() == Response.CLIENT_TIMEOUT || res.getStatus() == Response.SERVER_TIMEOUT) {
        try {
            TimeoutException te = new TimeoutException(res.getStatus() == Response.SERVER_TIMEOUT, channel, res.getErrorMessage());
            callbackCopy.caught(te);
        } catch (Exception e) {
            logger.error("callback invoke error ,url:" + channel.getUrl(), e);
        }
    } else {
        try {
            RuntimeException re = new RuntimeException(res.getErrorMessage());
            callbackCopy.caught(re);
        } catch (Exception e) {
            logger.error("callback invoke error ,url:" + channel.getUrl(), e);
        }
    }
}
Also used : Response(com.alibaba.dubbo.remoting.exchange.Response) ResponseCallback(com.alibaba.dubbo.remoting.exchange.ResponseCallback) TimeoutException(com.alibaba.dubbo.remoting.TimeoutException) RemotingException(com.alibaba.dubbo.remoting.RemotingException) TimeoutException(com.alibaba.dubbo.remoting.TimeoutException)

Example 2 with ResponseCallback

use of com.alibaba.dubbo.remoting.exchange.ResponseCallback 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);
            }
        });
    }
}
Also used : FutureAdapter(com.alibaba.dubbo.rpc.protocol.dubbo.FutureAdapter) ResponseCallback(com.alibaba.dubbo.remoting.exchange.ResponseCallback) ResponseFuture(com.alibaba.dubbo.remoting.exchange.ResponseFuture) Result(com.alibaba.dubbo.rpc.Result)

Aggregations

ResponseCallback (com.alibaba.dubbo.remoting.exchange.ResponseCallback)2 RemotingException (com.alibaba.dubbo.remoting.RemotingException)1 TimeoutException (com.alibaba.dubbo.remoting.TimeoutException)1 Response (com.alibaba.dubbo.remoting.exchange.Response)1 ResponseFuture (com.alibaba.dubbo.remoting.exchange.ResponseFuture)1 Result (com.alibaba.dubbo.rpc.Result)1 FutureAdapter (com.alibaba.dubbo.rpc.protocol.dubbo.FutureAdapter)1