开发者

Google Web Toolkit Asynchronous Call from a Service Implementation

开发者 https://www.devze.com 2023-01-02 17:56 出处:网络
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

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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消