|
RMQ - RabbitMQ C++ Library
|
rmqcpp is a RabbitMQ client library written in C++03.
rmqa::RabbitContext provides an API for connecting to RabbitMQ virtual hosts (vhosts). The RabbitContext object stores:
RabbitContext contains default implementations of them as detailed below. Users can replace these components with a rmqa::RabbitContextOptions object.
Threadpool is used for asynchronous callbacks to the user code (e.g., received messages, publisher confirms, error callbacks). A bdlmt::ThreadPool is created by default.
Metric publisher is used to publish internal library metrics. Users can provide their own implementation for publishing these metrics by implementing the rmqp::MetricPublisher interface. Alternatively, publishing can be disabled by passing an instance of rmqa::NoOpMetricPublisher.
Error callback is called when a connection or channel is closed by the RabbitMQ broker.
Important – RabbitContext object must outlive other library objects (VHost, Producer, Consumer).
rmqa::VHost provides an API for creating producer and consumer objects on the selected RabbitMQ vhost. A VHost object can be created from rmqa::RabbitContext.
Creating a rmqa::VHost instance does not immediately create a connection with the RabbitMQ broker. These connections are created lazily when calling rmqa::VHost::createProducer and rmqa::VHost::createConsumer.
Important – VHost object must outlive other library objects (Producer, Consumer).
For a more elaborate documentation of the topology, see AMQP 0-9-1 Model Explained.
AMQP topology consists of:
Each publisher sends messages to a particular exchange. Each consumer consumes messages from a particular queue. Exchanges and queues are connected using bindings.
Messages can be sent to a RabbitMQ broker using a rmqa::Producer object. To create one, it is necessary to provide:
send blocks.When sending a message, provide a callback function to be invoked when the publisher confirm is received from the broker.
To send a message, construct a rmqt::Message object. Each message needs its own object even when intentionally sending the same message multiple times – this is because the message ID must be unique.
When shutting down the producer, it is necessary to wait until it receives all outstanding publisher confirms to avoid losing the unconfirmed messages.
Messages can be consumed from a RabbitMQ broker using a rmqa::Consumer.
Messages are consumed asynchronously. The user-provided consumer callback method will be invoked on the RabbitContext threadpool for every message received.
A Consumer object is created with the following arguments:
topology);Here is an example of a consumer callback implementation:
Important – messages should be acknowledged only after completely processing them. For example, if the consumer logic sends received messages to some RabbitMQ exchange, the message should be acknowledged only after receiving the publisher confirm of the send operation. A premature call to ack() followed by a failure of the user's consumer logic can result in a dropped message.