Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve: RpcInvokeCallbackListener resolve response #336

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 33 additions & 77 deletions src/main/java/com/alipay/remoting/rpc/RpcInvokeCallbackListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,8 @@
import com.alipay.remoting.*;
import org.slf4j.Logger;

import com.alipay.remoting.exception.CodecException;
import com.alipay.remoting.exception.ConnectionClosedException;
import com.alipay.remoting.log.BoltLoggerFactory;
import com.alipay.remoting.rpc.exception.InvokeException;
import com.alipay.remoting.rpc.exception.InvokeServerBusyException;
import com.alipay.remoting.rpc.exception.InvokeServerException;
import com.alipay.remoting.rpc.exception.InvokeTimeoutException;
import com.alipay.remoting.rpc.protocol.RpcResponseCommand;

/**
* Listener which listens the Rpc invoke result, and then invokes the call back.
Expand Down Expand Up @@ -107,101 +101,63 @@ public CallbackTask(String remoteAddress, InvokeFuture future) {
public void run() {
InvokeCallback callback = future.getInvokeCallback();
// a lot of try-catches to protect thread pool
ResponseCommand response = null;

try {
response = (ResponseCommand) future.waitResponse(0);
} catch (InterruptedException e) {
String msg = "Exception caught when getting response from InvokeFuture. The address is "
+ this.remoteAddress;
logger.error(msg, e);
}
if (response == null || response.getResponseStatus() != ResponseStatus.SUCCESS) {
ResponseCommand response = null;

try {
Exception e;
if (response == null) {
e = new InvokeException("Exception caught in invocation. The address is "
+ this.remoteAddress + " responseStatus:"
+ ResponseStatus.UNKNOWN, future.getCause());
} else {
response.setInvokeContext(future.getInvokeContext());
switch (response.getResponseStatus()) {
case TIMEOUT:
e = new InvokeTimeoutException(
"Invoke timeout when invoke with callback.The address is "
+ this.remoteAddress);
break;
case CONNECTION_CLOSED:
e = new ConnectionClosedException(
"Connection closed when invoke with callback.The address is "
+ this.remoteAddress);
break;
case SERVER_THREADPOOL_BUSY:
e = new InvokeServerBusyException(
"Server thread pool busy when invoke with callback.The address is "
+ this.remoteAddress);
break;
case SERVER_EXCEPTION:
String msg = "Server exception when invoke with callback.Please check the server log! The address is "
+ this.remoteAddress;
RpcResponseCommand resp = (RpcResponseCommand) response;
resp.deserialize();
Object ex = resp.getResponseObject();
if (ex instanceof Throwable) {
e = new InvokeServerException(msg, (Throwable) ex);
} else {
e = new InvokeServerException(msg);
}
break;
default:
e = new InvokeException(
"Exception caught in invocation. The address is "
+ this.remoteAddress + " responseStatus:"
+ response.getResponseStatus(), future.getCause());
response = (ResponseCommand) future.waitResponse(0);
} catch (InterruptedException e) {
String msg = "Exception caught when getting response from InvokeFuture. The address is "
+ this.remoteAddress;
logger.error(msg, e);
throw new InvokeException(msg, e);
}

}
}
callback.onException(e);
} catch (Throwable e) {
logger
.error(
"Exception occurred in user defined InvokeCallback#onException() logic, The address is {}",
this.remoteAddress, e);
if (response == null) {
throw new InvokeException("Exception caught in invocation. The address is "
+ this.remoteAddress + " responseStatus:"
+ ResponseStatus.UNKNOWN, future.getCause());
JoeCqupt marked this conversation as resolved.
Show resolved Hide resolved
}
} else {

ClassLoader oldClassLoader = null;
try {
if (future.getAppClassLoader() != null) {
oldClassLoader = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(future.getAppClassLoader());
}

response.setInvokeContext(future.getInvokeContext());
RpcResponseCommand rpcResponse = (RpcResponseCommand) response;
response.deserialize();
Object responseObj = RpcResponseResolver.resolveResponseObject(response,
this.remoteAddress);
try {
callback.onResponse(rpcResponse.getResponseObject());
callback.onResponse(responseObj);
} catch (Throwable e) {
logger
.error(
"Exception occurred in user defined InvokeCallback#onResponse() logic.",
e);
}
} catch (CodecException e) {
logger
.error(
"CodecException caught on when deserialize response in RpcInvokeCallbackListener. The address is {}.",
this.remoteAddress, e);
} catch (Throwable e) {
logger.error(
"Exception caught in RpcInvokeCallbackListener. The address is {}",
this.remoteAddress, e);

} finally {
if (oldClassLoader != null) {
Thread.currentThread().setContextClassLoader(oldClassLoader);
}
}
} // enf of else
} // end of run

} catch (Throwable t) {
try {
callback.onException(t);
} catch (Throwable te) {
logger
.error(
"Exception occurred in user defined InvokeCallback#onException() logic, The address is {}",
this.remoteAddress, te);
// Consider rethrowing the exception or handling it according to your application's needs
}
}

}
}

/**
Expand Down
Loading