Chapter 8. Senders and receivers
The client uses sender and receiver links to represent channels for delivering messages. Senders and receivers are unidirectional, with a source end for the message origin, and a target end for the message destination.
Source and targets often point to queues or topics on a message broker. Sources are also used to represent subscriptions.
8.1. Creating queues and topics on demand
Some message servers support on-demand creation of queues and topics. When a sender or receiver is attached, the server uses the sender target address or the receiver source address to create a queue or topic with a name matching the address.
The message server typically defaults to creating either a queue (for one-to-one message delivery) or a topic (for one-to-many message delivery). The client can indicate which it prefers by setting the queue
or topic
capability on the source or target.
To select queue or topic semantics, follow these steps:
- Configure your message server for automatic creation of queues and topics. This is often the default configuration.
-
Set either the
queue
ortopic
capability on your sender target or receiver source, as in the examples below.
Example: Sending to a queue created on demand
void on_container_start(proton::container& cont) override { proton::connection conn = cont.connect("amqp://example.com"); proton::sender_options opts {}; proton::target_options topts {}; topts.capabilities(std::vector<proton::symbol> { "queue" }); opts.target(topts); conn.open_sender("jobs", opts); }
Example: Receiving from a topic created on demand
void on_container_start(proton::container& cont) override { proton::connection conn = cont.connect("amqp://example.com"); proton::receiver_options opts {}; proton::source_options sopts {}; sopts.capabilities(std::vector<proton::symbol> { "topic" }); opts.source(sopts); conn.open_receiver("notifications", opts); }
For more details, see the following examples:
8.2. Creating durable subscriptions
A durable subscription is a piece of state on the remote server representing a message receiver. Ordinarily, message receivers are discarded when a client closes. However, because durable subscriptions are persistent, clients can detach from them and then re-attach later. Any messages received while detached are available when the client re-attaches.
Durable subscriptions are uniquely identified by combining the client container ID and receiver name to form a subscription ID. These must have stable values so that the subscription can be recovered.
To create a durable subscription, follow these steps:
Set the connection container ID to a stable value, such as
client-1
:proton::container cont {handler, "client-1"};
Create a receiver with a stable name, such as
sub-1
, and configure the receiver source for durability by setting thedurability_mode
andexpiry_policy
options:void on_container_start(proton::container& cont) override { proton::connection conn = cont.connect("amqp://example.com"); proton::receiver_options opts {}; proton::source_options sopts {}; opts.name("sub-1"); sopts.durability_mode(proton::source::UNSETTLED_STATE); sopts.expiry_policy(proton::source::NEVER); opts.source(sopts); conn.open_receiver("notifications", opts); }
To detach from a subscription, use the proton::receiver::detach()
method. To terminate the subscription, use the proton::receiver::close()
method.
For more information, see the durable-subscribe.cpp example.