tags:

views:

211

answers:

1

I'm writing a simple Google Web Toolkit service which acts as a proxy, which will basically exist to allow the client to make a POST to a different server. The client essentially uses this service to request an HTTP call. The service has only one asynchronous method call, called ajax(), which should just forward the server response. My code for implementing the call looks like this:

class ProxyServiceImpl extends RemoteServiceServlet implements ProxyService {
    @Override
    public Response ajax(String data) {
        RequestBuilder rb = /*make a request builder*/
        RequestCallback rc = new RequestCallback() {
            @Override
            public void onResponseReceived(Response response) {
                /* Forward this response back to the client as the return value of 
                   the ajax method... somehow... */
            }
         };
         rb.sendRequest(data, requestCallback);
         return /* The response above... except I can't */;
    }
}

You can see the basic form of my problem, of course. The ajax() method is used asynchronously, but GWT decides to be smart and hide that from the dumb old developer, so they can just write normal Java code without callbacks. GWT services basically just do magic instead of accepting a callback parameter.

The trouble arises, then, because GWT is hiding the callback object from me. I'm trying to make my own asynchronous call from the service implementation, but I can't, because GWT services assume that you behave synchronously in service implementations. How can I work around this and make an asynchronous call from my service method implementation?

+3  A: 

You are mixing up client and server side code. In ProxyServiceImpl, you CANNOT use RequestBuilder. RequestBuilder is a client side class which will only execute in the browser.

A server-to-server http call is always synchronous. Instead of using RequestBuilder, you should make use of a library like HttpClient, get the results and then send it back to the client. That would solve the problem you are facing.

But I should add, you DO NOT want to build a proxy at the application level. You could just as well use a http proxy such as apache's mod_proxy.

sri
Ah, you're correct in this specific case. I'd still like to know in general how you'd handle making a second async call from the service implementation though. (Can't use mod_proxy 'cause this is on App Engine).
Derek Thurn
If you are on GAE, you will have to use `java.net.URL` to make your requests, `httpclient` is not supported.
sri
In general, it is difficult to handle a second async call, because you have to send some response to the client right away. You could spawn a thread and put in some synchronization code, but that would again not work in GAE. The only other way is to send a handle in the first response, and then make a second http request after some time to get the actual data. If you really, really, really need it, make multiple http requests.
sri