我们想出了另一种解决方法来乐观锁定我们的应用程序。由于版本不能与代理本身一起传递(正如 Thomas 解释的那样),我们通过HTTP GET参数将它传递给请求工厂。
在客户端:
MyRequestFactory factory = GWT.create( MyRequestFactory.class );
RequestTransport transport = new DefaultRequestTransport() {
@Override
public String getRequestUrl() {
return super.getRequestUrl() + "?version=" + getMyVersion();
}
};
factory.initialize(new SimpleEventBus(), transport);
在服务器上,我们创建一个 ServiceLayerDecorator 并从以下位置读取版本RequestFactoryServlet.getThreadLocalRequest()
:
public static class MyServiceLayerDecorator extends ServiceLayerDecorator {
@Override
public final <T> T loadDomainObject(final Class<T> clazz, final Object domainId) {
HttpServletRequest threadLocalRequest = RequestFactoryServlet.getThreadLocalRequest();
String clientVersion = threadLocalRequest.getParameter("version") );
T domainObject = super.loadDomainObject(clazz, domainId);
String serverVersion = ((HasVersion)domainObject).getVersion();
if ( versionMismatch(serverVersion, clientVersion) )
report("Version error!");
return domainObject;
}
}
优点是loadDomainObject()
在 RF 将任何更改应用于域对象之前调用它。
在我们的例子中,我们只跟踪一个实体,所以我们使用一个版本,但方法可以扩展到多个实体。