Chapter 3. Getting started

This chapter guides you through a simple exercise to help you get started using AMQ C++.

3.1. Preparing the broker

The example programs require a running broker with a queue named examples. Follow these steps to define the queue and start the broker:

Procedure

  1. Install the broker.
  2. Create a broker instance. Enable anonymous access.
  3. Start the broker instance and check the console for any critical errors logged during startup.

    $ <broker-instance-dir>/bin/artemis run
    ...
    14:43:20,158 INFO  [org.apache.activemq.artemis.integration.bootstrap] AMQ101000: Starting ActiveMQ Artemis Server
    ...
    15:01:39,686 INFO  [org.apache.activemq.artemis.core.server] AMQ221020: Started Acceptor at 0.0.0.0:5672 for protocols [AMQP]
    ...
    15:01:39,691 INFO  [org.apache.activemq.artemis.core.server] AMQ221007: Server is now live
  4. Use the artemis queue command to create a queue called examples.

    <broker-instance-dir>/bin/artemis queue create --name examples --auto-create-address --anycast

    You are prompted to answer a series of questions. For yes or no questions, type N. Otherwise, press Enter to accept the default value.

3.2. Building the examples

This section illustrates how to compile the example programs that come with the client API.

  1. Create a directory to hold the programs. This example names it "AMQ7C++SmokeTest", but you can use any name you like.

    $ mkdir AMQ7C++SmokeTest
  2. Enter the new directory.

    $ cd AMQ7C++SmokeTest
  3. Copy all the examples to this directory.

    $ cp -r /usr/share/proton-0.28.0/examples/cpp .
    Note

    The example directory name depends on the version of proton we just installed - 0.28.0 is the version as of the writing of this documentation. If a different version is actually installed, this directory name needs to be changed to reflect the actual name installed on the system.

    This example compiles all of the examples and then runs the two of interest. They can be compiled like this:

    $ cmake .
    $ make
    Note

    It is not recommended to use cmake in the same directory as the source being built. This example does so for simplicity.

    Consider creating a directory for builds and run cmake there.

3.3. Sending and receiving messages

The compiled example programs use the broker we started earlier to queue the messages between sending and receiving.

Sending messages

  • Use one of the example programs to send 10 messages to a queue named examples.

    $ ./simple_send -m 10

    The command line option -m 10 tells the program to send 10 messages.

    This outputs:

    all messages confirmed
    $

    By default the simple_send example connects to an AMQP listener on the same machine (IP address 127.0.0.1, port 5672) and sends messages to the AMQP address examples. This corresponds to the examples queue that we have configured in the AMQ Broker.

Receiving messages

  • Execute the following commands as you did in the previous example.

    $ ./simple_recv -m 10

    In this case the command line option -m 10 tells the program to exit after receiving 10 messages.

    simple_recv listening on 127.0.0.1:5672/examples
    {"sequence"=1}
    {"sequence"=2}
    {"sequence"=3}
    {"sequence"=4}
    {"sequence"=5}
    {"sequence"=6}
    {"sequence"=7}
    {"sequence"=8}
    {"sequence"=9}
    {"sequence"=10}
    $

    The simple_recv example is similar to the simple_send example. It also connects to an AMQP listener on the same machine. By default it subscribes to the AMQP address examples and receives 100 messages.