activemq-artemis/examples/protocols/amqp/proton-cpp
Clebert Suconic 0b30187091 [maven-release-plugin] prepare release 2.18.0 2021-08-06 17:04:16 -04:00
..
src/main ARTEMIS-2249 - update cpp example to use correct client libs 2019-03-05 15:27:08 -05:00
compile.sh removed flag ' -l qpidtypes' from proton-cpp example 2020-03-23 17:02:09 -04:00
pom.xml [maven-release-plugin] prepare release 2.18.0 2021-08-06 17:04:16 -04:00
readme.md ARTEMIS-2249 - update cpp example to use correct client libs 2019-03-05 15:27:08 -05:00

readme.md

AMQP CPP example

ActiveMQ Artemis is a multi protocol broker. It will inspect the initial handshake of clients to determine what protocol to use.

All you need to do is to connect a client into ActiveMQ's configured port and you should be able connect.

To run this example simply run the command mvn verify -Pexample, execute the compile.sh script and start the executable called ./hello

# first make sure you have the dependencies you need to compile and run the client
# You will have to adapt this step according to your platform. Consult the [qpid docs](http://qpid.apache.org/releases/qpid-0.30/programming/book/) for more information.
# There is a list of [packages](http://qpid.apache.org/packages.html) you can install as well.
[proton-cpp]$ sudo yum install qpid-proton-cpp-devel qpid-proton-cpp-docs

# on a first window
[proton-cpp]$ mvn verify -Pexample

# on a second window
# That goes without saying but you will of course need g++ (the C++ compiler) installed
[proton-cpp]$ ./compile.sh
[proton-cpp]$ ./hello

You don't need to do anything special to configure the ActiveMQ Artemis broker to accept AMQP clients.

Just for the sake of documentation though we are setting the port of ActiveMQ Artemis on this example as 5672 which is the port qpid have by default.

This is totally optional and you don't need to follow this convention. You can use any port you chose including ActiveMQ's 61616 default port:

<acceptor name="proton-acceptor">tcp://localhost:5672</acceptor>

Example step-by-step

We are using qpid cpp client on this example. There are several libraries you may chose from for AMQP. We have ellect one that we consider simple enough for users.

This example is based on qpid's hello world example.

  1. qpid-cpp-client-devel installed.

Assuming you are on Linux Fedora, you will need to run this following command

yum install qpid-cpp-client-devel

Consult the qpid documentation, and the required packages for information on other platoforms.

  1. Make the proper C++ imports

  2. Create an amqp qpid 1.0 connection.

    std::string broker = argc > 1 ? argv[1] : "localhost:61616"; std::string address = argc > 2 ? argv[2] : "jms.queue.exampleQueue"; std::string connectionOptions = argc > 3 ? argv[3] : "{protocol:amqp1.0}"; // Look at the docs for more options

    Connection connection(broker, connectionOptions); connection.open();

  3. Create a session

    Session session = connection.createSession();

  4. Create a sender

Sender sender = session.createSender(address);
  1. send a simple message
sender.send(Message("Hello world!"));
  1. create a receiver
Receiver receiver = session.createReceiver(address);
  1. receive the simple message
Message message = receiver.fetch(Duration::SECOND * 1);
  1. acknowledge the message
session.acknowledge();
  1. close the connection
connection.close();