Qpid Proton C++ API 0.39.0
 
Loading...
Searching...
No Matches
Tutorial

This is a brief guide to to the fundamentals of building messaging applications using Qpid Proton C++.

Proton provides an "event-driven" programming model, where you implement a subclass of proton::messaging_handler and override functions that react to various AMQP events (connections opening and closing, messages being delivered, and so on).

The examples below show how to implement handlers for clients and servers and how to run them using the proton::container, a portable, easy-to-use way to build single-threaded clients or servers.

Some of the examples require an AMQP broker that can receive, store, and send messages. broker.cpp defines a simple example broker. If run without arguments, it listens on 0.0.0.0:5672, the standard AMQP port on all network interfaces. To use a different port or network interface, use the -a option.

broker -a <host>:<port>

Instead of the example broker, you can use any AMQP 1.0-compliant broker. You must configure your broker to have a queue (or topic) named "examples".

The helloworld examples take an optional URL argument. The other examples take an option -a URL. A URL looks like this:

HOST:PORT/ADDRESS

It usually defaults to 127.0.0.1:5672/examples, but you can change this if your broker is on a different host or port, or you want to use a different queue or topic name (the ADDRESS part of the URL).

Hello World!

Tradition dictates that we start with Hello World! This example sends a message to a broker and then receives the same message back. In a realistic system the sender and receiver would normally be in different processes. The complete example is helloworld.cpp

We will include the following classes. proton::container runs an event loop which dispatches events to a proton::messaging_handler. This allows a reactive style of programming which is well suited to messaging applications. proton::connection and proton::delivery are AMQP entities used in the handler functions. proton::url is a simple parser for the URL format mentioned above.

We will define a class hello_world which is a subclass of proton::messaging_handler and overrides functions to handle the events of interest in sending and receiving a message.

proton::messaging_handler::on_container_start() is called when the event loop first starts. We handle that by establishing a connection and creating a sender and a receiver.

proton::messaging_handler::on_sendable() is called when the message can be transferred over the associated sender link to the remote peer. We create a proton::message, set the message body to "Hello World!", and send the message. Then we close the sender, since we only want to send one message. Closing the sender will prevent further calls to proton::messaging_handler::on_sendable().

proton::messaging_handler::on_message() is called when a message is received. We just print the body of the message and close the connection, as we only want one message.

The message body is a proton::value. See AMQP and C++ types for more on how to extract the message body as type-safe C++ values.

Our main function creates an instance of the hello_world handler and a proton::container using that handler. Calling proton::container::run() sets things in motion and returns when we close the connection. It may throw an exception, which will be a subclass of proton::error. That in turn is a subclass of std::exception.

Asynchronous send and receive

Of course, these Hello World! examples are very artificial, communicating as they do over a network connection but with the same process. A more realistic example involves communication between separate processes, which could indeed be running on completely separate machines.

Let's separate the sender from the receiver, and transfer more than a single message between them.

We'll start with a simple sender, simple_send.cpp.

As with the previous example, we define the application logic in a class that handles events. Because we are transferring more than one message, we need to keep track of how many we have sent. We'll use a sent member variable for that. The total member variable will hold the number of messages we want to send.

