Chapter 2. Using the Qpid Messaging API

Table of Contents

1. A Simple Messaging Program in C++
2. A Simple Messaging Program in Python
3. A Simple Messaging Program in .NET C#
4. Addresses
4.1. Address Strings
4.2. Subjects
4.3. Address String Options
4.4. Address String Grammar
5. Logging
5.1. Logging in C++
5.2. Logging in Python
6. Receiving Messages from Multiple Sources
7. Request / Response
8. Maps in Message Content
8.1. Qpid Maps in Python
8.2. Qpid Maps in C++
9. Performance
9.1. Batching Acknowledgements
9.2. Prefetch
9.3. Sizing the Replay Buffer
10. Reliability
10.1. Reconnect
10.2. Guaranteed Delivery
10.3. Reliability Options in Senders and Receivers
10.4. Cluster Failover
11. Security
12. Transactions
13. The AMQP 0-10 mapping

The Qpid Messaging API is quite simple, consisting of only a handful of core classes.

The following sections show how to use these classes in a simple messaging program.

1. A Simple Messaging Program in C++

The following C++ program shows how to create a connection, create a session, send messages using a sender, and receive messages using a receiver.

Example 2.1. "Hello world!" in C++

#include <qpid/messaging/Connection.h>
#include <qpid/messaging/Message.h>
#include <qpid/messaging/Receiver.h>
#include <qpid/messaging/Sender.h>
#include <qpid/messaging/Session.h>

#include <iostream>

using namespace qpid::messaging;

int main(int argc, char** argv) {
    std::string broker = argc > 1 ? argv[1] : "localhost:5672";
    std::string address = argc > 2 ? argv[2] : "amq.topic";
    Connection connection(broker); 
    try {
        connection.open();  (1)
        Session session = connection.createSession(); (2)

        Receiver receiver = session.createReceiver(address); (3)
        Sender sender = session.createSender(address); (4)

        sender.send(Message("Hello world!"));

        Message message = receiver.fetch(Duration::SECOND * 1); (5)
        std::cout << message.getContent() << std::endl;
        session.acknowledge(); (6)
        
        connection.close(); (7)
        return 0;
    } catch(const std::exception& error) {
        std::cerr << error.what() << std::endl;
        connection.close();
        return 1;   
    }
}

(1)

Establishes the connection with the messaging broker.

(2)

Creates a session object, which maintains the state of all interactions with the messaging broker, and manages senders and receivers.

(3)

Creates a receiver that reads from the given address.

(4)

Creates a sender that sends to the given address.

(5)

Reads the next message. The duration is optional, if omitted, will wait indefinitely for the next message.

(6)

Acknowledges messages that have been read. To guarantee delivery, a message remains on the messaging broker until it is acknowledged by a client. session.acknowledge() acknowledges all unacknowledged messages for the given session—this allows acknowledgements to be batched, which is more efficient than acknowledging messages individually.

(7)

Closes the connection, all sessions managed by the connection, and all senders and receivers managed by each session.