code after Gwt rpc AsyncCallbak will not be executed?

邮差的信 提交于 2019-12-02 11:48:56

问题


I can't understand why the code after the gwt rpc AsyncCallback will not be executed?

for example I have the interface AppService extends RemoteService, So I'll have AsyncAppService which does the async call.

the following code

            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;
            }
        }

Thanks for your explaination


回答1:


You should understand the nature of the Async call. The programm execution will not be waiting when you call service.getCurrentUser. The programm will continue to the next line (boolean isAdmin = false) and it will be true for some time that (currentUser == null) until method getCurrentUser is executing. You should move not executed block of code into the onSuccess handler

This example should look something like this:

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;
            }
        }

    }

});

I assume that currentUser and isAdmin are class fields, but not local variables. If isAdmin is local than you can wrap this variable into the final array: final boolean[] isAdmin = new boolean[1] and call it like isAdmin[0]




回答2:


Imagine you are a broker in a non-computerised stock/commodities market of the old days. Let's imagine that it functions in the following manner.

It's Monday 9.30 am. You have these planned in sequence. In fact, you are so experienced it is programmed into you:

Programme BuyAlBanySteel(500):

  1. You want to make a call to buy 500 AlbanySteel.
  2. Pass the call board which will be circulated around the trading floor.
  3. When call board returns with the offer and offer is agreeable to you, approach offeror to buy the stock.

Programme Drink Coffee

  1. Pour Coffee
  2. Drink coffee.

Caveat you have to deal with: getting a call response takes at least 10 minutes, or even an hour. It is asynchronous. You have an idea how long it takes but it is not certain.

So this is your plan in sequence for the morning:

  1. execute BuyAlBanySteel(500)
  2. Drink coffee.

Let me ask you, how would you structure your work flow? Would you structure it this way? Let's say each task takes you blocks of one minute to perform.

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.

Of course you can't. The reason is the following line

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

will not be performed properly. It will appear not to to have been performed because Annie would not have come back with an offer yet. It might take her another 10 minutes or an hour to come back with an offer.

So, this is how you have to execute your work flow

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.

So, in GWT pseudocode, which one would you choose to execute?

Would you execute this:

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.


Or this:

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.
    }
  }
);


来源:https://stackoverflow.com/questions/10014319/code-after-gwt-rpc-asynccallbak-will-not-be-executed

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!