Over the past year and a half,
RabbitMQ Queue Server, which operates under the AMQP protocol, has been gaining popularity. About this protocol there were already enough articles on Habré. The toolkit has a library
librabbitmqOn the basis of this library, I put the amqpcpp C ++ library project into
Google Code , which is a simplified interface to librabbitmq. There is no documentation, only three examples of use in the distribution. Under custom, a summary of the API and examples of its use. Use made easier.
Installation
very simple: run the makefile into which the compile library libamqpcpp.a is compiled
in the same makefile there is a compilation of examples:
g ++ -o example_get -lamqpcpp -lrabbitmq -Iamqpcpp -I / usr / local / include -L / usr / local / lib -L. example_get.cpp
replace the name example_get with my_project and compile. While configure is not done, I will be glad - if someone helps me to create it.
Brief description of the API
The library turns on
#include "amqpcpp.h"
')
All public methods are listed in the
project wiki . Naming convention:
All methods that are defined in the AMQP protocol begin with a capital letter , for example: Declare, Bind, Consume, Cancel etc.
The Amqp class carries two functions:
- connects to a broker
- is a factory for creating instances of the “queue” and “exchanges” classes and transferring the connection to them.
For each instance of the class "queue" or "exchange" opens a new channel. The channel is closed automatically when the class destructor is called.
By default, a connection is made to the broker on vhost = / login = guest password = guest port = 5672
The connection parameters are passed in a string (connectionString) of the following form: "password: login @ host: port / vhost". This is a well-established standard. If any parameter is missing, it is replaced by the default parameter.
If there is no connection, then you can print the connection parameters using the printConnect () method.
AMQP amqp;
amqp->printConnect();
./test
connect
port =5672
host =localhost
vhost=/
user =guest
passw=guest
// amqp-proxy-debug
AMQP amqp(AMQPDEBUG);
amqp->printConnect();
./test
connect
port =5673
host =localhost
vhost=/
user =guest
passw=guest
//
AMQP amqp("123123:akalend@10.0.0.2/private");
./test
connect
port =5672
host =10.0.0.2
vhost=private
user =akalend
passw=123123
Exchange
An instance of the AMQPExchange exchange class can be obtained from the AMQP class using the createExchange () method.
This method may or may not have a parameter - the name of the exchange. To avoid leaks - it is proposed to use auto_ptr
AMQP amqp ;
auto_ptr < AMQPExchange > ex ( amqp. createExchange ( "ex" ) ) ;
ex - > Declare ( "e" , "topic" ) ;
To carry out operations with the Exchange - it must be announced. You can declare an exchange in two ways: either specify the name of the exchange when you create it, or when announcing it:
AMQP amqp ;
auto_ptr < AMQPExchange > ex ( amqp. createExchange ( "ex" ) ) ;
ex - > Declare ( ) ;
//
auto_ptr < AMQPExchange > ex ( amqp. createExchange ( ) ) ;
ex - > Declare ( "e" , "topic" ) ;
The second parameter of the AMQPExchange :: Declare () method is the exchange type, a string value. Exchanges can be of three types:
- Direct direct - direct match on the key
- Thematic topics - matching mask, the key can be composite.
- fanout keyless
By default, the exchange type is set to direct.
The third optional parameter is the Exchange options. Exchange options - one of the constants:
- AMQP_PASSIVE - passive
- AMQP_AUTODELETE - auto-delete if not used
- AMQP_DURABLE - durable, i.e. remains when rebooting the broker.
If we do not need an exchange, then we can delete it:
AMQP amqp ;
auto_ptr < AMQPExchange > ex ( amqp. createExchange ( "ex" ) ) ;
ex - > Delete ( ) ;
// or
auto_ptr < AMQPExchange > ex ( amqp. createExchange ( ) ) ;
ex - > Delete ( "ex" ) ;
It is also possible to carry out binding (Bind) Exchange to the Queue. Binding is done by key. For the fanout exchange, we specify an empty string, the key value is ignored or we do not specify a key at all. If the exchange in the broker already exists, you can not re-declare it.
AMQP amqp ;
auto_ptr < AMQPExchange > ex ( amqp. createExchange ( "ex" ) ) ;
// exchange has been previously announced
ex - > Bind ( "queue_name" , "key_value" ) ;
// declaration of the fanout type and binding it to the queue
auto_ptr < AMQPExchange > ex2 ( amqp. createExchange ( ) ) ;
ex2 - > Declare ( "ex2" , "fanout" ) ;
ex2 - > Bind ( "queue_name2" ) ;
It should be noted that the Binding can be done both Queues to Exchange and Exchange to Queues.
Posting posts
:
If the exchange exists, then you can post messages to it. If there are queues attached to it, these messages will be sent to the appropriate queues. Each message is published with the corresponding key. If the Exchange type is fanout, then an empty key is set, which is ignored:
AMQP amqp ;
auto_ptr < AMQPExchange > ex ( amqp. createExchange ( "ex" ) ) ;
// exchange has been previously announced
ex - > Publish ( "some message" , "key" ) ;
PS ...
On the use of "Queues" in Part 2