This is the RabbitMQ Server Plugin Development Guide. It is expected that before reading this guide, the reader has a basic understanding of the RabbitMQ plugin mechanism. Readers are also expected to have a basic understanding of Erlang OTP system and design principles.
Writing a RabbitMQ plugin provides a number of appealing possibilities:
As with any plugin mechanism, consideration should be given when developing functionality as to whether embedding it as a plugin is the most appropriate path to take. Some reasons that you might not want to develop your functionality as a plugin:
To develop a RabbitMQ plugin, a working RabbitMQ development environment first needs to be configured. An "umbrella" project is now provided to assist in assembling all the necessary repositories. The following steps will walk through the process of checking out and using a copy of the umbrella project in your local environment.
$ hg clone http://hg.rabbitmq.com/rabbitmq-public-umbrella
$ cd rabbitmq-public-umbrella $ make co
$ make BRANCH=<tag> up_c
$ cd rabbitmq-stomp $ makeThis should build the dependencies of STOMP (in this case, this includes the rabbitmq-server sub-project) and then the STOMP adapter. You could also run the STOMP tests (issue make test in the sub-project directory), which should all PASS.
Instead of requiring that developers rebuild plugin archives and re-install them each time a change is made, plugins can be operated in a development-style mode to make it possible to develop plugins in place.
To enable and activate a plugin in a development environment, create a symlink to the build output location of the plugin directory from the rabbitmq-server/plugins directory (which you will have to create if it does not exist already). For example, to enable a development build of the rabbitmq-shovel plugin and its dependencies:
$ mkdir -p rabbitmq-server/plugins $ cd rabbitmq-server/plugins $ ln -s ../../rabbitmq-erlang-client $ ln -s ../../rabbitmq-shovel $ ../scripts/rabbitmq-plugins enable rabbitmq_shovelThe plugin will be activated when the server is restarted.
Conversely, to deactivate a plugin, simply remove the symlink, run rabbitmq-plugins disable plugin-name, and restart the broker instance.
As highlighted in the Administration Guide, badly-written plugins can pose a risk to the stability of the broker. The following tips aim to provide a series of best-practices for ensuring that your plugin can safely co-exist with Rabbit.
Seeing as no development guide would be complete without a Hello World example, the following tries to provide the basics of how your would build your very own RabbitMQ plugin. The following example details how you might build a simple plugin that acts like a metronome. Every second, it fires a message that has a routing key in the form yyyy.MM.dd.dow.hh.mm.ss to a topic exchange called "metronome". Applications can attach queues to this exchange with various routing keys in order to be invoked at regular intervals. For example, to receive a message every second, a binding of "*.*.*.*.*.*.*" could be applied. To recieve a message every minute, a binding of "*.*.*.*.*.*.00" could be applied instead.
The rabbitmq-metronome repository in Mercurial contains a copy of the code for this plugin. This will be checked out for you in the make co step above, or you can check it out separately.
The following table should explain the purpose of the various files in the repository.
| Filename | Purpose |
|---|---|
| Makefile | The top-level umbrella.mk file aims to make your application's Makefile as declarative as possible. Your plugin's Makefile is still a real Makefile however, so you can still do any kind of normal Make tasks. |
| package.mk | Contains various settings used by the umbrella Makefile. |
| src/rabbitmq_metronome.app.src | This file declares the dependencies required by the plugin (rabbit and amqp_client). These will be available and started before starting the plugin. Note that the real app file is generated from this; there is no need to fill in the list of modules. |
| src/rabbit_metronome.erl | Implementation of the Erlang "application" behaviour. Provides a means for the Erlang VM to start and stop the plugin. |
| src/rabbit_metronome_sup.erl | Implementation of the Erlang "supervisor" behaviour. Monitors the worker process and restarts it if it crashes. |
| src/rabbit_metronome_worker.erl | The core of the plugin. The worker will connect internally to the broker, then create a task that will be triggered every second. |
| test/rabbit_metronome_tests.erl | Automated tests for the plugin. |
$ makeNote that the umbrella build system will build the server and the Erlang client first if necessary.
$ mkdir -p rabbitmq-server/plugins $ cd ../rabbitmq-server/plugins $ ln -s ../../rabbitmq-erlang-client $ ln -s ../../rabbitmq-metronome $ ../scripts/rabbitmq-plugins enable rabbitmq_metronome $ cd ..
$ make run
./scripts/rabbitmqctl statusIf your plugin has loaded successfully, you should see an entry in the returned list that looks like:
{rabbitmq_metronome,"Embedded Rabbit Metronome","0.01"}
$ make test
$ make dist