1

gwt rpc AsyncCallback の後のコードが実行されない理由がわかりません。

たとえば、インターフェイス AppService が RemoteService を拡張しているため、非同期呼び出しを行う AsyncAppService があります。

次のコード

            AppServiceAsync service = GWT.create (AppService.class);
        service.getCurrentUser(new AsyncCallback<Employee>(){

            public void onFailure(Throwable caught) {

            }

            public void onSuccess(Employee result) {
                currentUser = result;
            }

        });
 // if i have the code after the above call, these code will not be execute, what is the problem
//code following will not be executed if they are in the same function.
    boolean isAdmin = false;
        if(currentUser!=null){
            if(currentUser.getUserRole().equals("ROLE_ADMIN") ||
                    currentUser.getUserRole().equals("ROLE_MANAGER")){
                isAdmin = true;
            }
        }

ご説明ありがとうございます

4

2 に答える 2

5

Async 呼び出しの性質を理解する必要があります。を呼び出すと、プログラムの実行は待機しませんservice.getCurrentUser。プログラムは次の行 ( boolean isAdmin = false) に進み、メソッドが実行されるまでしばらくの間 true になります。実行されていないコードのブロックをハンドラーに移動する必要があります(currentUser == null)getCurrentUseronSuccess

この例は次のようになります。

service.getCurrentUser(new AsyncCallback<Employee>(){

    public void onFailure(Throwable caught) {

    }

    public void onSuccess(Employee result) {
        currentUser = result;
        if (currentUser != null) {
            if (currentUser.getUserRole().equals("ROLE_ADMIN") ||
                currentUser.getUserRole().equals("ROLE_MANAGER")) {
                isAdmin = true;
            }
        }

    }

});

currentUser と isAdmin はクラス フィールドですが、ローカル変数ではないと想定しています。isAdminこの変数を最終的な配列にラップできるよりもローカルである場合:そしてfinal boolean[] isAdmin = new boolean[1]それを次のように呼び出しますisAdmin[0]

于 2012-04-04T16:19:39.187 に答える
1

あなたが昔のコンピューター化されていない株式/商品市場のブローカーだと想像してみてください。次のように機能することを想像してみましょう。

月曜日の午前9時30分です。これらを順番に計画しました。実際、あなたは非常に経験豊富なので、次のようにプログラムされています。

プログラム BuyAlBanySteel(500):

  1. あなたは 500 AlbanySteel を購入するために電話をかけたいと考えています。
  2. トレーディングフロアを回るコールボードを渡します。
  3. コールボードがオファーとともに戻ってきて、オファーがあなたに同意する場合は、オファー者に株式を購入するようアプローチしてください。

プログラム ドリンク コーヒー

  1. コーヒーを注ぐ
  2. コーヒーを飲む。

対処しなければならない注意点: 電話の応答を得るには、少なくとも 10 分、場合によっては 1 時間かかります。非同期です。どれくらいの時間がかかるかはわかりますが、確かではありません。

これが朝の一連の計画です。

  1. BuyAlBanySteel(500)を実行
  2. コーヒーを飲む。

お聞きしたいのですが、ワークフローをどのように構成しますか? このように構成しますか?各タスクを実行するのに 1 分のブロックがかかるとしましょう。

9.31 am
Send offer(
  buy = 500 AlbanySteel
  messenger = annie
  When annie comes back, analyse offer.
  Pour Annie a cup of tea.
)

9.32 am
if (annie has an agreeable offer) buy 500 AlbanySteel.

9.33 am
Pour Coffee.

9.34
Drink Coffee.

もちろん、できません。理由は次の行です

9.32 am
if (annie has an agreeable offer) buy 500 AlbanySteel.

正しく実行されません。アニーがまだオファーを持って戻ってこなかったので、それは実行されなかったように見えます. 彼女がオファーを返すまで、さらに 10 分または 1 時間かかる場合があります。

したがって、これがワークフローを実行する方法です

9.31 am
Send offer(
  buy = 500 AlbanySteel
  messenger = annie
  when annie comes back,
  analyse offer.
  Pour Annie a cup of tea.
  if (annie has an agreeable offer) buy 500 AlbanySteel.
)

9.33 am
Pour Coffee.

9.34
Drink Coffee.

では、GWT 疑似コードでは、どれを実行することを選択しますか?

これを実行しますか:

BuyAlbanySteelAsync albanyService = GWT.create(BuyAlbanySteel.class);

albanyService.getOffer(
  new Task<Annie>(){
    onFailure(Annie){Do nothing}

    OnSuccess(Annie){
       analyse offer.
       Pour Annie a cup of tea.
    }
  }
);

if(Annie has agreeable offer)
  buy the stock.


またはこれ:

BuyAlbanySteelAsync albanyService = GWT.create(BuyAlbanySteel.class);

albanyService.getOffer(
  new Task<Annie>(){
    onFailure(Annie){Do nothing}

    OnSuccess(Annie){
       analyse offer.
       Pour Annie a cup of tea.
       if(Annie has agreeable offer)
         buy the stock.
    }
  }
);
于 2012-04-05T02:32:29.080 に答える