Thrift Java server OutOfMemoryError with Javascript client

£可爱£侵袭症+ 提交于 2019-12-06 02:47:18

I have been looking for an easy way to use thrift inside polymer javascript. Finally here is the solution after It took me three days analyzing other solutions. They where working but they all needed to read the view.html local location file from my http://localhost:8088/view.html request. I found this so expensive since i am using google app engine and compute engine at the same time. i cant locate the local directory on app engine so here is what is did.

  1. Write your java server pretty much like this.
  2. write your polymer element like this

    <dom-module id="thrift-client">
    <template>
    </template>
    <script>
        Polymer({
            is:'thrift-client',
            ready:function(){
                var transport = new Thrift.TXHRTransport("http://localhost:8088/service");
    
                var protocol  = new Thrift.TJSONProtocol(transport);
    
                var client = new NtvApiClient(protocol);
    
                var result = client.ping(); // ping the java server. custom
    
                console.log(result);
            }
    
            });
    </script>
    

  3. Note that the http://localhost can be changed to http://Your_server_ip_address.

  4. please make sure the java server has a check point exactly named as service

  5. bravo, we have all it takes to access the java server now from a javascript web client without providing any file path.

  6. Note that with this u can easily use in plain js. like this

                    var transport = new Thrift.TXHRTransport("http://localhost:8088/service");
                    var protocol  = new Thrift.TJSONProtocol(transport);
                    var client = new NtvApiClient(protocol);
                    var result = client.ping(); 
    

    Cheers.

Default 'TTransport' is not a http protocol based server. If you want to use http as the communication protocol which seems the only way for js at the client side, you should use a http based 'TTransport'. Take a look at 'org.apache.thrift.server.TServlet', it's a simple way to build a http based thrift server in a servlet.

You should also use 'TJSONProtocol' instead of 'TBinaryProtocol' at server side because thrift js can NOT handle binary data well. This is the code snippet I used before.

public class CalculatorServlet extends TServlet 
{
   public CalculatorServlet() 
   {
      //'Calculator' is the generated class from thrift, 
      //'CalculatorHandler' is the implement class for thrift rpc
      super(new Calculator.Processor(
            new CalculatorHandler()),
            new TJSONProtocol.Factory());
  }
}

IIRC, JavaScript supports only the JSON protocol. This is (of course) incompatible with binary.

Therefore the solution is: use the JSON protocol instead of the binary protocol in your server.

A good rule of thumb with Thrift is, to always use the same transport/protocol stack on both ends. In this particilar case this means:

  • HTTP Server
  • JSON protocol
  • no extras like framed protocol (buffered would work, btw)

(There are some exceptions, because some server types intrinsically require framed, but that's a whole other story and has nothing to do with your problem)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!