finish the RPC call befor the execution of other methods or code?

22 views
Skip to first unread message

learning coding

unread,
Mar 19, 2012, 11:30:58 AM3/19/12
to google-we...@googlegroups.com, Juan Pablo Gardella
HI all
 
I am doing web application Project in GWT. I am using RPC, which is working fine.

I have 4 packeages

  1. .client
  2. .client.GUI
  3. .client.control
  4. .shared
  5. .server

From .gui.loginpage

On onClickmethod of a button I make a object of Login Class which is in client.control

onSuccess of RPC public Boolean setConnection(true)

but when i try to getConnectionform .gui.loginpage its show me null.

because the call back function is executed asynchronously, the function is invoked after the response comes to the browser from the server.

But the other part of the code is executed immediately by which time connection is still null.

I need a solution or a method or any logic so that other function waits till the rpc call is complete.
 
Thanks
 

Jens

unread,
Mar 19, 2012, 12:00:29 PM3/19/12
to google-we...@googlegroups.com, Juan Pablo Gardella
You have to convert your code to use a callback:

Instead of:

void onClick() {
  myObject.doSomethingAsync(xyz);
  myObject.getConnection();
  //additional code
}

convert it to:

void onClick() {
  myObject.doSomethingAsync(xyz, new Callback() {
    void onSuccess() {
      myObject.getConnection();
      //additional code
    }
  });
}

void doSomethingAsync(Object o, Callback cb) {
  serverService.rpcMethod(o, new AsyncCallback<Object> {
    void onSuccess(Object result) {
      setConnection(true);
      cb.onSuccess(); //notify caller via its callback
    }
  }
}

Alternatively you can use two events (one event results in execution of the async method and the second one will be fired when the async request has been finished).


-- J.

learning coding

unread,
Mar 19, 2012, 12:18:34 PM3/19/12
to google-we...@googlegroups.com, jens.ne...@gmail.com
Ya i did that before but somebody told me that this is not good way coding.
As I am new to coding i dont understand the complexity iissues much during coding.
 
here is my piece of code.
 .client.GUI
public Class GUi(){ 
 
// More Code
 
 
public void onClick(ClickEvent event) {
 
 
LoginServer
loginServer =new  LoginServer(getTextBoxUsername().getText(),getTextBoxPassword().getText());
     
loginServer
.setConnection(connection);
 
connection
=loginServer.getConnection();
 
// More code
 
} 
}
 
client.control
 
public class LoginServer { 
// more code
 
public void setConnection(Boolean connection) {
 
 
   
String[] authentication = {username,password};
 
   
//RPC call
 
    connectionService
.connectionServer(authentication, callbackConnection);
  
   
     System.out.println("setConnection" + connection); 
}
 
 
public Boolean getConnection() {
 
   
return connection;
 
}
 
AsyncCallback callbackConnection = new AsyncCallback() {
 
 
   
public void onFailure(Throwable caught) {
 
       
// TODO Auto-generated method stub
 
        connection
=false;
 
   
}
 
 
   
public void onSuccess(Object result) {
 
        connection
=true;
 
       
System.out.println("onSuccess + connection);
 
 
   
}
 
};
 
 
}
 

 

help me to solve this.
 
thanks

 
 

 

 



 

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-web-toolkit/-/JF6fL4gXVSUJ.
To post to this group, send email to google-we...@googlegroups.com.
To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

Reply all
Reply to author
Forward
0 new messages