CLIENT SIDE FILE: "UpdateGrid.java"
public class UpdateGrid implements EntryPoint {
/** This is the entry point method. */
public void onModuleLoad() {
UpdateServiceAsync updateSvc =
(UpdateServiceAsync)GWT.create(UpdateService.class);
ServiceDefTarget target = (ServiceDefTarget) updateSvc;
String staticResponseURL = "/app/updateService";
target.setServiceEntryPoint(staticResponseURL);
final AsyncCallback updateCallback = new AsyncCallback() {
// Method called on successful callback
public void onSuccess (Object result) {
// Update the status field
udpateStatus("Rx ("+((ArrayList)result).size()+")");
// Call the updateService again
UpdateServiceAsync updateSvc =
(UpdateServiceAsync)
GWT.create(UpdateService.class);
ServiceDefTarget endpoint =
(ServiceDefTarget) updateSvc;
endpoint.setServiceEntryPoint("/app/updateService");
updateSvc.updateRows("0",this);
}
// Method called on callback failed
public void onFailure (Throwable ex) {
RootPanel.get().add(new HTML(ex.toString()));
}
};
}
// call the service
updateSvc.updateRows("0",updateCallback);
// update the message panel
public void udpateStatus(String msg){
RootPanel status = RootPanel.get("gwtStatus");
if(status !=null){
Label label= (Label)status.iterator().next();
String currentMsg = label.getText();
label.setText(msg);
label.setStyleName("status");
}
}
}
SERVER SIDE FILE: "UpdateServiceImpl.java"
public ArrayList updateRows(String viewID){
ArrayList updates = new ArrayList();
try {
// if there are no new updates, sleep for a while
while(incomingUpdates.size() == 0){
try {
Thread.sleep(Math.max(0, 500));
} catch (InterruptedException e){ }
}
// we must have new updates, so deal with them
Iterator updateItr = null;
synchronized (this) {
updateItr = new ArrayList(incomingUpdates).iterator();
incomingUpdates.clear();
}
// create new TableRows for each update
while(updateItr.hasNext()){
UpdateHolder uh = (UpdateHolder)updateItr.next();
// create the new TableRow for the update
TableRow tr = convertUpdate(tr);
System.out.println("- update ADDED");
updates.add(tr);
}
// return this array to the client callback method
return updates;
} catch(Exception e){
logger.log(Level.SEVERE,"Bad happenings",e);
}
return null;
}
Can anyone give me suggestions on how to debug this one? How can I
test for dropped messages between the server and client?
I use a tcp monitor to see the data going back and forth to the
server, and I also use firebug (in firefox) to monitor the RPC requests.
is it possible that your callbacks are going out of scope or getting
destroyed (is that even possible) prior to receiving a response?
I've not noticed any dropped responses in my application.
-jason
In my application, the client continually sends RPC requests and gets
callbacks every few seconds. Maybe the callback frequency has
something to do with it.
I use the tcpmon monitor from the apache axis project. It is a
standalone java monitor.
However, firebug seems to do just as good a job when just looking at
the RPC request/response cycle.
are you running in hosted or web mode? Since I'm developing on OS X,
I only ever run in web mode.
BTW there is also an IRC channel ##GWT on freenode if you want more
interactive conversations.
-jason
Sorry for not responding earlier. The problem was that the servlet
method updateRows() was holding the connection open by waiting for new
update objects:
while(incomingUpdates.size() == 0){
try {
Thread.sleep(Math.max(0, 500));
} catch (InterruptedException e){ }
}
When someone hit the browser refresh, the RPC mechanism would sometimes
get screwed up.
I resolved this problem by moving the wait to the client side. Now the
client polls the server and server responds immediately with whatever
it has. The original code was just a poor design.