Chapter 3. Getting Started
This chapter guides you through a simple exercise to help you get started using AMQ C++. Before starting, make sure you have completed the steps in the Chapter 2, Installation chapter for your environment.
3.1. Preparing the Broker Copy linkLink copied to clipboard!
The example programs require a running broker with a queue named examples
. Follow these steps to define the queue and start the broker.
- Install the broker.
- Create a broker instance. Enable anonymous access.
Start the broker instance and check the console for any critical errors logged during startup.
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Use the
artemis queue
command to create a queue calledexamples
.BROKER_INSTANCE_DIR/bin/artemis queue create --name examples --auto-create-address --anycast
$ BROKER_INSTANCE_DIR/bin/artemis queue create --name examples --auto-create-address --anycast
Copy to Clipboard Copied! Toggle word wrap Toggle overflow You are prompted to answer a series of questions. For yes|no questions, type
N
; otherwise, press Enter to accept the default value.
3.2. Building the Examples Copy linkLink copied to clipboard!
This section illustrates how to compile the example programs that come with the client API.
Create a directory to hold the programs. This example will call it "AMQ7C++SmokeTest", but you can use any name you like.
mkdir AMQ7C++SmokeTest
$ mkdir AMQ7C++SmokeTest
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Enter the new directory.
cd AMQ7C++SmokeTest
$ cd AMQ7C++SmokeTest
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Copy all the examples to this directory.
cp -r /usr/share/proton-0.21.0/examples/cpp .
$ cp -r /usr/share/proton-0.21.0/examples/cpp .
Copy to Clipboard Copied! Toggle word wrap Toggle overflow NoteThe example directory name depends on the version of proton we just installed - 0.21.0 is the version as of the writing of this documentation. If a different version is actually installed, this directory name will need to be changed to reflect the actual name installed on the system.
This example compiles all of the examples and will then run the two of interest. They can be compiled like this:
cmake . make
$ cmake . $ make
Copy to Clipboard Copied! Toggle word wrap Toggle overflow NoteIt 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 Copy linkLink copied to clipboard!
The compiled example programs will 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 called
examples
../simple_send -m 10
$ ./simple_send -m 10
Copy to Clipboard Copied! Toggle word wrap Toggle overflow The command line option
-m 10
tells the program to send 10 messages.This will output:
all messages confirmed $
all messages confirmed $
Copy to Clipboard Copied! Toggle word wrap Toggle overflow By default the
simple_send
example connects to an AMQP listener on the same machine (IP address127.0.0.1
, port5672
) and sends messages to the AMQP addressexamples
. This corresponds to theexamples
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
$ ./simple_recv -m 10
Copy to Clipboard Copied! Toggle word wrap Toggle overflow In this case the command line option
-m 10
tells the program to exit after receiving 10 messages.Copy to Clipboard Copied! Toggle word wrap Toggle overflow The
simple_recv
example is similar to thesimple_send
example. It also connects to an AMQP listener on the same machine. By default it will subscribe to the AMQP addressexamples
and it will receive 100 messages.