Hi,
Comments inline.
On Sun, Sep 7, 2008 at 8:49 AM, Mathias Herberts
<[EMAIL PROTECTED]> wrote:
> On Sun, Sep 7, 2008 at 1:29 AM, Fredrik Hedberg
> <[EMAIL PROTECTED]> wrote:
>> Hi,
>>
>> As it happens, I have also just this evening spent some time working
>> with GWT and Thrift. What I wanted to do was to consume Thrift
>> services from GWT using JSON. Here's what I've done.
>
> Hi,
>
> I guess this was the *idea in the air* yesterday night :-)
>
>> 1) Created a stripped down jar of Thrift, axed most protocol,
>> transport and server implementations, in order to get a
>> JavaScript-translatable version of Thrift. I did not need to change
>> any of the base Thrift classes, nor modify the compiler for GWT to
>> translate the structs, but I might have missed something here
>> (Mathias?).
>
> My approach was driven by the non translatable status of theTxxx
> classes. I wanted to reduce the complexity of the classes GWT would
> have to translate and use on the JS side, I guess I should have tried
> your way too as my approach needs my conversion class which makes
> heavy use of reflection and is probably a CPU hog in the pipe.
>
>> 2) Added an option for the Thrift Java compiler to generate
>> asynchronous service interfaces and client proxies. This is manifested
>> as:
>>
>> public class Repository {
>> public interface Iface {
>> public Document get_document(String uri) throws TException;
>> public int get_count() throws TException;
>> }
>>
>> public interface AsyncIface {
>> public void get_document(String uri, TAsyncCallback<Document>
>> callback) throws TException;
>> public void get_count(TAsyncCallback<Integer> callback) throws TException;
>> }
>
> Why use TAsyncCallback and not the regular callback GWT provides?
So that the asynchronous interfaces could be used outside of GWT if need be.
>
>> This is done inline with GWT's RPC framework and gives the developer
>> the standard synchronous interface to implement on the server side (I
>> use it with embedded Jetty in a daemon) and an asynchonous interface
>> to use in the GWT client. TAsyncCallback<T> just has a plain
>> onSuccess(T result) method.
>>
>> 3) Implemented a client transport using GWT's RequestBuilder (the
>> XmlHttpRequest abstraction) that executes the TAsyncCallback
>> asynchronously when the response has been received.
>>
>> 4) Modified the JSONProtocol slightly to be fully
>> JavaScript-translatable. This could probably be more efficiently done
>> by using GWT's JSNI framework, but I really haven't had the time to
>> optimize anything yet.
>
> Would your approach actually work with the standard GWT serialization?
Actually, GWT serialization isn't used in my version, as I don't use
GWT RPC at all. I guess another way of doing it would be to modify the
generated struct to be serializable, as you did, and use the GWT RPC
framwork to proxy request on the server side to the Thrift service.
This would maybe perform better on the client-side, but would incur a
lot more work and complexity on the Thrift modifications required, and
also be less flexible and less performing on the server side. This
way, any Thrift protocol that is compatible with the JRE emulation
library in GWT could be used.
That being said, the best way of doing it would probably be to write a
Java client library from scratch, and compiler mode, for use in GWT
apps, as well as a JSON protocol (for the server) that is directly
translatable into JavaScript native objects using JSNI, but that's a
lot of work ;).
>
> Did you also have a look at the translated JS code? Isn't it too
> cluttered with useless stuff (that was my initial worry)?
No, I did not look at the code. But it probably is ;)
>
> Anyway I'm glad I'm not alone fiddling with that stuff.
>
> Mathias.
>
Fredrik