
Lib amqpcpp wrapper for librabbitmq
Over the past year and a half, the RabbitMQ Queue Server , which runs on the AMQP protocol , is actively gaining popularity . About this protocol there were already enough articles on Habré. There is a librabbitmq library in the toolkit.
Based on this library, I uploaded the C ++ project amqpcpp library, which is a simplified interface to librabbitmq, in Google Code . There is no documentation, there are only three examples of use in the distribution kit. Under the cast, a brief summary of the API and examples of its use. Use has become easier.
very simple: run the makefile, which compiles the static library libamqpcpp.a
there is also a compilation of examples in the makefile:
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. Until configure is done, I will be glad - if anyone helps me create it.
The library is included.
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.
Class Amqp - carries two functions:
For each instance of the class "queue" or "exchange" opens a new channel. The channel closes automatically when the class destructor is called.
By default, a connection is made with the broker at vhost = / login = guest password = guest port = 5672
Connection parameters are transmitted in the line (connectionString) of the following form: “password: login @ host: port / vhost”. This is a well-established standard. If any parameter is missing, then it is replaced with the default parameter.
If there is no connection, then you can print the connection parameters using the printConnect () method.
An instance of the AMQPExchange exchange class can be obtained from the AMQP class by the createExchange ()
method. This method may or may not have a parameter - the name of the exchange. To avoid leaks - it is suggested to use auto_ptr
For operations with the Exchange - it must be declared. There are two ways to declare an exchange: either specify the name of the exchange when it is created, or when declaring:
The second parameter of the AMQPExchange :: Declare () method is the exchange type, a string value. Exchanges can be of three types:
By default, the type of exchange is set - direct.
The third optional parameter is the Exchange parameters. Exchange parameters - one of the constants:
If we do not need an exchange, then we can delete it:
You can also bind (Exchange) to the Queue. Binding is done by key. To exchange fanout - specify an empty string, the key value is ignored or do not specify the key at all. If the exchange already exists in the broker, then you can not declare it again.
It should be noted that the Binding can be done as Queues for Exchange, and Exchange to the Queue.
If the exchange exists, then you can publish messages to it. If there are queues attached to it, then these messages will be sent to the corresponding queues. Each message is published with the corresponding key. If the type of exchange is fanout, then an empty key is set, which is ignored:
PS ...
On the use of "Queues" in part 2
Based on this library, I uploaded the C ++ project amqpcpp library, which is a simplified interface to librabbitmq, in Google Code . There is no documentation, there are only three examples of use in the distribution kit. Under the cast, a brief summary of the API and examples of its use. Use has become easier.
Installation
very simple: run the makefile, which compiles the static library libamqpcpp.a
there is also a compilation of examples in the makefile:
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. Until configure is done, I will be glad - if anyone helps me create it.
API Summary
The library is included.
#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.
Class Amqp - carries two functions:
- connects to a broker
- It is a factory for creating instances of classes of "queues" and "exchanges" and transferring the connection to them.
For each instance of the class "queue" or "exchange" opens a new channel. The channel closes automatically when the class destructor is called.
By default, a connection is made with the broker at vhost = / login = guest password = guest port = 5672
Connection parameters are transmitted in the line (connectionString) of the following form: “password: login @ host: port / vhost”. This is a well-established standard. If any parameter is missing, then it is replaced with 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 by the createExchange ()
method. This method may or may not have a parameter - the name of the exchange. To avoid leaks - it is suggested to use auto_ptr
AMQP amqp;
auto_ptrex (amqp.createExchange ("ex"));
ex-> Declare ("e", "topic");
For operations with the Exchange - it must be declared. There are two ways to declare an exchange: either specify the name of the exchange when it is created, or when declaring:
AMQP amqp;
auto_ptrex (amqp.createExchange ("ex"));
ex-> Declare ();
//
auto_ptrex (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 by key
- Thematic topic - matching by mask, the key can be composite.
- fanout, keyless
By default, the type of exchange is set - direct.
The third optional parameter is the Exchange parameters. Exchange parameters - one of the constants:
- AMQP_PASSIVE - Passive
- AMQP_AUTODELETE - auto delete if not used
- AMQP_DURABLE - durable, i.e. remains when the broker reboots.
If we do not need an exchange, then we can delete it:
AMQP amqp;
auto_ptrex (amqp.createExchange ("ex"));
ex-> Delete ();
// or
auto_ptrex (amqp.createExchange ());
ex-> Delete ("ex");
You can also bind (Exchange) to the Queue. Binding is done by key. To exchange fanout - specify an empty string, the key value is ignored or do not specify the key at all. If the exchange already exists in the broker, then you can not declare it again.
AMQP amqp;
auto_ptrex (amqp.createExchange ("ex"));
// exchange was announced earlier
ex-> Bind ("queue_name", "key_value");
// declare an exchange of type fanout and bind it to the
auto_ptr queueex2 (amqp.createExchange ());
ex2-> Declare ("ex2", "fanout");
ex2-> Bind ("queue_name2");
It should be noted that the Binding can be done as Queues for Exchange, and Exchange to the Queue.
Posting
:If the exchange exists, then you can publish messages to it. If there are queues attached to it, then these messages will be sent to the corresponding queues. Each message is published with the corresponding key. If the type of exchange is fanout, then an empty key is set, which is ignored:
AMQP amqp;
auto_ptrex (amqp.createExchange ("ex"));
// exchange was announced earlier
ex-> Publish ("some message", "key");
PS ...
On the use of "Queues" in part 2