为了包装每一个AsynCallback<T>
传递给任何RemoteService
你需要覆盖的东西,RemoteServiceProxy#doCreateRequestCallback()
因为每一个AsynCallback<T>
都是在 RPC 调用发生之前提交的。
以下是执行此操作的步骤:
首先,您需要定义自己的代理生成器,以便在每次RemoteService
生成代理时介入。从扩展ServiceInterfaceProxyGenerator
和覆盖开始#createProxyCreator()
。
/**
* This Generator extends the default GWT {@link ServiceInterfaceProxyGenerator} and replaces it in the
* co.company.MyModule GWT module for all types that are assignable to
* {@link com.google.gwt.user.client.rpc.RemoteService}. Instead of the default GWT {@link ProxyCreator} it provides the
* {@link MyProxyCreator}.
*/
public class MyServiceInterfaceProxyGenerator extends ServiceInterfaceProxyGenerator {
@Override
protected ProxyCreator createProxyCreator(JClassType remoteService) {
return new MyProxyCreator(remoteService);
}
}
在您MyModule.gwt.xml
使用延迟绑定来指示 GWT 在生成以下内容时使用您的代理生成器进行编译RemoteService
:
<generate-with
class="com.company.ourapp.rebind.rpc.MyServiceInterfaceProxyGenerator">
<when-type-assignable class="com.google.gwt.user.client.rpc.RemoteService"/>
</generate-with>
扩展ProxyCreator
和覆盖#getProxySupertype()
. 使用它,MyServiceInterfaceProxyGenerator#createProxyCreator()
以便您可以为所有生成的RemoteServiceProxies
.
/**
* This proxy creator extends the default GWT {@link ProxyCreator} and replaces {@link RemoteServiceProxy} as base class
* of proxies with {@link MyRemoteServiceProxy}.
*/
public class MyProxyCreator extends ProxyCreator {
public MyProxyCreator(JClassType serviceIntf) {
super(serviceIntf);
}
@Override
protected Class<? extends RemoteServiceProxy> getProxySupertype() {
return MyRemoteServiceProxy.class;
}
}
确保你MyProxyCreator
和你MyServiceInterfaceProxyGenerator
都位于一个不会被 GWT 交叉编译成 javascript 的包中。否则你会看到这样的错误:
[ERROR] Line XX: No source code is available for type com.google.gwt.user.rebind.rpc.ProxyCreator; did you forget to inherit a required module?
您现在已准备好扩展RemoteServiceProxy
和覆盖#doCreateRequestCallback()
!在这里,您可以做任何您喜欢的事情,并将其应用于发送到您服务器的每个回调。确保将此类以及您在此处使用的任何其他类(在我的情况下AsyncCallbackProxy
)添加到要交叉编译的客户端包中。
/**
* The remote service proxy extends default GWT {@link RemoteServiceProxy} and proxies the {@link AsyncCallback} with
* the {@link AsyncCallbackProxy}.
*/
public class MyRemoteServiceProxy extends RemoteServiceProxy {
public MyRemoteServiceProxy(String moduleBaseURL, String remoteServiceRelativePath, String serializationPolicyName,
Serializer serializer) {
super(moduleBaseURL, remoteServiceRelativePath, serializationPolicyName, serializer);
}
@Override
protected <T> RequestCallback doCreateRequestCallback(RequestCallbackAdapter.ResponseReader responseReader,
String methodName, RpcStatsContext statsContext,
AsyncCallback<T> callback) {
return super.doCreateRequestCallback(responseReader, methodName, statsContext, new AsyncCallbackProxy<T>(callback));
}
}
现在,你AsyncCallbackProxy
可以看起来像这样:
public class AsyncCallbackProxy<T> implements AsyncCallback<T> {
private AsyncCallback<T> delegate;
public AsyncCallbackProxy(AsyncCallback<T> delegate) {
this.delegate = delegate;
}
@Override
public final void onFailure(Throwable caught) {
GWT.log("AsyncCallbackProxy#onFailure() : " + caught.getMessage(), caught);
if (caught instanceof NotLoggedInException) {
// Handle it here
}
delegate.onFailure(proxy);
}
@Override
public final void onSuccess(T result) {
delegate.onSuccess(result);
}
}
参考: