Nie*_*Wet 5 gwt callback gwt-rpc
在我的GWT应用程序中,使用许多不同的服务对服务器进行了许多不同的异步调用.为了做更好的错误处理,我想包装所有的回调,以便我可以像InvocationExceptions在一个地方一样处理异常.超类实现AsyncCallback实际上不是一个选项,因为这意味着我将不得不修改每个异步调用.
RpcServiceProxy#doCreateRequestCallback()看起来像要覆盖的方法.很简单.我只是看不出如何让GWT使用我的新课程.
陈述问题的另一种方式是
如何让GWT使用我自己的子类
RpcServiceProxy?
为了包装AsynCallback<T>传递给RemoteService你需要覆盖的任何东西,RemoteServiceProxy#doCreateRequestCallback()因为AsynCallback<T>在RPC调用发生之前每个都被传递到这里.
正如@ChrisLercher所提到的,您需要定义自己的代理生成器,以便在每次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);
}
}
Run Code Online (Sandbox Code Playgroud)
在您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>
Run Code Online (Sandbox Code Playgroud)
扩展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;
}
}
Run Code Online (Sandbox Code Playgroud)
确保你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?
Run Code Online (Sandbox Code Playgroud)
您现在已准备好扩展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));
}
}
Run Code Online (Sandbox Code Playgroud)
参考文献: