5.4.2. Publishing Messages to a Topic Exchange
The first program in the publish/subscribe example, topic_publisher.cpp, defines two new functions: one that publishes messages to the topic exchange, and one that indicates that no more messages are coming.
The publish_messages() function publishes a series of five messages using the specified routing key.
void publish_messages(Session& session, string routing_key)
{
Message message;
// Set the routing key once, we'll use the same routing key for all
// messages.
message.getDeliveryProperties().setRoutingKey(routing_key);
for (int i=0; i<5; i++) {
stringstream message_data;
message_data << "Message " << i;
message.setData(message_data.str());
session.messageTransfer(arg::content=message, arg::destination="amq.topic");
}
}
The no_more_messages() function signals the end of messages using the control routing key, which is reserved for control messages.
void no_more_messages(Session& session)
{
Message message;
message.getDeliveryProperties().setRoutingKey("control");
message.setData("That's all, folks!");
session.messageTransfer(arg::content=message, arg::destination="amq.topic");
}
In the main body of the program, messages are published using four different routing keys, and then the end of messages is indicated by a message sent to a separate routing key.
publish_messages(session, "usa.news");
publish_messages(session, "usa.weather");
publish_messages(session, "europe.news");
publish_messages(session, "europe.weather");
no_more_messages(session);