Apache Thrift, RPC’s between Java(server)-PHP(client), Hello World Application

A more comprehensive tutorial can be found here.
HOW TO INSTALL:
type(for the pre-requisites): 
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_glib

For the final installation, download the tarball from the website, http://thrift.apache.org/download/
Then to install the tarball: 

tar -xvf /path/to/tarball
cd /path/to/extraction
./configure
sudo make
sudo make install

Also install the eclipse editor for thrift files.
Eclipse –> help –> Install new Software –>
add the URL: http://thrift4eclipse.sourceforge.net/updatesite/
tick the only package shown and install it.

Compiling the REQUIRED LIBRARIES (for the different languages that has to be supported):

  • for JAVA
    Go to folder /path/to/thrift-version/folder/lib/java/
    execute the command “ant” – compiles using apache ant
    Now the build folder contains all the lib files required.
  • for PHP
    No need for compiling any files, php is used in its raw form.

Making the thrift file:

Tutorial can be found here : http://diwakergupta.github.com/thrift-missing-guide/
Thrift file will include all the services and structures shared between the two languages.

Start with:

namespace java package-name

Making the JAVA server:

Make a new project in Eclipse with type, “Dynamic Web Project”.
Put the “thrift file” in the <project-name>/Java Resources/src/ folder.
Copy the lib files (libthrift-<version>.jar, build/lib/*) to <project-name>/WebContent/WEB-INF/lib/ folder.

Generate the auto-generated java files from the file using the command:

cd path/to/thrift-file/
thrift --gen java -out thrift-file-name

Now we have to implement the services mentioned in the thrift-file by:

  • make a new file in the same package <package-name>.
  • Wrie a class <service-implement> implementing <service-name>.Iface (like this implement all the services)

Now we have to make the server file:

public class server_name implements Runnable {
	/* port to listen */
	private static final int PORT = 9090;
	public void run() {
		try {
			TServerSocket serverTransport = new TServerSocket(PORT);
			HelloService.Processor processor = new HelloService.Processor(new ());
			TServer server = new TThreadPoolServer(new TThreadPoolServer.Args(serverTransport).processor(processor));
			System.out.println("Starting server on port: "+PORT);
			server.serve();
		} catch(TTransportException e) {
			System.out.println("Message: "+e.getMessage());
			System.out.println("StackTrace: ");
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		new Thread(new server_name()).run();
	}
}
Run the server as a java application. This completes the making of the server.
NOTE: To stop the server you’ll need to kill the process via the console.
Making the PHP client:
First auto-generate the PHP package from the thrift file already created using the command:
cd path/to/thrift-file/
thrift --gen php thrift-file-name

Create a new directory named “thrift” 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 “packages” in “thrift” directory, and copy the auto-generated PHP package here.

Create a new file <client-file>.php adjacent to the “thrift” directory.
Contents of the PHP file will be:

// defining the port and server to listen
define("PORT", '9090');
define("SERVER", 'localhost');

//Global variable where the php library files are stored
$GLOBALS['THRIFT_ROOT'] = 'thrift';

//including the library files
require_once $GLOBALS['THRIFT_ROOT'].'/Thrift.php';
require_once $GLOBALS['THRIFT_ROOT'].'/protocol/TBinaryProtocol.php';
require_once $GLOBALS['THRIFT_ROOT'].'/transport/TSocket.php';
require_once $GLOBALS['THRIFT_ROOT'].'/transport/TBufferedTransport.php';

//loading the auto-generated package
require_once $GLOBALS['THRIFT_ROOT'].'/packages/hello/HelloService.php';

try {
	//create a thrift connection
	$socket = new TSocket(SERVER, PORT);
	$transport = new TBufferedTransport($socket);
	$protocol = new TBinaryProtocol($transport);
	
	//create a new hello service client
	$client = new HelloServiceClient($protocol);
	
	//open the connection
	$transport->open();
	
	//calling the service
	$result = $client->sayHello();
	echo "Result: ".$result;
	
	//closing the connection
	$transport->close();
} catch(TException $tx) {
	echo "Thrift Exception: ".$tx->getMessage()."\r\n";
}
To TEST:
Run the JAVA server.
            CONSOLE: “Starting server on port: 9090”
Run the <client-file>.php using the command: php5 client.php
            CONSOLE: “Result: HelloWorld!!”
Finally make a directory “client” and copy the client related files here. Also, make a new directory names “server”, 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).

Leave a Reply

Your email address will not be published. Required fields are marked *