取消 GWT RequestFactory 请求

发布于 2024-11-25 12:34:37 字数 34 浏览 0 评论 0原文

有没有办法取消/中止请求工厂请求?使用 GWT 2.3

Is there a way to cancel/abort request factory requests? Using GWT 2.3

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一袭白衣梦中忆 2024-12-02 12:34:37

调用 fire() 方法后无法取消请求。考虑构建自定义 Receiver 基类,如下所示:

public abstract class CancelableReceiver<V> extends Receiver<V> {
  private boolean canceled;

  public void cancel() {
    canceled = true;
  }

  @Override
  public final void onSuccess(V response) {
    if (!canceled) {
      doOnSuccess(response);
    }
  }

  protected abstract void doOnSuccess(V response);
}

对于 Receiver 类型中的其他方法,可以重复该模式。

There is no way to cancel a request after the fire() method has been called. Consider building a custom Receiver base class such as the following:

public abstract class CancelableReceiver<V> extends Receiver<V> {
  private boolean canceled;

  public void cancel() {
    canceled = true;
  }

  @Override
  public final void onSuccess(V response) {
    if (!canceled) {
      doOnSuccess(response);
    }
  }

  protected abstract void doOnSuccess(V response);
}

The pattern can be repeated for other methods in the Receiver type.

流绪微梦 2024-12-02 12:34:37

另一种选择是创建替代的 com.google.web.bindery.requestfactory.shared.RequestTransport 类型,而不是使用 DefaultRequestTransport。这样做的缺点(也是 BobV 方法的优点)是,您不知道服务器上的请求何时杀死它,因此它可能已经运行了您的一些方法 - 您不会从其中任何一个方法获得反馈,您只需停止传出请求即可。

我怀疑这就是为什么 RF 没有像 RPC 那样具有此功能的原因。甚至考虑 RPC 或 RequestBuilder 的情况 - 它们如何通知服务器他们改变了主意,并且不运行请求?我的理解是他们没有 - 他们提前关闭的唯一方法是当他们尝试读/写响应时,并收到 TCP 错误,因为连接已关闭。 (我可能错了,另一个线程密切关注 tcp 连接的状态并调用 thread.stop(Throwable),但 stop 已被弃用 相当多while。)

一种想法是向服务器发送一条消息,告诉它杀死来自同一会话的其他请求 - 但这需要积极参与您的服务器代码,可能会在 中通用ServiceLayerDecorator 子类型,可能至少在 invokeloadDomainObject(s)getSetter 等中。很明显,这需要 GWT 为您构建它……

Another option would be to create an alternative com.google.web.bindery.requestfactory.shared.RequestTransport type, instead of using DefaultRequestTransport. Downside to this (and upside to BobV's approach) is that you won't know when in the request on the server you kill it, so it might have already run some of your methods - you won't get feedback from any of them, you'll just stop the outgoing request.

I suspect this is why RF doesn't have this feature already, as RPC does. Consider even the case of RPC though or RequestBuilder - how do those notify the server that they've changed their mind, and to not run the request? My understanding is that they don't - the only way they are shut down early is when they try to read/write to the response, and get a tcp error, as the connection has been closed. (It's possible I am mistaken, and that another thread keeps an eye on the state of the tcp connection and calls thread.stop(Throwable), but stop has been deprecated for quite a while.)

One thought would be to send a message to the server, telling it to kill off other requests from the same session - this would require active participation in your server code though, possibly made generic in a ServiceLayerDecorator subtype, probably in at least invoke, loadDomainObject(s), and getSetter, among others. This pretty clearly is to involved to ask GWT to build it for you though...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文