Demonstrating the use of Apache Avro for serialization in order to interchange data between Perl and Java via RabbitMQ in RPC mode. This code is based on the excellent RPC tutorial by RabbitMQ.
Since one hardly finds any examples of using RabbitMQ in connection with Apache Avro for Perl, I decided to write a small example of using RabbitMQ in RPC mode to interchange data serialized with Avro between Perl and Java.
This tutorial is part of a ''greater'' series using other data-interchange formats such as Google Protocol Buffers and Apache Thrift.
Please skip this section, if you've already installed Gradle, RabbitMQ, Perl including AnyEvent, Net::RabbitFoot, DBD::Mock, and Apache Avro for Perl.
It should be noted, that the following instructions assume Mac OS X to be used as an operating system. The OS X version the installation is tested on is 10.9. Please adapt the commands to satisfy your needs, if needed.
Download Gradle via the following link
https://services.gradle.org/distributions/gradle-1.12-all.zipunpack, and set the desired environment variable. Please replace {username} and {path-to-gradle}:
GRADLE_HOME=/Users/{username}/{path-to-gradle}/gradle-1.12
export GRADLE_HOME
export PATH=$PATH:$GRADLE_HOME/binThe easiest way to install RabbitMQ on Mac OS X is via Homebrew, the ''missing package manager for OS X''. Open a terminal, and install Homebrew as follows:
ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"Next, install RabbitMQ (currently v3.2.4), and add the path to your $PATH variable:
brew update
brew install rabbitmq
export PATH=$PATH:/usr/local/sbinEnable the management plugin (optional):
rabbitmq-plugins enable rabbitmq_managementStart the server:
rabbitmq-serverYou can now browse to http://localhost:15762 in order to monitor your running RabbitMQ instance (if you previously installed the management plugin).
I advice to install Perl via perlbrew. If you don't have a running installation of perlbrew, then just execute the following line in your command line:
\curl -L http://install.perlbrew.pl | bashNext, install a current version of Perl. It should be noted, that 5.16.0 has a bug when compiling the Protobuf definitions for Perl. Hence, you might want to use another version, e.g. 5.18.2:
perlbrew install perl-5.18.2
perlbrew switch perl-5.18.2Now, we need to install some dependencies (use cpan or cpanminus):
cpanm install --notest AnyEvent
cpanm install --notest Net::RabbitFoot
cpanm install --notest DBD::MockPlease note, that there are some errors while running the tests for each of the packages. Thus, we have to use the ''no test'' option.
Download and install the Perl libraries in order to use Avro as follows:
git clone https://github.com/yannk/perl-avro/
cd perl-avro
perl Makefile.PL
make
sudo make installThis section assumes that you've successfully installed RabbitMQ and the necessary Perl libraries.
First, clone the repository:
git clone git://github.com/hopped/rabbitmq-thrift.gitNext, you can build the project using the Gradle build file:
# Current directory is the project root
gradle buildSince I don't have written a suitable Gradle task yet, you have to execute the following commands to run the default client/server scenario (ideally you can run each command in its own shell):
# Current directory is the project root
# (1) Start the RabbitMQ Server
rabbitmq-server
# (2) Start the server written in Perl
cd src/main/perl
perl RPCServer.pl
# (3) Run the client written in Java
gradle runWhat data was actually interchanged? For this example, I wrote some Avro schemas that might be used by a running website such as Strava or SmashRun in order to store runs for users. Let's have a look at 'User.avsc' as an example. All files are kept in src/main/resources.
{
"namespace": "com.hopped.runner.avro",
"name": "User",
"type": "record",
"fields": [
{
"name": "nameOrAlias",
"type": [ "null", "string" ],
"default": null
},
{
"name": "id",
"type": [ "null", "int" ],
"default": null
},
{
"name": "birthdate",
"type": [ "null", "int" ],
"default": null
},
{
"name": "totalDistanceMeters",
"type": [ "null", "double" ],
"default": null
},
{
"name": "eMail",
"type": [ "null", "string" ],
"default": null
},
{
"name": "firstName",
"type": [ "null", "string" ],
"default": null
},
{
"name": "gender",
"type": [ "null", "string" ],
"default": null
},
{
"name": "height",
"type": [ "null", "int" ],
"default": null
},
{
"name": "lastName",
"type": [ "null", "string" ],
"default": null
},
{
"name": "weight",
"type": [ "null", "int" ],
"default": null
}
]
}
Find a bug? Have a feature request? Please create an issue.
Dennis Hoppe
| Date | Version | Comment |
|---|---|---|
| 2014-05-14 | 0.1.0 | Initial release. |
- Test cases
Copyright 2014 Dennis Hoppe.