Skip to content

Commit dca34c2

Browse files
committed
improve RpcInvokeCallbackListener resolve response
1 parent 829be55 commit dca34c2

File tree

1 file changed

+32
-71
lines changed

1 file changed

+32
-71
lines changed

src/main/java/com/alipay/remoting/rpc/RpcInvokeCallbackListener.java

Lines changed: 32 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -107,101 +107,62 @@ public CallbackTask(String remoteAddress, InvokeFuture future) {
107107
public void run() {
108108
InvokeCallback callback = future.getInvokeCallback();
109109
// a lot of try-catches to protect thread pool
110-
ResponseCommand response = null;
111110

112111
try {
113-
response = (ResponseCommand) future.waitResponse(0);
114-
} catch (InterruptedException e) {
115-
String msg = "Exception caught when getting response from InvokeFuture. The address is "
116-
+ this.remoteAddress;
117-
logger.error(msg, e);
118-
}
119-
if (response == null || response.getResponseStatus() != ResponseStatus.SUCCESS) {
112+
ResponseCommand response = null;
113+
120114
try {
121-
Exception e;
122-
if (response == null) {
123-
e = new InvokeException("Exception caught in invocation. The address is "
124-
+ this.remoteAddress + " responseStatus:"
125-
+ ResponseStatus.UNKNOWN, future.getCause());
126-
} else {
127-
response.setInvokeContext(future.getInvokeContext());
128-
switch (response.getResponseStatus()) {
129-
case TIMEOUT:
130-
e = new InvokeTimeoutException(
131-
"Invoke timeout when invoke with callback.The address is "
132-
+ this.remoteAddress);
133-
break;
134-
case CONNECTION_CLOSED:
135-
e = new ConnectionClosedException(
136-
"Connection closed when invoke with callback.The address is "
137-
+ this.remoteAddress);
138-
break;
139-
case SERVER_THREADPOOL_BUSY:
140-
e = new InvokeServerBusyException(
141-
"Server thread pool busy when invoke with callback.The address is "
142-
+ this.remoteAddress);
143-
break;
144-
case SERVER_EXCEPTION:
145-
String msg = "Server exception when invoke with callback.Please check the server log! The address is "
146-
+ this.remoteAddress;
147-
RpcResponseCommand resp = (RpcResponseCommand) response;
148-
resp.deserialize();
149-
Object ex = resp.getResponseObject();
150-
if (ex instanceof Throwable) {
151-
e = new InvokeServerException(msg, (Throwable) ex);
152-
} else {
153-
e = new InvokeServerException(msg);
154-
}
155-
break;
156-
default:
157-
e = new InvokeException(
158-
"Exception caught in invocation. The address is "
159-
+ this.remoteAddress + " responseStatus:"
160-
+ response.getResponseStatus(), future.getCause());
115+
response = (ResponseCommand) future.waitResponse(0);
116+
} catch (InterruptedException e) {
117+
String msg = "Exception caught when getting response from InvokeFuture. The address is "
118+
+ this.remoteAddress;
119+
logger.error(msg, e);
120+
throw new InvokeException(msg, e);
121+
}
161122

162-
}
163-
}
164-
callback.onException(e);
165-
} catch (Throwable e) {
166-
logger
167-
.error(
168-
"Exception occurred in user defined InvokeCallback#onException() logic, The address is {}",
169-
this.remoteAddress, e);
123+
if (response == null) {
124+
throw new InvokeException("Exception caught in invocation. The address is "
125+
+ this.remoteAddress + " responseStatus:"
126+
+ ResponseStatus.UNKNOWN, future.getCause());
170127
}
171-
} else {
128+
129+
Object responseObj = RpcResponseResolver.resolveResponseObject(response,
130+
this.remoteAddress);
131+
172132
ClassLoader oldClassLoader = null;
173133
try {
174134
if (future.getAppClassLoader() != null) {
175135
oldClassLoader = Thread.currentThread().getContextClassLoader();
176136
Thread.currentThread().setContextClassLoader(future.getAppClassLoader());
177137
}
178138
response.setInvokeContext(future.getInvokeContext());
179-
RpcResponseCommand rpcResponse = (RpcResponseCommand) response;
180-
response.deserialize();
181139
try {
182-
callback.onResponse(rpcResponse.getResponseObject());
140+
callback.onResponse(responseObj);
183141
} catch (Throwable e) {
184142
logger
185143
.error(
186144
"Exception occurred in user defined InvokeCallback#onResponse() logic.",
187145
e);
188146
}
189-
} catch (CodecException e) {
190-
logger
191-
.error(
192-
"CodecException caught on when deserialize response in RpcInvokeCallbackListener. The address is {}.",
193-
this.remoteAddress, e);
194-
} catch (Throwable e) {
195-
logger.error(
196-
"Exception caught in RpcInvokeCallbackListener. The address is {}",
197-
this.remoteAddress, e);
147+
198148
} finally {
199149
if (oldClassLoader != null) {
200150
Thread.currentThread().setContextClassLoader(oldClassLoader);
201151
}
202152
}
203-
} // enf of else
204-
} // end of run
153+
154+
} catch (Throwable t) {
155+
try {
156+
callback.onException(t);
157+
} catch (Throwable te) {
158+
logger
159+
.error(
160+
"Exception occurred in user defined InvokeCallback#onException() logic, The address is {}",
161+
this.remoteAddress, te);
162+
}
163+
}
164+
165+
}
205166
}
206167

207168
/**

0 commit comments

Comments
 (0)