Table of Contents
The Qpid Messaging API is quite simple, consisting of only a handful of core classes.
A message consists of a standard set
of fields (e.g. subject
,
reply-to
), an application-defined set of
properties, and message content (the main body of the
message).
A connection represents a network connection to a remote endpoint.
A session provides a sequentially ordered context for sending and receiving messages. A session is obtained from a connection.
A sender sends messages to a target
using the sender.send
method. A sender is
obtained from a session for a given target address.
A receiver receives messages from a
source using the receiver.fetch
method.
A receiver is obtained from a session for a given source
address.
The following sections show how to use these classes in a simple messaging program.
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 1.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";
std::string connectionOptions = argc > 3 ? argv[3] : "";
Connection connection(broker, connectionOptions);
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;
}
}
Establishes the connection with the messaging broker. | |
Creates a session object on which messages will be sent and received. | |
Creates a receiver that receives messages from the given address. | |
Creates a sender that sends to the given address. | |
Receives the next message. The duration is optional, if omitted, will wait indefinitely for the next message. | |
Acknowledges receipt of all fetched messages on the session. This informs the broker that the messages were transferred and processed by the client successfully. | |
Closes the connection, all sessions managed by the connection, and all senders and receivers managed by each session. |
Apache Qpid, Messaging built on AMQP; Copyright © 2015 The Apache Software Foundation; Licensed under the Apache License, Version 2.0; Apache Qpid, Qpid, Qpid Proton, Proton, Apache, the Apache feather logo, and the Apache Qpid project logo are trademarks of The Apache Software Foundation; All other marks mentioned may be trademarks or registered trademarks of their respective owners