code after Gwt rpc AsyncCallbak will not be executed?

前端 未结 2 1116
梦如初夏
梦如初夏 2021-01-25 02:30

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 A

相关标签:
2条回答
  • 2021-01-25 03:03

    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]

    0 讨论(0)
  • 2021-01-25 03:27

    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.
        }
      }
    );
    
    0 讨论(0)
提交回复
热议问题