You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by ca...@apache.org on 2013/06/04 10:18:22 UTC
svn commit: r1489323 - in /thrift/site: content/docs/idl.md
content/tutorial/java.md publish/docs/idl/index.html publish/index.html
publish/sitemap/index.html publish/tutorial/java/index.html
Author: carl
Date: Tue Jun 4 08:18:22 2013
New Revision: 1489323
URL: http://svn.apache.org/r1489323
Log:
THRIFT-1989: Update Java tutorial and broken links
Modified:
thrift/site/content/docs/idl.md
thrift/site/content/tutorial/java.md
thrift/site/publish/docs/idl/index.html
thrift/site/publish/index.html
thrift/site/publish/sitemap/index.html
thrift/site/publish/tutorial/java/index.html
Modified: thrift/site/content/docs/idl.md
URL: http://svn.apache.org/viewvc/thrift/site/content/docs/idl.md?rev=1489323&r1=1489322&r2=1489323&view=diff
==============================================================================
--- thrift/site/content/docs/idl.md (original)
+++ thrift/site/content/docs/idl.md Tue Jun 4 08:18:22 2013
@@ -188,9 +188,9 @@ Here are some examples of Thrift definit
* [Apache Cassandra's][] Thrift IDL: [cassandra.thrift][]
* [Evernote API][]
- [ThriftTest.thrift]: test/ThriftTest.thrift
- [tutorial]: tutorial/
- [fb303.thrift]: contrib/fb303/if/fb303.thrift
+ [ThriftTest.thrift]: https://git-wip-us.apache.org/repos/asf?p=thrift.git;a=blob_plain;f=test/ThriftTest.thrift;hb=HEAD
+ [tutorial]: /tutorial/
+ [fb303.thrift]: https://git-wip-us.apache.org/repos/asf?p=thrift.git;a=blob_plain;f=contrib/fb303/if/fb303.thrift;hb=HEAD
[Apache Cassandra's]: http://cassandra.apache.org/
[cassandra.thrift]: http://svn.apache.org/viewvc/cassandra/trunk/interface/cassandra.thrift?view=co
[Evernote API]: http://www.evernote.com/about/developer/api/
Modified: thrift/site/content/tutorial/java.md
URL: http://svn.apache.org/viewvc/thrift/site/content/tutorial/java.md?rev=1489323&r1=1489322&r2=1489323&view=diff
==============================================================================
--- thrift/site/content/tutorial/java.md (original)
+++ thrift/site/content/tutorial/java.md Tue Jun 4 08:18:22 2013
@@ -6,13 +6,130 @@ library_lang: "java"
### Prerequisites
+The Java tutorial includes an ant file; running ```ant build``` will bring in the libraries required for running the tutorial.
+
### Client
-<pre><code class="language-java">
+<pre><code class="language-java">
+TTransport transport = new TSocket("localhost", 9090);
+transport.open();
+
+TProtocol protocol = new TBinaryProtocol(transport);
+Calculator.Client client = new Calculator.Client(protocol);
+
+client.ping();
+System.out.println("ping()");
+
+int sum = client.add(1,1);
+System.out.println("1+1=" + sum);
+
+Work work = new Work();
+
+work.op = Operation.DIVIDE;
+work.num1 = 1;
+work.num2 = 0;
+try {
+ int quotient = client.calculate(1, work);
+ System.out.println("Whoa we can divide by 0");
+} catch (InvalidOperation io) {
+ System.out.println("Invalid operation: " + io.why);
+}
+
+work.op = Operation.SUBTRACT;
+work.num1 = 15;
+work.num2 = 10;
+try {
+ int diff = client.calculate(1, work);
+ System.out.println("15-10=" + diff);
+} catch (InvalidOperation io) {
+ System.out.println("Invalid operation: " + io.why);
+}
+
+SharedStruct log = client.getStruct(1);
+System.out.println("Check log: " + log.value);
+
+transport.close();
</code></pre>
### Server
+<pre><code class="language-java">
+CalculatorHandler handler = new CalculatorHandler();
+Calculator.Processor processor = new Calculator.Processor(handler);
+TServerTransport serverTransport = new TServerSocket(9090);
+TServer server = new TSimpleServer(new Args(serverTransport).processor(processor));
+
+System.out.println("Starting the simple server...");
+server.serve();
+</code></pre>
+
+### Server Handler
+
+<pre><code class="language-java">
+public class CalculatorHandler implements Calculator.Iface {
+
+ private HashMap<Integer,SharedStruct> log;
+ public CalculatorHandler() {
+ log = new HashMap<Integer, SharedStruct>();
+ }
+
+ public void ping() {
+ System.out.println("ping()");
+ }
+
+ public int add(int n1, int n2) {
+ System.out.println("add(" + n1 + "," + n2 + ")");
+ return n1 + n2;
+ }
+
+ public int calculate(int logid, Work work) throws InvalidOperation {
+ System.out.println("calculate(" + logid + ", {" + work.op + "," + work.num1 + "," + work.num2 + "})");
+ int val = 0;
+ switch (work.op) {
+ case ADD:
+ val = work.num1 + work.num2;
+ break;
+ case SUBTRACT:
+ val = work.num1 - work.num2;
+ break;
+ case MULTIPLY:
+ val = work.num1 * work.num2;
+ break;
+ case DIVIDE:
+ if (work.num2 == 0) {
+ InvalidOperation io = new InvalidOperation();
+ io.what = work.op.getValue();
+ io.why = "Cannot divide by 0";
+ throw io;
+ }
+ val = work.num1 / work.num2;
+ break;
+ default:
+ InvalidOperation io = new InvalidOperation();
+ io.what = work.op.getValue();
+ io.why = "Unknown operation";
+ throw io;
+ }
+
+ SharedStruct entry = new SharedStruct();
+ entry.key = logid;
+ entry.value = Integer.toString(val);
+ log.put(logid, entry);
+
+ return val;
+ }
+
+ public SharedStruct getStruct(int key) {
+ System.out.println("getStruct(" + key + ")");
+ return log.get(key);
+ }
+
+ public void zip() {
+ System.out.println("zip()");
+ }
+
+}
+</code></pre>
## Additional Information
Modified: thrift/site/publish/docs/idl/index.html
URL: http://svn.apache.org/viewvc/thrift/site/publish/docs/idl/index.html?rev=1489323&r1=1489322&r2=1489323&view=diff
==============================================================================
--- thrift/site/publish/docs/idl/index.html (original)
+++ thrift/site/publish/docs/idl/index.html Tue Jun 4 08:18:22 2013
@@ -273,10 +273,10 @@
<ul>
<li>
-<a href="test/ThriftTest.thrift">ThriftTest.thrift</a> used by the Thrift TestFramework</li>
-<li>Thrift <a href="tutorial/">tutorial</a>
+<a href="https://git-wip-us.apache.org/repos/asf?p=thrift.git;a=blob_plain;f=test/ThriftTest.thrift;hb=HEAD">ThriftTest.thrift</a> used by the Thrift TestFramework</li>
+<li>Thrift <a href="/tutorial/">tutorial</a>
</li>
-<li>Facebook's <a href="contrib/fb303/if/fb303.thrift">fb303.thrift</a>
+<li>Facebook's <a href="https://git-wip-us.apache.org/repos/asf?p=thrift.git;a=blob_plain;f=contrib/fb303/if/fb303.thrift;hb=HEAD">fb303.thrift</a>
</li>
<li>
<a href="http://cassandra.apache.org/">Apache Cassandra's</a> Thrift IDL: <a href="http://svn.apache.org/viewvc/cassandra/trunk/interface/cassandra.thrift?view=co">cassandra.thrift</a>
Modified: thrift/site/publish/index.html
URL: http://svn.apache.org/viewvc/thrift/site/publish/index.html?rev=1489323&r1=1489322&r2=1489323&view=diff
==============================================================================
--- thrift/site/publish/index.html (original)
+++ thrift/site/publish/index.html Tue Jun 4 08:18:22 2013
@@ -150,10 +150,10 @@
</code></pre>
</div>
<div class="tab-pane" id="2">
- <div class="CodeRay"><div class="code"><pre><code class="language-python"> <span style="color:#777"># Make an object</span>
- up = UserProfile(uid=<span style="color:#00D">1</span>,
- name=<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">Test User</span><span style="color:#710">"</span></span>,
- blurb=<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">Thrift is great</span><span style="color:#710">"</span></span>)
+ <div class="CodeRay"><div class="code"><pre><code class="language-python"> <span style="color:#777"># Make an object</span>
+ up = UserProfile(uid=<span style="color:#00D">1</span>,
+ name=<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">Test User</span><span style="color:#710">"</span></span>,
+ blurb=<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">Thrift is great</span><span style="color:#710">"</span></span>)
<span style="color:#777"># Talk to a server via TCP sockets, using a binary protocol</span>
transport = TSocket.TSocket(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">localhost</span><span style="color:#710">"</span></span>, <span style="color:#00D">9090</span>)
@@ -166,11 +166,10 @@
<span style="color:#777"># Retrieve something as well</span>
up2 = service.retrieve(<span style="color:#00D">2</span>)
- </code></pre>
-</div></div></div>
-
-<div class="tab-pane" id="3">
- <div class="CodeRay"><div class="code"><pre><code class="language-c">
+ </code></pre>
+</div>
+<div <span style="color:#080;font-weight:bold">class</span>=<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">tab-pane</span><span style="color:#710">"</span></span> id=<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">3</span><span style="color:#710">"</span></span>>
+ <pre><code <span style="color:#080;font-weight:bold">class</span>=<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">language-c</span><span style="color:#710">"</span></span>>
<span style="color:#080;font-weight:bold">class</span> <span style="color:#B06;font-weight:bold">UserStorageHandler</span> : virtual public UserStorageIf {
public:
UserStorageHandler() {
@@ -197,8 +196,10 @@
shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
TSimpleServer server(processor, serverTransport, transportFactory, protocolFactory);
server.serve();
+ <span style="color:#080;font-weight:bold">return</span> <span style="color:#00D">0</span>;
}
- </code></pre></div></div>
+ </code></pre>
+</div></code></pre></div></div>
</div>
</div>
</div>
Modified: thrift/site/publish/sitemap/index.html
URL: http://svn.apache.org/viewvc/thrift/site/publish/sitemap/index.html?rev=1489323&r1=1489322&r2=1489323&view=diff
==============================================================================
--- thrift/site/publish/sitemap/index.html (original)
+++ thrift/site/publish/sitemap/index.html Tue Jun 4 08:18:22 2013
@@ -75,8 +75,6 @@
<li><a href="/about/">About</a></li>
- <li><a href="/download/">Download</a></li>
-
<li>
<a href="/docs/">Documentation</a><ul>
<li><a href="/docs/BuildingFromSource/">Building From Source</a></li> <li><a href="/docs/concepts/">Concepts</a></li> <li><a href="/docs/features/">Features</a></li> <li><a href="/docs/HowToContribute/">How To Contribute</a></li> <li>
@@ -89,6 +87,8 @@
</ul>
</li>
+ <li><a href="/download/">Download</a></li>
+
<li><span class="active" title="You're here.">Sitemap</span></li>
<li><a href="/developers/">Developers</a></li>
Modified: thrift/site/publish/tutorial/java/index.html
URL: http://svn.apache.org/viewvc/thrift/site/publish/tutorial/java/index.html?rev=1489323&r1=1489322&r2=1489323&view=diff
==============================================================================
--- thrift/site/publish/tutorial/java/index.html (original)
+++ thrift/site/publish/tutorial/java/index.html Tue Jun 4 08:18:22 2013
@@ -83,12 +83,126 @@
<li><p>Followed all prerequesets listed </p></li>
</ol><h3>Prerequisites</h3>
+<p>The Java tutorial includes an ant file; running <code>ant build</code> will bring in the libraries required for running the tutorial.</p>
+
<h3>Client</h3>
-<div class="CodeRay"><div class="code"><pre><code class="language-java"></code></pre></div></div>
+<div class="CodeRay"><div class="code"><pre><code class="language-java">TTransport transport = <span style="color:#080;font-weight:bold">new</span> TSocket(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">localhost</span><span style="color:#710">"</span></span>, <span style="color:#00D">9090</span>);
+transport.open();
+
+TProtocol protocol = <span style="color:#080;font-weight:bold">new</span> TBinaryProtocol(transport);
+Calculator.Client client = <span style="color:#080;font-weight:bold">new</span> Calculator.Client(protocol);
+
+client.ping();
+<span style="color:#0a5;font-weight:bold">System</span>.out.println(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">ping()</span><span style="color:#710">"</span></span>);
+
+<span style="color:#339;font-weight:bold">int</span> sum = client.add(<span style="color:#00D">1</span>,<span style="color:#00D">1</span>);
+<span style="color:#0a5;font-weight:bold">System</span>.out.println(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">1+1=</span><span style="color:#710">"</span></span> + sum);
+
+Work work = <span style="color:#080;font-weight:bold">new</span> Work();
+
+work.op = <span style="color:#0a5;font-weight:bold">Operation</span>.DIVIDE;
+work.num1 = <span style="color:#00D">1</span>;
+work.num2 = <span style="color:#00D">0</span>;
+<span style="color:#080;font-weight:bold">try</span> {
+ <span style="color:#339;font-weight:bold">int</span> quotient = client.calculate(<span style="color:#00D">1</span>, work);
+ <span style="color:#0a5;font-weight:bold">System</span>.out.println(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">Whoa we can divide by 0</span><span style="color:#710">"</span></span>);
+} <span style="color:#080;font-weight:bold">catch</span> (InvalidOperation io) {
+ <span style="color:#0a5;font-weight:bold">System</span>.out.println(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">Invalid operation: </span><span style="color:#710">"</span></span> + io.why);
+}
+
+work.op = <span style="color:#0a5;font-weight:bold">Operation</span>.SUBTRACT;
+work.num1 = <span style="color:#00D">15</span>;
+work.num2 = <span style="color:#00D">10</span>;
+<span style="color:#080;font-weight:bold">try</span> {
+ <span style="color:#339;font-weight:bold">int</span> diff = client.calculate(<span style="color:#00D">1</span>, work);
+ <span style="color:#0a5;font-weight:bold">System</span>.out.println(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">15-10=</span><span style="color:#710">"</span></span> + diff);
+} <span style="color:#080;font-weight:bold">catch</span> (InvalidOperation io) {
+ <span style="color:#0a5;font-weight:bold">System</span>.out.println(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">Invalid operation: </span><span style="color:#710">"</span></span> + io.why);
+}
+
+SharedStruct log = client.getStruct(<span style="color:#00D">1</span>);
+<span style="color:#0a5;font-weight:bold">System</span>.out.println(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">Check log: </span><span style="color:#710">"</span></span> + log.value);
+
+transport.close();</code></pre></div></div>
<h3>Server</h3>
+<div class="CodeRay"><div class="code"><pre><code class="language-java">CalculatorHandler handler = <span style="color:#080;font-weight:bold">new</span> CalculatorHandler();
+Calculator.Processor processor = <span style="color:#080;font-weight:bold">new</span> Calculator.Processor(handler);
+TServerTransport serverTransport = <span style="color:#080;font-weight:bold">new</span> TServerSocket(<span style="color:#00D">9090</span>);
+TServer server = <span style="color:#080;font-weight:bold">new</span> TSimpleServer(<span style="color:#080;font-weight:bold">new</span> Args(serverTransport).processor(processor));
+
+<span style="color:#0a5;font-weight:bold">System</span>.out.println(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">Starting the simple server...</span><span style="color:#710">"</span></span>);
+server.serve();</code></pre></div></div>
+
+<h3>Server Handler</h3>
+
+<div class="CodeRay"><div class="code"><pre><code class="language-java"><span style="color:#088;font-weight:bold">public</span> <span style="color:#339;font-weight:bold">class</span> <span style="color:#B06;font-weight:bold">CalculatorHandler</span> <span style="color:#088;font-weight:bold">implements</span> Calculator.Iface {
+
+ <span style="color:#088;font-weight:bold">private</span> <span style="color:#0a5;font-weight:bold">HashMap</span> log;
+
+ <span style="color:#088;font-weight:bold">public</span> CalculatorHandler() {
+ log = <span style="color:#080;font-weight:bold">new</span> <span style="color:#0a5;font-weight:bold">HashMap</span>();
+ }
+
+ <span style="color:#088;font-weight:bold">public</span> <span style="color:#339;font-weight:bold">void</span> ping() {
+ <span style="color:#0a5;font-weight:bold">System</span>.out.println(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">ping()</span><span style="color:#710">"</span></span>);
+ }
+
+ <span style="color:#088;font-weight:bold">public</span> <span style="color:#339;font-weight:bold">int</span> add(<span style="color:#339;font-weight:bold">int</span> n1, <span style="color:#339;font-weight:bold">int</span> n2) {
+ <span style="color:#0a5;font-weight:bold">System</span>.out.println(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">add(</span><span style="color:#710">"</span></span> + n1 + <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">,</span><span style="color:#710">"</span></span> + n2 + <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">)</span><span style="color:#710">"</span></span>);
+ <span style="color:#080;font-weight:bold">return</span> n1 + n2;
+ }
+
+ <span style="color:#088;font-weight:bold">public</span> <span style="color:#339;font-weight:bold">int</span> calculate(<span style="color:#339;font-weight:bold">int</span> logid, Work work) <span style="color:#088;font-weight:bold">throws</span> InvalidOperation {
+ <span style="color:#0a5;font-weight:bold">System</span>.out.println(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">calculate(</span><span style="color:#710">"</span></span> + logid + <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">, {</span><span style="color:#710">"</span></span> + work.op + <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">,</span><span style="color:#710">"</span></span> + work.num1 + <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">,</span><span style="color:#710">"</span></span> + work.num2 + <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">})</span><span style="color:#710">"</span></span>);
+ <span style="color:#339;font-weight:bold">int</span> val = <span style="color:#00D">0</span>;
+ <span style="color:#080;font-weight:bold">switch</span> (work.op) {
+ <span style="color:#080;font-weight:bold">case</span> ADD:
+ val = work.num1 + work.num2;
+ <span style="color:#080;font-weight:bold">break</span>;
+ <span style="color:#080;font-weight:bold">case</span> SUBTRACT:
+ val = work.num1 - work.num2;
+ <span style="color:#080;font-weight:bold">break</span>;
+ <span style="color:#080;font-weight:bold">case</span> MULTIPLY:
+ val = work.num1 * work.num2;
+ <span style="color:#080;font-weight:bold">break</span>;
+ <span style="color:#080;font-weight:bold">case</span> DIVIDE:
+ <span style="color:#080;font-weight:bold">if</span> (work.num2 == <span style="color:#00D">0</span>) {
+ InvalidOperation io = <span style="color:#080;font-weight:bold">new</span> InvalidOperation();
+ io.what = work.op.getValue();
+ io.why = <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">Cannot divide by 0</span><span style="color:#710">"</span></span>;
+ <span style="color:#080;font-weight:bold">throw</span> io;
+ }
+ val = work.num1 / work.num2;
+ <span style="color:#080;font-weight:bold">break</span>;
+ <span style="color:#080;font-weight:bold">default</span>:
+ InvalidOperation io = <span style="color:#080;font-weight:bold">new</span> InvalidOperation();
+ io.what = work.op.getValue();
+ io.why = <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">Unknown operation</span><span style="color:#710">"</span></span>;
+ <span style="color:#080;font-weight:bold">throw</span> io;
+ }
+
+ SharedStruct entry = <span style="color:#080;font-weight:bold">new</span> SharedStruct();
+ entry.key = logid;
+ entry.value = <span style="color:#0a5;font-weight:bold">Integer</span>.toString(val);
+ log.put(logid, entry);
+
+ <span style="color:#080;font-weight:bold">return</span> val;
+ }
+
+ <span style="color:#088;font-weight:bold">public</span> SharedStruct getStruct(<span style="color:#339;font-weight:bold">int</span> key) {
+ <span style="color:#0a5;font-weight:bold">System</span>.out.println(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">getStruct(</span><span style="color:#710">"</span></span> + key + <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">)</span><span style="color:#710">"</span></span>);
+ <span style="color:#080;font-weight:bold">return</span> log.get(key);
+ }
+
+ <span style="color:#088;font-weight:bold">public</span> <span style="color:#339;font-weight:bold">void</span> zip() {
+ <span style="color:#0a5;font-weight:bold">System</span>.out.println(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">zip()</span><span style="color:#710">"</span></span>);
+ }
+
+}</code></pre></div></div>
+
<h2>Additional Information</h2>
</div>
<div class="container">