From 6ffe440029e8f1ba79698006cc2c5c4ac7850db5 Mon Sep 17 00:00:00 2001 From: "chao.liuc" Date: Tue, 25 Oct 2011 08:09:32 +0000 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3setcallback=20=E6=97=B6?= =?UTF-8?q?=E5=8F=AF=E8=83=BD=E5=87=BA=E7=8E=B0=E7=9A=84=E5=B9=B6=E5=8F=91?= =?UTF-8?q?=E9=97=AE=E9=A2=98=20DUBBO-750?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: http://code.alibabatech.com/svn/dubbo/trunk@43 1a56cb94-b969-4eaa-88fa-be21384802f2 --- .../exchange/support/DefaultFuture.java | 61 ++++++++++++------- 1 file changed, 39 insertions(+), 22 deletions(-) diff --git a/dubbo-remoting/src/main/java/com/alibaba/dubbo/remoting/exchange/support/DefaultFuture.java b/dubbo-remoting/src/main/java/com/alibaba/dubbo/remoting/exchange/support/DefaultFuture.java index 7c1086fc6..7525b8a4a 100644 --- a/dubbo-remoting/src/main/java/com/alibaba/dubbo/remoting/exchange/support/DefaultFuture.java +++ b/dubbo-remoting/src/main/java/com/alibaba/dubbo/remoting/exchange/support/DefaultFuture.java @@ -38,7 +38,8 @@ import com.alibaba.dubbo.remoting.exchange.Response; /** * DefaultFuture. * - * @author qian.lei + * @author qian.lei + * @author chao.liuc */ public class DefaultFuture implements ResponseFuture { @@ -110,8 +111,9 @@ public class DefaultFuture implements ResponseFuture { } public void cancel(){ - response = new Response(id); - response.setErrorMessage("request future has been canceled."); + Response errorResult = new Response(id); + errorResult.setErrorMessage("request future has been canceled."); + response = errorResult ; FUTURES.remove(id); CHANNELS.remove(id); } @@ -120,16 +122,39 @@ public class DefaultFuture implements ResponseFuture { return response != null; } - public void setCallback(ResponseCallback callback) { - if (isDone()) { - try { - callback.done(returnFromResponse()); - } catch (Throwable e) { - callback.caught(e); - } - } else { - this.callback = callback; + public void setCallback(ResponseCallback callback) { + if (isDone()) { + invokeCallback(callback); + } else { + boolean isdone = false; + lock.lock(); + try{ + if (!isDone()) { + this.callback = callback; + } else { + isdone = true; + } + }finally { + lock.unlock(); + } + if (isdone){ + invokeCallback(callback); + } } + } + private void invokeCallback(ResponseCallback c){ + if (c == null){ + throw new NullPointerException("callback cannot be null."); + } + ResponseCallback callbackCopy = c; + c = null; + Object result = null; + try { + result = returnFromResponse(); + } catch (Throwable e) { + callbackCopy.caught(e); + } + callbackCopy.done(result); } private long getId() { @@ -198,14 +223,8 @@ public class DefaultFuture implements ResponseFuture { } finally { lock.unlock(); } - ResponseCallback c = callback; - if (c != null) { - try { - c.done(returnFromResponse()); - } catch (Throwable e) { - c.caught(e); - } - callback = null; + if (callback != null) { + invokeCallback(callback); } } @@ -217,8 +236,6 @@ public class DefaultFuture implements ResponseFuture { if (res.getStatus() == Response.CLIENT_TIMEOUT || res.getStatus() == Response.SERVER_TIMEOUT) { throw new TimeoutException(res.getStatus() == Response.SERVER_TIMEOUT, channel, res.getErrorMessage()); } - FUTURES.remove(id); - CHANNELS.remove(id); throw new RemotingException(channel, res.getErrorMessage()); } -- GitLab