Archive

Archive for the ‘Programming’ Category

Thrift Tutorial on Ubuntu 11.10

February 10, 2012 Leave a comment

What is Thrift and how can it help you and your team build a highly scalable platform for web services? According to its project page [1], “Thrift is a software framework for scalable cross-language services development. It combines a software stack with a code generation engine to build services that work efficiently and seamlessly between C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node.js, Smalltalk, and OCaml.” Although Facebook open sourced it in April 2007, its documentation could be improved to make it easier to try out and start building prototypes. Even the project’s tutorial page is still a work-in-progress. So, here’s my version of such, specifically for Java on Ubuntu 11.10. I hope you find it useful.

Get Thrift

First, make sure your system meets the requirements specified at [2]. Here’s how you can install the required packages on Ubuntu 11.10.

$ sudo apt-get update
$ sudo apt-get install libboost-dev libboost-test-dev libboost-program-options-dev libevent-dev automake libtool flex bison pkg-config g++ libssl-dev

Then, install the Java OpenJDK and ant.

$ sudo apt-get install openjdk-6-jdk ant

Now you can get Thrift. I chose to checkout Thrift from the Apache SVN repository.

$ cd ~/svn
$ svn co http://svn.apache.org/repos/asf/thrift/trunk thrift

You can also simply download the stable, snapshot or archived releases of Thrift from [3].

Build and Install

Now, you’re ready to build and install Thrift on your system.

$ cd thrift
$ ./bootstrap.sh
$ ./configure
$ make
$ sudo make install
$ thrift -version

Your First Thrift File

The tutorial directory, thrift/tutorial, contains the files tutorial.thrift and shared.thrift. Cleverly, the tutorial.thrift file teaches you Thrift in a .thrift file! Read those files and start learning.

Build for Java

At this point, you’re probably itching to see something running. For this tutorial, I chose the Java example. The tutorial directory, thrift/tutorial, contains examples under each language’s directory. But, before you can run the Java example, you have to compile the Java library first.

$ cd ~/svn/thrift/lib/java
$ ant

Now, you get to see the code generation engine at work.

Using The Thrift Compiler

$ cd ~/svn/thrift/tutorial

The following command tells Thrift to generate Java code. The -r option tells it to also generate included files.

$ thrift -r –gen java tutorial.thrift

After the command runs, you should see a directory called gen-java. If you look under that directory, you should see two directories: shared and tutorial.

Compile The Example

Now that the code has been generated, you can compile the Java example.

$ cd ~/svn/thrift/tutorial/java
$ ant

Running A Thrift Server

Now, for what you’ve been waiting for, a working example of a client/server application working through Thrift!

$ ./JavaServer &
Starting the simple server…
Starting the secure server…

Running A Thrift Client

$ ./JavaClient simple

If all is well, you should see something like the following:

ping()
1+1=2
Whoa we can divide by 0
15-10=5
Check log: 5

Other Languages

Make sure to try out the other examples for the other languages under each respective directory. You may need to install additional packages to support other languages. Please refer to [5].

References:

  1. http://thrift.apache.org/
  2. http://wiki.apache.org/thrift/ThriftRequirements
  3. http://thrift.apache.org/download/
  4. http://wiki.apache.org/thrift/ThriftInstallation
  5. http://wiki.apache.org/thrift/GettingUbuntuPackages

How To Install RabbitMQ Server and pika on Ubuntu 10.10

RabbitMQ provides a messaging mechanism for applications, based on a protocol called AMQP. pika is one of the python libraries that understands AMQP.

Here are the steps I took:

Install or upgrade python

$ sudo apt-get install python

Install python-pip and git-core, since the pika installation depends on these packages.

$ sudo apt-get install python-pip git-core

Install the RabbitMQ server package

$ sudo apt-get install rabbitmq-server

Install pika using the pip package management tool

$ sudo pip install pika==0.5.2

Create the code for the sender application, send.py

$ vi send.py


#!/usr/bin/env python

import pika

connection = pika.AsyncoreConnection(pika.ConnectionParameters(host='localhost', credentials=pika.PlainCredentials('guest', 'guest')))

channel = connection.channel()

channel.queue_declare(queue='test')

channel.basic_publish(exchange='',routing_key='test',body='Hello World!')

print " [x] Sent 'Hello World!'"

Create the code for the receiver application, receive.py

$ vi receive.py

#!/usr/bin/env python

import pika

connection = pika.AsyncoreConnection(pika.ConnectionParameters(host='127.0.0.1',credentials=pika.PlainCredentials('guest', 'guest')))

channel = connection.channel()

channel.queue_declare(queue='test')

print ' [*] Waiting for messages. To exit press CTRL+C'

def callback(ch, method, header, body):
    print " [x] Received %.20r" % (body,)

channel.basic_consume(callback,queue='test',no_ack=True)

pika.asyncore_loop()

Check that RabbitMQ is running.

$ sudo rabbitmqctl status

Status of node ‘rabbit@web-server’ …

[{running_applications,[{rabbit,”RabbitMQ”,”1.8.0″},

{mnesia,”MNESIA  CXC 138 12″,”4.4.12″},

{os_mon,”CPO  CXC 138 46″,”2.2.4″},

{sasl,”SASL  CXC 138 11″,”2.1.8″},

{stdlib,”ERTS  CXC 138 10″,”1.16.4″},

{kernel,”ERTS  CXC 138 10″,”2.13.4″}]},

{nodes,[‘rabbit@web-server’]},

{running_nodes,[‘rabbit@web-server’]}]

…done.

Send a message in one window.

$ python send.py

[x] Sent ‘Hello World!’

List the queues and see how many messages are in them.

$ sudo rabbitmqctl list_queues

Listing queues …

test1

…done.

Receive a message in another window.

$ python receive.py

[*] Waiting for messages. To exit press CTRL+C

[x] Received ‘Hello World!’

List the queues again.

$ Listing queues …

test0

…done.

Kill the receive.py

– Use CTRL+C to interrupt and kill the receiver process

Enjoy!

References:

1. RabbitMQ Python & Pika Tutorial

http://www.rabbitmq.com/tutorials/tutorial-one-python.html

2. AMQP

http://www.amqp.org/confluence/display/AMQP/About+AMQP

Categories: Programming, Ubuntu Tags: , , ,
Design a site like this with WordPress.com
Get started