{"id":18,"date":"2012-11-29T14:10:00","date_gmt":"2012-11-29T14:10:00","guid":{"rendered":"http:\/\/digital-madness.in\/blog\/2012\/%month%\/apache-thrift-rpcs-between-javaserver-phpclient-hello-world-application\/"},"modified":"2018-02-17T17:59:37","modified_gmt":"2018-02-17T17:59:37","slug":"apache-thrift-rpcs-between-javaserver-phpclient-hello-world-application","status":"publish","type":"post","link":"http:\/\/digital-madness.in\/blog\/2012\/apache-thrift-rpcs-between-javaserver-phpclient-hello-world-application\/","title":{"rendered":"Apache Thrift, RPC&#8217;s between Java(server)-PHP(client), Hello World Application"},"content":{"rendered":"<div dir=\"ltr\">A more comprehensive tutorial can be found <a href=\"http:\/\/digital-madness.in\/blog\/wp-content\/uploads\/2012\/11\/BSD_08_2013.8-18.pdf\" target=\"_blank\" rel=\"noopener\">here<\/a>.<\/div>\n<div dir=\"ltr\">Code can be downloaded from:\u00a0<a href=\"https:\/\/github.com\/chirag1992m\/apacheThriftHelloWorld\">https:\/\/github.com\/chirag1992m\/apacheThriftHelloWorld<\/a><\/div>\n<div dir=\"ltr\"><\/div>\n<div dir=\"ltr\"><strong><span style=\"text-decoration: underline;\" data-mce-mark=\"1\">HOW TO INSTALL:<\/span><\/strong><\/div>\n<div dir=\"ltr\"><\/div>\n<div dir=\"ltr\"><b>type(for the pre-requisites):\u00a0<\/b><\/div>\n<div dir=\"ltr\">\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsudo apt-get install libboost-dev libboost-test-dev libboost-program-options-dev libevent-dev automake libtool flex bison pkg-config g++ libssl-dev\r\nsudo apt-get install php5-dev php5-cli #for php\r\nsudo apt-get install libglib2.0-dev #for c_glib<\/pre><p>For the final installation, download the tarball from the website,\u00a0<span style=\"text-decoration: underline;\" data-mce-mark=\"1\"><a href=\"http:\/\/thrift.apache.org\/download\/\">http:\/\/thrift.apache.org\/download\/<br \/>\n<\/a><\/span><span style=\"font-family: Georgia, 'Times New Roman', 'Bitstream Charter', Times, serif; font-size: 13px; line-height: 19px;\" data-mce-mark=\"1\">Then to install the tarball:\u00a0<\/span><\/p>\n<div>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">tar -xvf \/path\/to\/tarball\r\ncd \/path\/to\/extraction\r\n.\/configure\r\nsudo make\r\nsudo make install<\/pre><p>Also install the eclipse editor for thrift files.<br \/>\nEclipse &#8211;&gt; help &#8211;&gt; Install new Software &#8211;&gt;<br \/>\nadd the URL:\u00a0<a href=\"http:\/\/thrift4eclipse.sourceforge.net\/updatesite\/\">http:\/\/thrift4eclipse.sourceforge.net\/updatesite\/<\/a><br \/>\ntick the only package shown and install it.<\/p><p><strong><span style=\"text-decoration: underline;\" data-mce-mark=\"1\">Compiling the REQUIRED LIBRARIES (for the different languages that has to be supported):<\/span><\/strong><\/p>\n<ul>\n<li><em><strong>for<\/strong><\/em><strong> JAVA<\/strong><strong><br \/>\n<\/strong><em>Go to folder<\/em> \/path\/to\/thrift-version\/folder\/lib\/java\/<br \/>\nexecute the command \u201cant\u201d &#8211; compiles using\u00a0apache ant<br \/>\nNow the build folder contains all the lib files required.<\/li>\n<li><em><strong>for<\/strong><\/em><strong>\u00a0PHP<br \/>\n<\/strong>No need for compiling any files, php is used in its raw form.<strong><br \/>\n<\/strong><\/li>\n<\/ul><p><strong><span style=\"text-decoration: underline;\" data-mce-mark=\"1\">Making the thrift file:<\/span><\/strong><\/p><p>Tutorial can be found here :\u00a0<a href=\"http:\/\/diwakergupta.github.com\/thrift-missing-guide\/\">http:\/\/diwakergupta.github.com\/thrift-missing-guide\/<\/a><br \/>\nThrift file will include all the services and structures shared between the two languages.<\/p><p>Start with:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">namespace java package-name<\/pre><p><strong><span style=\"text-decoration: underline;\" data-mce-mark=\"1\">Making the JAVA server:<\/span><\/strong><\/p>\n<\/div>\n<div>Make a new project in Eclipse with type, \u201cDynamic Web Project\u201d.<\/div>\n<div>Put the \u201cthrift file\u201d in the &lt;project-name&gt;\/Java Resources\/src\/ folder.<br \/>\nCopy the lib files (libthrift-&lt;version&gt;.jar, build\/lib\/*) to &lt;project-name&gt;\/WebContent\/WEB-INF\/lib\/ folder.<\/div>\n<div><p>Generate the auto-generated java files from the file using the command:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">cd path\/to\/thrift-file\/\r\nthrift --gen java -out thrift-file-name\r\n<\/pre><p>Now we have to implement the services mentioned in the thrift-file by:<\/p>\n<\/div>\n<div>\n<ul>\n<li>make a new file in the same package &lt;package-name&gt;.<\/li>\n<li>Wrie a class &lt;service-implement&gt; implementing &lt;service-name&gt;.Iface (like this implement all the services)<\/li>\n<\/ul><p><em><strong>Now we have to make the server file:<\/strong><\/em><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class server_name implements Runnable {\r\n\t\/* port to listen *\/\r\n\tprivate static final int PORT = 9090;\r\n\tpublic void run() {\r\n\t\ttry {\r\n\t\t\tTServerSocket serverTransport = new TServerSocket(PORT);\r\n\t\t\tHelloService.Processor processor = new HelloService.Processor(new ());\r\n\t\t\tTServer server = new TThreadPoolServer(new TThreadPoolServer.Args(serverTransport).processor(processor));\r\n\t\t\tSystem.out.println(&quot;Starting server on port: &quot;+PORT);\r\n\t\t\tserver.serve();\r\n\t\t} catch(TTransportException e) {\r\n\t\t\tSystem.out.println(&quot;Message: &quot;+e.getMessage());\r\n\t\t\tSystem.out.println(&quot;StackTrace: &quot;);\r\n\t\t\te.printStackTrace();\r\n\t\t}\r\n\t}\r\n\r\n\tpublic static void main(String&#x5B;] args) {\r\n\t\tnew Thread(new server_name()).run();\r\n\t}\r\n}\r\n<\/pre>\n<div align=\"LEFT\">Run the server as a java application.\u00a0This <em>completes<\/em> the making of the server.<\/div>\n<div align=\"LEFT\"><strong>NOTE: To stop the server you&#8217;ll need to kill the process via the console.<\/strong><\/div>\n<div align=\"LEFT\"><\/div>\n<div style=\"margin-top: 10px;\" align=\"LEFT\"><strong><span style=\"text-decoration: underline;\" data-mce-mark=\"1\">Making the PHP client:<\/span><\/strong><\/div>\n<div align=\"LEFT\"><\/div>\n<div align=\"LEFT\">First auto-generate the PHP package from the thrift file already created using the command:<\/div>\n<div align=\"LEFT\">\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">cd path\/to\/thrift-file\/\r\nthrift --gen php thrift-file-name\r\n<\/pre><p>Create a new directory named &#8220;thrift&#8221; and copy all the php library files available in the directory \/path\/to\/thrift-version-folder\/lib\/php\/src\/ to the newly created directory. Also create a new sub-directory named &#8220;packages&#8221; in &#8220;thrift&#8221; directory, and copy the auto-generated PHP package here.<\/p><p>Create a new file &lt;client-file&gt;.php adjacent to the &#8220;thrift&#8221; directory.<br \/>\nContents of the PHP file will be:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n\/\/ defining the port and server to listen\r\ndefine(&quot;PORT&quot;, '9090');\r\ndefine(&quot;SERVER&quot;, 'localhost');\r\n\r\n\/\/Global variable where the php library files are stored\r\n$GLOBALS&#x5B;'THRIFT_ROOT'] = 'thrift';\r\n\r\n\/\/including the library files\r\nrequire_once $GLOBALS&#x5B;'THRIFT_ROOT'].'\/Thrift.php';\r\nrequire_once $GLOBALS&#x5B;'THRIFT_ROOT'].'\/protocol\/TBinaryProtocol.php';\r\nrequire_once $GLOBALS&#x5B;'THRIFT_ROOT'].'\/transport\/TSocket.php';\r\nrequire_once $GLOBALS&#x5B;'THRIFT_ROOT'].'\/transport\/TBufferedTransport.php';\r\n\r\n\/\/loading the auto-generated package\r\nrequire_once $GLOBALS&#x5B;'THRIFT_ROOT'].'\/packages\/hello\/HelloService.php';\r\n\r\ntry {\r\n\t\/\/create a thrift connection\r\n\t$socket = new TSocket(SERVER, PORT);\r\n\t$transport = new TBufferedTransport($socket);\r\n\t$protocol = new TBinaryProtocol($transport);\r\n\t\r\n\t\/\/create a new hello service client\r\n\t$client = new HelloServiceClient($protocol);\r\n\t\r\n\t\/\/open the connection\r\n\t$transport-&gt;open();\r\n\t\r\n\t\/\/calling the service\r\n\t$result = $client-&gt;sayHello();\r\n\techo &quot;Result: &quot;.$result;\r\n\t\r\n\t\/\/closing the connection\r\n\t$transport-&gt;close();\r\n} catch(TException $tx) {\r\n\techo &quot;Thrift Exception: &quot;.$tx-&gt;getMessage().&quot;\\r\\n&quot;;\r\n}\r\n<\/pre>\n<div align=\"LEFT\"><strong><span style=\"text-decoration: underline;\">To TEST:<\/span><\/strong><\/div>\n<div align=\"LEFT\"><\/div>\n<div align=\"LEFT\">Run the JAVA server.<\/div>\n<div align=\"LEFT\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 CONSOLE: &#8220;Starting server on port: 9090&#8221;<\/div>\n<div align=\"LEFT\"><\/div>\n<div align=\"LEFT\">Run the &lt;client-file&gt;.php using the command: php5 client.php<\/div>\n<div align=\"LEFT\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 CONSOLE: &#8220;Result: HelloWorld!!&#8221;<\/div>\n<div align=\"LEFT\"><\/div>\n<div align=\"LEFT\">Finally make a directory &#8220;client&#8221; and copy the client related files here. Also, make a new directory names &#8220;server&#8221;, copy all the java server files here. So we have a simple apache thrift application making a bridge between between java(server) and php(client).<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>A more comprehensive tutorial can be found here. Code can be downloaded from:\u00a0https:\/\/github.com\/chirag1992m\/apacheThriftHelloWorld HOW TO INSTALL: type(for the pre-requisites):\u00a0 sudo apt-get install libboost-dev libboost-test-dev libboost-program-options-dev libevent-dev automake libtool flex bison pkg-config g++ libssl-dev sudo apt-get install php5-dev php5-cli #for php sudo apt-get install libglib2.0-dev #for c_glibFor the final installation, download the tarball from the website,\u00a0http:\/\/thrift.apache.org\/download\/&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_s2mail":"","footnotes":""},"categories":[41],"tags":[7,11,8,12,9,3,10],"class_list":["post-18","post","type-post","status-publish","format-standard","hentry","category-inter-process-communication-ipc","tag-apache-thrift-2","tag-java","tag-java-server-2","tag-php","tag-php-client","tag-rpc","tag-ubuntu"],"aioseo_notices":[],"_links":{"self":[{"href":"http:\/\/digital-madness.in\/blog\/wp-json\/wp\/v2\/posts\/18","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/digital-madness.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/digital-madness.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/digital-madness.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/digital-madness.in\/blog\/wp-json\/wp\/v2\/comments?post=18"}],"version-history":[{"count":45,"href":"http:\/\/digital-madness.in\/blog\/wp-json\/wp\/v2\/posts\/18\/revisions"}],"predecessor-version":[{"id":196,"href":"http:\/\/digital-madness.in\/blog\/wp-json\/wp\/v2\/posts\/18\/revisions\/196"}],"wp:attachment":[{"href":"http:\/\/digital-madness.in\/blog\/wp-json\/wp\/v2\/media?parent=18"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/digital-madness.in\/blog\/wp-json\/wp\/v2\/categories?post=18"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/digital-madness.in\/blog\/wp-json\/wp\/v2\/tags?post=18"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}