I've mentioned
Apache Qpid
a number of times in this newsletter over the past year
or so. In case you're not yet familiar with Apache Qpid,
it's a message queuing system which implements the
Advanced Message Queuing Protocol (
AMQP).
Because it's open source and freely available for
multiple platforms, it's an attractive choice when you
need reliable, persistent message queuing.
Apache
Qpid offers client APIs in a number of languages, but I
work more with the C++ version, so will use that
language for this example.
To lay a little
groundwork, the players in AMQP that we're concerned
with are:
- Client: an entity that sends and receives
messages. The example in this article is a client
program.
- Exchange: destination for messages that a client
sends; there are a number of different exchange types
which provide various message distribution mechanisms
such as direct, fan-out, and publish-subscribe
(pub-sub).
- Queue: holds messages that a client will
receive
- Broker: the traffic cop; manages exchanges,
queues, and the routing between them
- Connection: a data connection to the broker
- Session: an abstraction for managing a set of
message transfers; Sessions operate over
Connections.
Our example will send a
message to a direct exchange. The message will have a
routing key that will help the broker route the message
from the exchange to any queues that are interested in
this message. A subsequent example will show how to
receive such a message.
Ok, let's get into it...
first, the header files we need:
#include
<qpid/client/Connection.h>#include
<qpid/client/Session.h>#include
<qpid/client/Message.h>In order to
anything, we need a connection to the broker and a
session to use. We'll assume there's a broker on the
local system and that it's listening on TCP port 5672
(the default for Qpid).
qpid::client::Connection
connection;try
{
connection.open("localhost", 5672);
qpid::client::Session session =
connection.newSession();
qpid::client::Message message("Hello, world!",
"my_key");
session.messageTransfer(arg::content=message,
arg::destination="amq.direct");
connection.close();
return 0;}
catch(const std::exception& error) {
std::cout << error.what() <<
std::endl;
return 1;}That's
it. Of course, there are many fancy options for when
needed, but this program sends a message and will work
on all Qpid platforms. If you're interested in checking
out the API details behind this, the API reference is at
http://qpid.apache.org/docs/api/cpp/html/index.html.
Keep
an eye on my
blog for
more how-to article on Qpid (and ACE). Is there
something particular you want to see an example of? Let
me know!