使用Javascript客户端的Thrift Java服务器OutOfMemoryError [英] Thrift Java server OutOfMemoryError with Javascript client

查看:152
本文介绍了使用Javascript客户端的Thrift Java服务器OutOfMemoryError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我昨天开始学习节俭。

经过大量的努力,我成功地执行了Java教程。
Java服务器和客户端运行正常。

After a lot of effort, I successfully executed the Java tutorial. Java server and client are running perfectly.

但是现在,我想要一个javascript客户端与Java Thrift服务器通信。

But now, I want a javascript client to communicate with Java Thrift server.

为此,我将所有js文件移动到 js / 文件夹中。

For that, I moved all js files in js/ folder.

并粘贴index.html代码如下:

And paste index.html code as below:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Thrift Javascript Bindings - Tutorial Example</title>

  <script src="js/thrift.js"  type="text/javascript"></script>
  <script src="js/tutorial_types.js"    type="text/javascript"></script>
  <script src="js/shared_types.js"      type="text/javascript"></script>
  <script src="js/SharedService.js"     type="text/javascript"></script>
  <script src="js/Calculator.js"        type="text/javascript"></script>

  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>


  <script type="text/javascript" charset="utf-8">
  //<![CDATA[
  $(document).ready(function(){
    // remove pseudo child required for valid xhtml strict
    $("#op").children().remove();
    // add operations to it's dropdown menu
    $.each(Operation, function(key, value) {
      $('#op').append($("<option></option>").attr("value",value).text(key)); 
    });

     $('table.calculator').attr('width', 500);
  });

  function calc() {
    var transport = new Thrift.Transport("http://localhost:9090");
    var protocol  = new Thrift.Protocol(transport);
    var client    = new CalculatorClient(protocol);

    var work = new Work()
    work.num1 = $("#num1").val();
    work.num2 = $("#num2").val();
    work.op = $("#op").val();

    try {
      result = client.calculate(1, work);
      $('#result').val(result);
      $('#result').css('color', 'black');
    } catch(ouch){
      $('#result').val(ouch.why);
      $('#result').css('color', 'red');
    }
  }

  function auto_calc() {
    if ($('#autoupdate:checked').val() !== undefined) {
      calc();
    }
  }
  //]]>
  </script>

</head>
<body>
  <h2>Thrift Javascript Bindings</h2>
  <form action="">
  <table class="calculator">
    <tr>
      <td>num1</td>
      <td><input type="text" id="num1" value="20" onkeyup="javascript:auto_calc();"/></td>
    </tr>
    <tr>
      <td>Operation</td>
      <td><select id="op" size="1" onchange="javascript:auto_calc();"><option></option></select></td>
    </tr>
    <tr>
      <td>num2</td>
      <td><input type="text" id="num2" value="5" onkeyup="javascript:auto_calc();"/></td></tr>
    <tr>
      <td>result</td>
      <td><input type="text" id="result" value=""/></td></tr>
    <tr>
      <td><input type="checkbox" id="autoupdate" checked="checked"/>autoupdate</td>
      <td><input type="button" id="calculate" value="calculate" onclick="javascript:calc();"/></td>
    </tr>
  </table>
  </form>

  <p>This Java Script example uses <a href="https://git-wip-us.apache.org/repos/asf?p=thrift.git;a=blob;f=tutorial/tutorial.thrift;hb=HEAD">tutorial.thrift</a> and a Thrift server using JSON protocol and HTTP transport.
  </p>
    <p>
        <a href="http://validator.w3.org/check/referer"><img
            src="http://www.w3.org/Icons/valid-xhtml10"
            alt="Valid XHTML 1.0!" height="31" width="88" /></a>
    </p>
</body>
</html>

我换了行

var transport = new Thrift.Transport("/thrift/service/tutorial/");

with

var transport = new Thrift.Transport("http://localhost:9090");

因为,我的服务器在端口9090上。并且 / thrift / service /教程对我来说没有任何意义,它既不起作用。

As, my server is on port 9090. And /thrift/service/tutorial was not making any sense to me neither it was working.

然后,当我打开我的页面时。

Then, when I open my page.

Java thrift服务器崩溃并出现以下错误:

Java thrift server crashes with following error:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at org.apache.thrift.protocol.TBinaryProtocol.readStringBody(TBinaryProtocol.java:339)
    at org.apache.thrift.protocol.TBinaryProtocol.readMessageBegin(TBinaryProtocol.java:202)
    at org.apache.thrift.TBaseProcessor.process(TBaseProcessor.java:27)
    at org.apache.thrift.server.TSimpleServer.serve(TSimpleServer.java:83)
    at thrift.server.CalculatorServer.main(CalculatorServer.java:23)

这是 CalculatorServer java class。

Here is the CalculatorServer java class.

package thrift.server;

import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TServer.Args;
import org.apache.thrift.server.TSimpleServer;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TServerTransport;
import org.apache.thrift.transport.TTransportException;

import tutorial.Calculator;

public class CalculatorServer {

    public static void main(String[] args) {
        CalculatorHandler handler = new CalculatorHandler();
        Calculator.Processor<CalculatorHandler> processor = new Calculator.Processor<>(handler);        

        try {
            TServerTransport serverTransport = new TServerSocket(9090);
            TServer server = new TSimpleServer(new Args(serverTransport).processor(processor));

            System.out.println("Starting the simple server...");
            server.serve();
        } catch (TTransportException e) {
            e.printStackTrace();
        }
    }

}

其余该代码用于Thrift教程。事实上,Java Thrift服务器与Java Thrift客户端完美配合。我认为Java Server端没有任何问题。

Rest of the code is used from Thrift Tutorials. In fact, Java Thrift server is working perfectly with Java Thrift client. I don't think there is any problem at Java Server side.

有人可以告诉我出了什么问题吗?

Can anyone tell what is going wrong?

Javascript客户端有什么替代方案吗?

Is there any alternative for Javascript client?

我正在使用thrift在我的Java SE应用程序和本地计算机上的网站之间进行通信。所以,我可以使用HTML + CSS + JS开发我的GUI,而不是使用Java Swing。

I am using thrift to communicate between my Java SE application and a website on local computer. So that, I can develop my GUI using HTML+CSS+JS, rather using Java Swing.

推荐答案

我一直在寻找在聚合物javascript中使用thrift的简单方法。最后,这是我用三天时间分析其他解决方案之后的解决方案。他们在哪里工作但他们都需要从我的 http:// localhost:8088 / view中读取view.html本地位置文件.html 请求。我发现这很昂贵,因为我同时使用谷歌应用程序引擎和计算引擎。我无法在app引擎上找到本地目录,所以这就是做了什么。

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. 写你的java服务器喜欢这个

  2. 像这样写你的聚合物元素

  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>

请注意 http:// localhost 可以更改为 http:/ / Your_server_ip_address

请确保java服务器的检查点名称为 service

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

bravo,我们现在只需从javascript Web客户端访问java服务器,而无需提供任何文件路径。

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

请注意,有了这个,你可以在普通的js中轻松使用。像这样

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(); 

干杯。

这篇关于使用Javascript客户端的Thrift Java服务器OutOfMemoryError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