我昨天开始学习节俭.
经过大量的努力,我成功地执行了Java教程.
Java服务器和客户端运行良好.
但是现在,我希望一个javascript客户端与Java Thrift服务器进行通信.
为此,我将所有js文件移到js /文件夹中.
并粘贴index.html代码,如下所示:
<!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/");
与
var transport = new Thrift.Transport("http://localhost:9090");
因为,我的服务器位于端口9090上,所以/ thrift / service / tutorial对我来说没有任何意义,也不起作用.
然后,当我打开页面时.
Java节俭服务器崩溃,出现以下错误:
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类.
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方面没有任何问题.
谁能告诉我出了什么问题?
Javascript客户端还有其他选择吗?
我正在使用节俭在Java SE应用程序和本地计算机上的网站之间进行通信.这样,我可以使用HTML CSS JS而不是Java Swing开发GUI.
解决方法:
我一直在寻找一种在聚合物javascript中使用节俭的简单方法.最后,这是我花了三天时间分析其他解决方案之后的解决方案.他们在工作,但他们都需要从我的http://localhost:8088/view.html请求中读取view.html本地文件.我发现这太昂贵了,因为我同时使用了Google App Engine和计算引擎.我无法在应用程序引擎上找到本地目录,所以这是做什么的.
>编写您的Java服务器差不多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的检查点
> bravo,我们现在拥有从javascript网络客户端访问java服务器而无需提供任何文件路径的全部能力.
>请注意,您可以轻松地在纯js中使用它.像这样
var transport = new Thrift.TXHRTransport("http://localhost:8088/service");
var protocol = new Thrift.TJSONProtocol(transport);
var client = new NtvApiClient(protocol);
var result = client.ping();
干杯.