English 中文(简体)
GWT: 以同步 RPC 取代阻断电话
原标题:GWT: Replacing blocking calls with async RPCs

我正在将本地的 Java 应用程序转换为 GWT 。 与服务器的通信只在状态改变期间发生, 并且到目前为止一直通过阻塞操作进行处理 。

例如, 当前同步逻辑:

void onUserClickedSync() {
    downloadData(); // blocking operation
    uploadData(); // blocking operation
    setState(DONE);
}

< 强势> 如何用那些使用非同步回击的阻截操作来取代阻截操作?

我目前的想法是基本上添加一系列“不做任何事情的”额外“busy ” 状态。 然后我用RPC的回电触发下一个状态,逻辑可以延续到下一个状态。

例如,同步逻辑将变成:

void onUserClickedSync() {
    rpc.downloadData(new AsyncCallback<Data> {
        public void onSuccess(Data result) {
            //...
            onDownloaded();
        }
        //...
    });
    setState(WAITING_FOR_DOWNLOAD);
}

void onDownloaded() {
    rpc.uploadData(new AsyncCallback<Void> {
        public void onSuccess(Void void) {
            //...
            setState(DONE);
        }
    //...
    });
    setState(WAITING_FOR_UPLOAD);
}

这个方法有效吗?

" 强势 " EDIT: " 强势 " 重写我用假码写的例子,因为它们非常不清楚。

最佳回答

好吧,对不起 缠绕你的问题, 但这个例子对我来说并不很清楚。

Now that I got a better handle of the situation yes, I think your approach is viable. Just be careful that when the callback changes the state of the system there is no "side effect" that could possibly clash with other events going on at the same time.

具体地说,它并不清楚您是否在“ 等待多个回调 ” ( 即用户开始 4 个上传, 这样您可以得到 4 个回调, 不一定是“ 正确的顺序 ” ) 。 另外, 上传数据方法能否在相应的下载数据之前结束?

一般而言,你必须小心,因为虽然你以前的代码为了可预测性而牺牲了反应能力(例如,第一次下载完成之前不会发生任何情况),但现在事情却以更不可预测的顺序发生,有时你可能会引入难以正确诊断或复制的微妙错误。

我们看不到应用程序的其余部分, 所以不清楚回调之间还能发生什么, 但我敦促您非常小心, 并且让回调错误处理特别强( 例如, 如果上传在中途失败, 会发生什么? 您仍然会收到服务器的回调, 告诉你它已经中止? 您移动到哪个状态?

问题回答

当设计远程互动界面时,通常你应该尽可能粗糙地进行操作,以减少延迟。

特别是,您可以将您的“ 下载” 和“ 上载” 合并为单项操作。 这样您就可以将延迟时间降低为单次往返, 并消除多个等候状态 。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签