class simple_send : public proton::messaging_handler {
private:
std::string url;
std::string user;
std::string password;
bool reconnect;
int sent;
int confirmed;
int total;
Handler for Proton messaging events.
Definition: messaging_handler.hpp:69
A channel for sending messages.
Definition: sender.hpp:40

As before, we use the proton::messaging_handler::on_container_start() event to establish our sender link over which we will transfer messages.

void on_container_start(proton::container &c) override {
if (!user.empty()) co.user(user);
if (!password.empty()) co.password(password);
if (reconnect) co.reconnect(proton::reconnect_options());
sender = c.open_sender(url, co);
}
Options for creating a connection.
Definition: connection_options.hpp:67
connection_options & password(const std::string &)
Set the password used to authenticate the connection.
connection_options & reconnect(const reconnect_options &)
Unsettled API - Set reconnect timing options.
connection_options & user(const std::string &)
Set the user name used to authenticate the connection.
A top-level container of connections, sessions, and links.
Definition: container.hpp:50
returned< sender > open_sender(const std::string &addr_url)
Open a connection and sender for addr_url.
Unsettled API - Options for reconnect and failover after connection loss.
Definition: reconnect_options.hpp:49

AMQP defines a credit-based flow-control mechanism. Flow control allows the receiver to control how many messages it is prepared to receive at a given time and thus prevents any component being overwhelmed by the number of messages it is sent.

In the proton::messaging_handler::on_sendable() callback, we check that our sender has credit before sending messages. We also check that we haven't already sent the required number of messages.

void on_sendable(proton::sender &s) override {
while (s.credit() && sent < total) {
std::map<std::string, int> m;
m["sequence"] = sent + 1;
msg.id(sent + 1);
msg.body(m);
s.send(msg);
sent++;
}
An AMQP message.
Definition: message.hpp:48
void id(const message_id &)
Set the message ID.
void body(const value &x)
Set the body. Equivalent to body() = x.
tracker send(const message &m)
Send a message on the sender.
}

The proton::sender::send() call above is asynchronous. When it returns, the message has not yet actually been transferred across the network to the receiver. By handling the proton::messaging_handler::on_tracker_accept() event, we can get notified when the receiver has received and accepted the message. In our example we use this event to track the confirmation of the messages we have sent. We only close the connection and exit when the receiver has received all the messages we wanted to send.

void on_tracker_accept(proton::tracker &t) override {
confirmed++;
if (confirmed == total) {
std::cout << "all messages confirmed" << std::endl;
}
void close()
Close the connection.
A tracker for a sent message.
Definition: tracker.hpp:41
class connection connection() const
Return the connection for this transfer.
}

If we are disconnected after a message is sent and before it has been confirmed by the receiver, it is said to be "in doubt". We don't know whether or not it was received. In this example, we will handle that by resending any in-doubt messages. This is known as an "at-least-once" guarantee, since each message should eventually be received at least once, though a given message may be received more than once (i.e., duplicates are possible). In the proton::messaging_handler::on_transport_close() callback, we reset the sent count to reflect only those that have been confirmed. The library will automatically try to reconnect for us, and when our sender is sendable again, we can restart from the point we know the receiver got to.

void on_transport_close(proton::transport &) override {
sent = confirmed;
}
A network channel supporting an AMQP connection.
Definition: transport.hpp:37

Now let's look at the corresponding receiver, simple_recv.cpp.

This time we'll use an expected member variable for for the number of messages we expect and a received variable to count how many we have received so far.

class simple_recv : public proton::messaging_handler {
private:
std::string url;
std::string user;
std::string password;
proton::receiver receiver;
int expected;
int received;
A channel for receiving messages.
Definition: receiver.hpp:41

We handle proton::messaging_handler::on_container_start() by creating our receiver, much like we did for the sender.

void on_container_start(proton::container &c) override {
if (!user.empty()) co.user(user);
if (!password.empty()) co.password(password);
receiver = c.open_receiver(url, co);
}
returned< receiver > open_receiver(const std::string &addr_url)
Open a connection and receiver for addr_url.

We also handle the proton::messaging_handler::on_message() event for received messages and print the message out as in the Hello World! examples. However, we add some logic to allow the receiver to wait for a given number of messages and then close the connection and exit. We also add some logic to check for and ignore duplicates, using a simple sequential ID scheme.

void on_message(proton::delivery &d, proton::message &msg) override {
if (!msg.id().empty() && proton::coerce<int>(msg.id()) < received) {
return; // Ignore if no id or duplicate
}
A received message.
Definition: delivery.hpp:40

Direct send and receive

Sending between these two examples requires an intermediary broker since neither accepts incoming connections. AMQP allows us to send messages directly between two processes. In that case, one or other of the processes needs to accept incoming connections. Let's create a modified version of the receiving example that does this with direct_recv.cpp.

There are only two differences here. Instead of initiating a link (and implicitly a connection), we listen for incoming connections.

void on_container_start(proton::container &c) override {
listener = c.listen(url, listen_handler);
}
listener listen(const std::string &listen_url, listen_handler &handler)
Listen for new connections on listen_url.

When we have received all the expected messages, we then stop listening for incoming connections by calling proton::listener::stop()

void on_message(proton::delivery &d, proton::message &msg) override {
if (proton::coerce<int>(msg.id()) < received) {
return; // Ignore duplicate
}
if (expected == 0 || received < expected) {
std::cout << msg.body() << std::endl;
received++;
}
if (received == expected) {
d.receiver().close();
listener.stop();
}
class receiver receiver() const
Return the receiver for this delivery.
}

You can use the simple_send.cpp example to send to this receiver directly. (Note: you will need to stop any broker that is listening on the 5672 port, or else change the port used by specifying a different address to each example via the -a command-line switch).

We can also modify the sender to allow the original receiver to connect to it, in direct_send.cpp. Again, that requires just two modifications:

As with the modified receiver, instead of initiating establishment of a link, we listen for incoming connections.

void on_container_start(proton::container &c) override {
listener = c.listen(url, listen_handler);
}

When we have received confirmation of all the messages we sent, we call container::listener::stop() to exit.

void on_tracker_accept(proton::tracker &t) override {
confirmed++;
if (confirmed == total) {
std::cout << "all messages confirmed" << std::endl;
listener.stop();
}
}

To try this modified sender, run the original simple_recv.cpp against it.

The symmetry in the underlying AMQP wire protocol that enables this is quite unique and elegant, and in reflecting this the Proton API provides a flexible toolkit for implementing all sorts of interesting intermediaries.

Request and response

A common pattern is to send a request message and expect a response message in return. AMQP has special support for this pattern. Let's have a look at a simple example. We'll start with server.cpp, the program that will process the request and send the response. Note that we are still using a broker in this example.

Our server will provide a very simple service: it will respond with the body of the request converted to uppercase.

class server : public proton::messaging_handler {
std::string conn_url_;
std::string addr_;
std::map<std::string, proton::sender> senders_;
public:
server(const std::string& u, const std::string& a) :
conn_url_(u), addr_(a) {}
conn_ = c.connect(conn_url_);
conn_.open_receiver(addr_);
std::cout << "Server connected to " << conn_url_ << std::endl;
}
std::string to_upper(const std::string& s) {
std::string uc(s);
size_t l = uc.size();
for (size_t i=0; i<l; i++) {
uc[i] = static_cast<char>(std::toupper(uc[i]));
}
return uc;
}
std::cout << "Received " << m.body() << std::endl;
std::string reply_to = m.reply_to();
reply.to(reply_to);
reply.body(to_upper(proton::get<std::string>(m.body())));
if (!senders_[reply_to]) {
senders_[reply_to] = conn_.open_sender(reply_to);
}
senders_[reply_to].send(reply);
}
};
A connection to a remote AMQP peer.
Definition: connection.hpp:47
receiver open_receiver(const std::string &addr)
Open a receiver for addr on default_session().
sender open_sender(const std::string &addr)
Open a sender for addr on default_session().
returned< connection > connect(const std::string &conn_url, const connection_options &conn_opts)
Connect to conn_url and send an open request to the remote peer.
void to(const std::string &)
Set the destination address.
void reply_to(const std::string &)
Set the address for replies.
void correlation_id(const message_id &)
Set the ID for matching related messages.
virtual void on_message(delivery &, message &)
A message is received.
virtual void on_container_start(container &)
The container event loop is starting.
std::string get< std::string >(const message_id &x)
Get the std::string value or throw conversion_error.
Definition: message_id.hpp:87

The code here is not too different from the simple receiver example. However, when we receive a request in proton::messaging_handler::on_message, we look at the proton::message::reply_to address and create a sender with that address for the response. We'll cache the senders in case we get further requests with the same reply_to.

Now let's create a simple client.cpp to test this service out.

Our client takes a list of strings to send as requests.

client(const std::string &u, const std::vector<std::string>& r) : url(u), requests(r) {}

Since we will be sending and receiving, we create a sender and a receiver in proton::messaging_handler::on_container_start. Our receiver has a blank address and sets the dynamic flag to true, which means we expect the remote end (the broker or server) to assign a unique address for us.

void on_container_start(proton::container &c) override {
sender = c.open_sender(url);
// Create a receiver requesting a dynamically created queue
// for the message source.
receiver_options opts = receiver_options().source(source_options().dynamic(true));
receiver = sender.connection().open_receiver("", opts);
}

Now we need a function to send the next request from our list of requests. We set the reply_to address to be the dynamically assigned address of our receiver.

void send_request() {
req.body(requests.front());
req.reply_to(receiver.source().address());
sender.send(req);
}
class source source() const
Get the source node.
std::string address() const
The address of the source.

We need to use the address assigned by the broker as the reply_to address of our requests, so we can't send them until our receiver has been set up. To do that, we add an proton::messaging_handler::on_receiver_open() method to our handler class and use that as the trigger to send our first request.

void on_receiver_open(proton::receiver &) override {
send_request();
}

When we receive a reply, we send the next request.

void on_message(proton::delivery &d, proton::message &response) override {
if (requests.empty()) return; // Spurious extra message!
std::cout << requests.front() << " => " << response.body() << std::endl;
requests.erase(requests.begin());
if (!requests.empty()) {
send_request();
} else {
}
}

Direct request and response

We can avoid the intermediary process by writing a server that accepts connections directly, server_direct.cpp. It involves the following changes to our original server:

Our server must generate unique reply-to addresses for links from the client that request a dynamic address (previously this was done by the broker). We use a simple counter.

std::string generate_address() {
std::ostringstream addr;
addr << "server" << address_counter++;
return addr.str();
}

Next we need to handle incoming requests for links with dynamic addresses from the client. We give the link a unique address and record it in our senders map.

void on_sender_open(proton::sender &sender) override {
if (sender.source().dynamic()) {
std::string addr = generate_address();
sender.open(proton::sender_options().source(proton::source_options().address(addr)));
senders[addr] = sender;
}
Options for creating a sender.
Definition: sender_options.hpp:60
class source source() const
Get the source node.
void open()
Open the sender.
Options for creating a source node for a sender or receiver.
Definition: source_options.hpp:46
bool dynamic() const
True if the remote node is created dynamically.

Note that we are interested in sender links above because we are implementing the server. A receiver link created on the client corresponds to a sender link on the server.

Finally when we receive a message we look up its reply_to in our senders map and send the reply.

void on_message(proton::delivery &, proton::message &m) override {
std::cout << "Received " << m.body() << std::endl;
std::string reply_to = m.reply_to();
sender_map::iterator it = senders.find(reply_to);
if (it == senders.end()) {
std::cout << "No link for reply_to: " << reply_to << std::endl;
} else {
proton::sender sender = it->second;
reply.to(reply_to);
reply.body(to_upper(proton::get<std::string>(m.body())));
sender.send(reply);
}
}