Using the AMQ C++ Client
For Use with AMQ Clients 2.4
Abstract
Chapter 1. Overview Copy linkLink copied to clipboard!
AMQ C++ is a library for developing messaging applications. It enables you to write C++ applications that send and receive AMQP messages.
AMQ C++ is part of AMQ Clients, a suite of messaging libraries supporting multiple languages and platforms. For an overview of the clients, see AMQ Clients Overview. For information about this release, see AMQ Clients 2.4 Release Notes.
AMQ C++ is based on the Proton API from Apache Qpid.
1.1. Key features Copy linkLink copied to clipboard!
- An event-driven API that simplifies integration with existing applications
- SSL/TLS for secure communication
- Flexible SASL authentication
- Automatic reconnect and failover
- Seamless conversion between AMQP and language-native data types
- Access to all the features and capabilities of AMQP 1.0
1.2. Supported standards and protocols Copy linkLink copied to clipboard!
AMQ C++ supports the following industry-recognized standards and network protocols:
- Version 1.0 of the Advanced Message Queueing Protocol (AMQP)
- Versions 1.0, 1.1, 1.2, and 1.3 of the Transport Layer Security (TLS) protocol, the successor to SSL
- Simple Authentication and Security Layer (SASL) mechanisms supported by Cyrus SASL, including ANONYMOUS, PLAIN, SCRAM, EXTERNAL, and GSSAPI (Kerberos)
- Modern TCP with IPv6
1.3. Supported configurations Copy linkLink copied to clipboard!
AMQ C++ supports the following OS and language versions:
- Red Hat Enterprise Linux 6 with GNU C++, compiling as C++03 or C++0x (partial C++11 support)
- Red Hat Enterprise Linux 7 and 8 with GNU C++, compiling as C++03 or C++11
- Microsoft Windows 10 Pro with Microsoft Visual Studio 2013
- Microsoft Windows Server 2012 R2 and 2016 with Microsoft Visual Studio 2013
AMQ C++ is supported in combination with the following AMQ components and versions:
- All versions of AMQ Broker
- All versions of AMQ Interconnect
- All versions of AMQ Online
- A-MQ 6 versions 6.2.1 and higher
For more information, see Red Hat AMQ Supported Configurations.
1.4. Terms and concepts Copy linkLink copied to clipboard!
This section introduces the core API entities and describes how they operate together.
| Entity | Description |
|---|---|
| Container | A top-level container of connections. |
| Connection | A channel for communication between two peers on a network. It contains sessions. |
| Session | A context for sending and receiving messages. It contains senders and receivers. |
| Sender | A channel for sending messages to a target. It has a target. |
| Receiver | A channel for receiving messages from a source. It has a source. |
| Source | A named point of origin for messages. |
| Target | A named destination for messages. |
| Message | An application-specific piece of information. |
| Delivery | A message transfer. |
AMQ C++ sends and receives messages. Messages are transferred between connected peers over senders and receivers. Senders and receivers are established over sessions. Sessions are established over connections. Connections are established between two uniquely identified containers. Though a connection can have multiple sessions, often this is not needed. The API allows you to ignore sessions unless you require them.
A sending peer creates a sender to send messages. The sender has a target that identifies a queue or topic at the remote peer. A receiving peer creates a receiver to receive messages. The receiver has a source that identifies a queue or topic at the remote peer.
The sending of a message is called a delivery. The message is the content sent, including all metadata such as headers and annotations. The delivery is the protocol exchange associated with the transfer of that content.
To indicate that a delivery is complete, either the sender or the receiver settles it. When the other side learns that it has been settled, it will no longer communicate about that delivery. The receiver can also indicate whether it accepts or rejects the message.
1.5. Document conventions Copy linkLink copied to clipboard!
This document uses the following conventions for the sudo command and file paths.
The sudo command
In this document, sudo is used for any command that requires root privileges. You should always exercise caution when using sudo, as any changes can affect the entire system.
For more information about using sudo, see The sudo Command.
About the use of file paths in this document
In this document, all file paths are valid for Linux, UNIX, and similar operating systems (for example, /home/...). If you are using Microsoft Windows, you should use the equivalent Microsoft Windows paths (for example, C:\Users\...).
Chapter 2. Installation Copy linkLink copied to clipboard!
This chapter guides you through the steps to install AMQ C++ in your environment.
2.1. Prerequisites Copy linkLink copied to clipboard!
To begin installation, use your subscription to access AMQ distribution files and repositories.
2.2. Installing on Red Hat Enterprise Linux Copy linkLink copied to clipboard!
AMQ C++ is distributed as a set of RPM packages for Red Hat Enterprise Linux. Follow these steps to install them.
Use the
subscription-managercommand to subscribe to the required package repositories.Red Hat Enterprise Linux 6
sudo subscription-manager repos --enable=amq-clients-2-for-rhel-6-server-rpms
$ sudo subscription-manager repos --enable=amq-clients-2-for-rhel-6-server-rpmsCopy to Clipboard Copied! Toggle word wrap Toggle overflow Red Hat Enterprise Linux 7
sudo subscription-manager repos --enable=amq-clients-2-for-rhel-7-server-rpms
$ sudo subscription-manager repos --enable=amq-clients-2-for-rhel-7-server-rpmsCopy to Clipboard Copied! Toggle word wrap Toggle overflow Use the
yumcommand to install theqpid-proton-cpp-develandqpid-proton-cpp-docspackages.sudo yum install qpid-proton-cpp-devel qpid-proton-cpp-docs
$ sudo yum install qpid-proton-cpp-devel qpid-proton-cpp-docsCopy to Clipboard Copied! Toggle word wrap Toggle overflow
In order to compile programs using the API, you will also need to install gcc-c++, cmake, and make.
sudo yum install gcc-c++ cmake make
$ sudo yum install gcc-c++ cmake make
2.3. Installing on Microsoft Windows Copy linkLink copied to clipboard!
AMQ C++ is distributed as an SDK zip archive for use with Visual Studio. Follow these steps to install it.
- Open a browser and log in to the Red Hat Customer Portal Product Downloads page at access.redhat.com/downloads.
- Locate the Red Hat AMQ Clients entry in the JBOSS INTEGRATION AND AUTOMATION category.
- Click Red Hat AMQ Clients. The Software Downloads page opens.
- Download the AMQ C++ Client Windows SDK zip file.
- Extract the file contents into a directory of your choosing by right-clicking on the zip file and selecting Extract All.
Chapter 3. Getting started Copy linkLink copied to clipboard!
This chapter guides you through a simple exercise to help you get started using AMQ C++.
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:
Procedure
- 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 queuecommand 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 --anycastCopy to Clipboard Copied! Toggle word wrap Toggle overflow 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 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 names it "AMQ7C++SmokeTest", but you can use any name you like.
mkdir AMQ7C++SmokeTest
$ mkdir AMQ7C++SmokeTestCopy to Clipboard Copied! Toggle word wrap Toggle overflow Enter the new directory.
cd AMQ7C++SmokeTest
$ cd AMQ7C++SmokeTestCopy to Clipboard Copied! Toggle word wrap Toggle overflow Copy all the examples to this directory.
cp -r /usr/share/proton-0.28.0/examples/cpp .
$ cp -r /usr/share/proton-0.28.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.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
$ cmake . $ makeCopy 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 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
$ ./simple_send -m 10Copy to Clipboard Copied! Toggle word wrap Toggle overflow The command line option
-m 10tells the program to send 10 messages.This outputs:
all messages confirmed $
all messages confirmed $Copy to Clipboard Copied! Toggle word wrap Toggle overflow By default the
simple_sendexample 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 theexamplesqueue 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 10Copy to Clipboard Copied! Toggle word wrap Toggle overflow In this case the command line option
-m 10tells the program to exit after receiving 10 messages.Copy to Clipboard Copied! Toggle word wrap Toggle overflow The
simple_recvexample is similar to thesimple_sendexample. It also connects to an AMQP listener on the same machine. By default it subscribes to the AMQP addressexamplesand receives 100 messages.
Chapter 4. Examples Copy linkLink copied to clipboard!
This chapter demonstrates the use of AMQ C++ through example programs.
See the Qpid Proton C++ examples for more sample programs.
The code presented in this guide uses C++11 features. AMQ C++ is also compatible with C++03, but the code will require minor modifications.
4.1. Sending messages Copy linkLink copied to clipboard!
This client program connects to a server using <connection-url>, creates a sender for target <address>, sends a message containing <message-body>, closes the connection, and exits.
Example: Sending messages
Running the example
To run the example program, copy it to a local file, compile it, and execute it from the command line.
g++ send.cpp -o send -std=c++11 -lstdc++ -lqpid-proton-cpp ./send amqp://localhost queue1 hello
$ g++ send.cpp -o send -std=c++11 -lstdc++ -lqpid-proton-cpp
$ ./send amqp://localhost queue1 hello
4.2. Receiving messages Copy linkLink copied to clipboard!
This client program connects to a server using <connection-url>, creates a receiver for source <address>, and receives messages until it is terminated or it reaches <count> messages.
Example: Receiving messages
Running the example
To run the example program, copy it to a local file, compile it, and execute it from the command line.
g++ receive.cpp -o receive -std=c++11 -lstdc++ -lqpid-proton-cpp ./receive amqp://localhost queue1
$ g++ receive.cpp -o receive -std=c++11 -lstdc++ -lqpid-proton-cpp
$ ./receive amqp://localhost queue1
Chapter 5. Using the API Copy linkLink copied to clipboard!
This chapter explains how to use the AMQ C++ API to perform common messaging tasks.
5.1. Basic operation Copy linkLink copied to clipboard!
5.1.1. Handling messaging events Copy linkLink copied to clipboard!
AMQ C++ is an asynchronous event-driven API. To define how the application handles events, the user implements callback methods on the messaging_handler class. These methods are then called as network activity or timers trigger new events.
Example: Handling messaging events
These are only a few common-case events. The full set is documented in the API reference.
5.1.2. Creating a container Copy linkLink copied to clipboard!
The container is the top-level API object. It is the entry point for creating connections, and it is responsible for running the main event loop. It is often constructed with a global event handler.
Example: Creating a container
int main() {
example_handler handler {};
proton::container cont {handler};
cont.run();
}
int main() {
example_handler handler {};
proton::container cont {handler};
cont.run();
}
Setting the container identity
Each container instance has a unique identity called the container ID. When AMQ C++ makes a connection, it sends the container ID to the remote peer. To set the container ID, pass it to the proton::container constructor.
Example: Setting the container identity
proton::container cont {handler, "job-processor-3"};
proton::container cont {handler, "job-processor-3"};
If the user does not set the ID, the library will generate a UUID when the container is constucted.
5.2. Network connections Copy linkLink copied to clipboard!
5.2.1. Connection URLs Copy linkLink copied to clipboard!
Connection URLs encode the information used to establish new connections.
Connection URL syntax
scheme://host[:port]
scheme://host[:port]
-
Scheme - The connection transport, either
amqpfor unencrypted TCP oramqpsfor TCP with SSL/TLS encryption. - Host - The remote network host. The value can be a hostname or a numeric IP address. IPv6 addresses must be enclosed in square brackets.
-
Port - The remote network port. This value is optional. The default value is 5672 for the
amqpscheme and 5671 for theamqpsscheme.
Connection URL examples
amqps://example.com amqps://example.net:56720 amqp://127.0.0.1 amqp://[::1]:2000
amqps://example.com
amqps://example.net:56720
amqp://127.0.0.1
amqp://[::1]:2000
5.2.2. Creating outgoing connections Copy linkLink copied to clipboard!
To connect to a remote server, call the container::connect() method with a connection URL. This is typically done inside the messaging_handler::on_container_start() method.
Example: Creating outgoing connections
See the Section 5.5, “Security” section for information about creating secure connections.
5.2.3. Configuring reconnect Copy linkLink copied to clipboard!
Reconnect allows a client to recover from lost connections. It is used to ensure that the components in a distributed system reestablish communication after temporary network or component failures.
AMQ C++ disables reconnect by default. To enable it, set the reconnect connection option to an instance of the reconnect_options class.
Example: Enabling reconnect
With reconnect enabled, if a connection is lost or a connection attempt fails, the client will try again after a brief delay. The delay increases exponentially for each new attempt.
To control the delays between connection attempts, set the delay, delay_multiplier, and max_delay options. All durations are specified in milliseconds.
To limit the number of reconnect attempts, set the max_attempts option. Setting it to 0 removes any limit.
Example: Configuring reconnect
5.2.4. Configuring failover Copy linkLink copied to clipboard!
AMQ C++ allows you to configure multiple connection endpoints. If connecting to one fails, the client attempts to connect to the next in the list. If the list is exhausted, the process starts over.
To specify alternate connection endpoints, set the failover_urls reconnect option to a list of connection URLs.
Example: Configuring failover
5.3. Message delivery Copy linkLink copied to clipboard!
5.3.1. Sending messages Copy linkLink copied to clipboard!
To send a message, override the on_sendable event handler and call the sender::send() method. The sendable event fires when the proton::sender has enough credit to send at least one message.
Example: Sending messages
5.3.2. Tracking sent messages Copy linkLink copied to clipboard!
When a message is sent, the sender can keep a reference to the tracker object representing the transfer. After the message is delivered, the receiver accepts or rejects it. The sender is notified of the outcome for each tracked delivery.
To monitor the outcome of a sent message, override the on_tracker_accept and on_tracker_reject event handlers and map the delivery state update to the tracker returned from send().
Example: Tracking sent messages
5.3.3. Receiving messages Copy linkLink copied to clipboard!
To receive messages, create a receiver and override the on_message event handler.
Example: Receiving messages
5.3.4. Acknowledging received messages Copy linkLink copied to clipboard!
To explicitly accept or reject a delivery, use the delivery::accept() or delivery::reject() methods in the on_message event handler.
Example: Acknowledging received messages
By default, if you do not explicity acknowledge a delivery, then the library accepts it after on_message returns. To disable this behavior, set the auto_accept receiver option to false.
5.4. Error handling Copy linkLink copied to clipboard!
Errors in AMQ C++ can be handled in two different ways:
- Catching exceptions
- Overriding virtual functions to handle AMQP protocol or connection errors
Catching exceptions
Catching exceptions is the most basic, but least granular, way to handle errors. If an error is not handled using an override in a handler routine, an exception will be thrown and can be caught and handled. An exception thrown in this way will be thrown by the container’s run method.
All of the exceptions that can be thrown by AMQ C++ are descended from proton::error, which in turn is a subclass of std::runtime_error (which is a subclass of std::exception).
The code example below illustrates how a block could be written to catch any exception thrown from AMQ C++.
Example: API-Specific exception handling
If you require no API-specific exception handling, you only need to catch std::exception since proton::error descends from it.
Example: General exception handling
Because all exceptions in a C++ program descend from std::exception, you can write a code block to wrap your main method and display information about any std::exception errors.
Handling connection and protocol errors
You can handle protocol-level errors by overriding the following messaging_handler methods:
-
on_transport_error(proton::transport&) -
on_connection_error(proton::connection&) -
on_session_error(proton::session&) -
on_receiver_error(proton::receiver&) -
on_sender_error(proton::sender&)
These event handling routines are called whenever there is an error condition with the specific object that is in the event. After calling the error handler, the appropriate close handler will also be called.
If not overridden the default error handler will be called with an indication of the error condition that occurred.
There is also a default error handler:
-
on_error(proton::error_condition&)
If one of the more specific error handlers is not overridden, this will be called.
As the close handlers will be called in the event of any error, only error itself need be handled within the error handler. Resource clean up can be managed by close handlers. If there is no error handling that is specific to a particular object it is typical to use the general on_error handler and not have a more specific handler.
5.5. Security Copy linkLink copied to clipboard!
5.5.1. Securing connections with SSL/TLS Copy linkLink copied to clipboard!
AMQ C++ uses SSL/TLS to encrypt communication between clients and servers.
To connect to a remote server with SSL/TLS, set the ssl_client_options connection option and use a connection URL with the amqps scheme. The ssl_client_options constructor takes the filename, directory, or database ID of a CA certificate.
Example: Enabling SSL/TLS
5.5.2. Connecting with a user and password Copy linkLink copied to clipboard!
AMQ C++ can authenticate connections with a user and password.
To specify the credentials used for authentication, set the user and password options on the connect method.
Example: Connecting with a user and password
5.5.3. Configuring SASL authentication Copy linkLink copied to clipboard!
AMQ C++ uses the SASL protocol to perform authentication. SASL can use a number of different authentication mechanisms. When two network peers connect, they exchange their allowed mechanisms, and the strongest mechanism allowed by both is selected.
The client uses Cyrus SASL to perform authentication. Cyrus SASL uses plug-ins to support specific SASL mechanisms. Before you can use a particular SASL mechanism, the relevant plug-in must be installed. For example, you need the cyrus-sasl-plain plug-in in order to use SASL PLAIN authentication.
To see a list of Cyrus SASL plug-ins in Red Hat Enterprise Linux, use the yum search cyrus-sasl command. To install a Cyrus SASL plug-in, use the yum install PLUG-IN command.
By default, AMQ C++ allows all of the mechanisms supported by the local SASL library configuration. To restrict the allowed mechanisms and thereby control what mechanisms can be negotiated, use the sasl_allowed_mechs connection option. It takes a string containing a space-separated list of mechanism names.
Example: Configuring SASL authentication
proton::connection_options opts {};
opts.sasl_allowed_mechs("ANONYMOUS");
container.connect("amqps://example.com", opts);
proton::connection_options opts {};
opts.sasl_allowed_mechs("ANONYMOUS");
container.connect("amqps://example.com", opts);
This example forces the connection to authenticate using the ANONYMOUS mechanism even if the server we connect to offers other options. Valid mechanisms include ANONYMOUS, PLAIN, SCRAM-SHA-256, SCRAM-SHA-1, GSSAPI, and EXTERNAL.
AMQ C++ enables SASL by default. To disable it, set the sasl_enabled connection option to false.
Example: Disabling SASL
proton::connection_options opts {};
opts.sasl_enabled(false);
container.connect("amqps://example.com", opts);
proton::connection_options opts {};
opts.sasl_enabled(false);
container.connect("amqps://example.com", opts);
5.5.4. Authenticating using Kerberos Copy linkLink copied to clipboard!
Kerberos is a network protocol for centrally managed authentication based on the exchange of encrypted tickets. See Using Kerberos for more information.
- Configure Kerberos in your operating system. See Configuring Kerberos to set up Kerberos on Red Hat Enterprise Linux.
Enable the
GSSAPISASL mechanism in your client application.proton::connection_options opts {}; opts.sasl_allowed_mechs("GSSAPI"); container.connect("amqps://example.com", opts);proton::connection_options opts {}; opts.sasl_allowed_mechs("GSSAPI"); container.connect("amqps://example.com", opts);Copy to Clipboard Copied! Toggle word wrap Toggle overflow Use the
kinitcommand to authenticate your user credentials and store the resulting Kerberos ticket.kinit USER@REALM
$ kinit USER@REALMCopy to Clipboard Copied! Toggle word wrap Toggle overflow - Run the client program.
5.6. Timers Copy linkLink copied to clipboard!
AMQ C++ has the ability to execute code after a delay. You can use this to implement time-based behaviors in your application, such as periodically scheduled work or timeouts.
5.6.1. Scheduling deferred work Copy linkLink copied to clipboard!
To defer work for a fixed amount of time, use the schedule method to set the delay and register a function defining the work.
Example: Sending a message after a delay
This example uses the schedule method on the work queue of the sender in order to establish it as the execution context for the work.
5.7. More information Copy linkLink copied to clipboard!
For more information, see the API reference.
Chapter 6. File-based configuration Copy linkLink copied to clipboard!
AMQ C++ can read the configuration options used to establish connections from a local file named connect.json. This enables you to configure connections in your application at the time of deployment.
The library attempts to read the file when the application calls the container connect method without supplying any connection options.
This feature is currently unavailable on Microsoft Windows.
6.1. File locations Copy linkLink copied to clipboard!
If set, AMQ C++ uses the value of the MESSAGING_CONNECT_FILE environment variable to locate the configuration file.
If MESSAGING_CONNECT_FILE is not set, AMQ C++ searches for a file named connect.json at the following locations and in the order shown. It stops at the first match it encounters.
-
$PWD/connect.json, where$PWDis the current working directory of the client process -
$HOME/.config/messaging/connect.json, where$HOMEis the current user home directory -
/etc/messaging/connect.json
6.2. File format Copy linkLink copied to clipboard!
The connect.json file contains JSON data, with additional support for JavaScript comments.
Many of the options have default values, so a simple example need only provide a few details:
Example: A simple connect.json file
{
"host": "example.com",
"user": "alice",
"password": "secret"
}
{
"host": "example.com",
"user": "alice",
"password": "secret"
}
SASL and SSL/TLS options are nested under "sasl" and "tls" namespaces:
Example: A connect.json file with SASL and SSL/TLS options
6.3. Configuration options Copy linkLink copied to clipboard!
The option keys containing a dot (.) represent attributes nested inside a namespace.
| Key | Value type | Default value | Description |
|---|---|---|---|
|
| string |
|
|
|
| string |
| The hostname or IP address of the remote host |
|
| string or number |
| A port number or port literal |
|
| string | None | The user name for authentication |
|
| string | None | The password for authentication |
|
| list or string | None (system defaults) | A JSON list of enabled SASL mechanisms. A bare string represents one mechanism. If none are specified, the client uses the default mechanisms provided by the system. |
|
| boolean |
| Enable mechanisms that send cleartext passwords |
|
| string | None | The filename or database ID of the client certificate |
|
| string | None | The filename or database ID of the private key for the client certificate |
|
| string | None | The filename, directory, or database ID of the CA certificate |
|
| boolean |
| Require a valid server certificate with a matching hostname |
Chapter 7. Multithreading Copy linkLink copied to clipboard!
AMQ C++ supports full multithreading with C++11 and later. Limited multithreading is possible with older versions of C++. See Section 7.5, “Using older versions of C++”.
7.1. Threading model Copy linkLink copied to clipboard!
The container object can handle multiple connections concurrently. As AMQP events occur on connections, the container calls messaging_handler callback functions. Callbacks for any one connection are serialized (not called concurrently), but callbacks for different connections can be safely executed in parallel.
You can assign a handler to a connection in container::connect() or listen_handler::on_accept() using the handler connection option. We recommend creating a separate handler for each connection. That way the handler does not need locks or other synchronization to protect it against concurrent use by library threads. If any non-library threads use the handler concurrently, then you will need synchronization.
7.2. Thread-safety rules Copy linkLink copied to clipboard!
The connection, session, sender, receiver, tracker, and delivery objects are not thread-safe and are subject to the following rules.
-
You must use them only from a
messaging_handlercallback or awork_queuefunction. - You must not use objects belonging to one connection from a callback for another connection.
- You can store AMQ C++ objects in member variables for use in a later callback, provided you respect rule two.
The message object is a value type with the same threading constraints as a standard C++ built-in type. It cannot be concurrently modified.
7.3. Work queues Copy linkLink copied to clipboard!
The work_queue interface provides a safe way to communicate between different connection handlers or between non-library threads and connection handlers.
-
Each connection has an associated
work_queue. - The work queue is thread-safe (C++11 or greater). Any thread can add work.
-
A
workitem is astd::function, and bound arguments are called like an event callback.
When the library calls the work function, it will be serialized safely so that you can treat the work function like an event callback and safely access the handler and AMQ C++ objects stored on it.
7.4. The wake primitive Copy linkLink copied to clipboard!
The connection::wake() method allows any thread to prompt activity on a connection by triggering an on_connection_wake() callback. This is the only thread-safe method on connection.
wake() is a lightweight, low-level primitive for signaling between threads.
-
It does not carry any code or data, unlike
work_queue. -
Multiple calls to
wake()might be coalesced into a singleon_connection_wake(). -
Calls to
on_connection_wake()can occur without any application call towake()since the library useswake()internally.
The semantics of wake() are similar to std::condition_variable::notify_one(). There will be a wakeup, but there must be some shared application state to determine why the wakeup occurred and what, if anything, to do about it.
Work queues are easier to use in many instances, but wake() may be useful if you already have your own external thread-safe queues and need an efficient way to wake a connection to check them for data.
7.5. Using older versions of C++ Copy linkLink copied to clipboard!
Before C++11 there was no standard support for threading in C++. You can use AMQ C++ with threads but with the following limitations.
-
The container will not create threads. It will only use the single thread that calls
container::run(). -
None of the AMQ C++ library classes are thread-safe, including
containerandwork_queue. You need an external lock to usecontainerin multiple threads. The only exception isconnection::wake(). It is thread-safe even in older C++.
The container::schedule() and work_queue APIs accept C++11 lambda functions to define units of work. If you are using a version of C++ that does not support lambdas, you must use the make_work() function instead.
Chapter 8. Interoperability Copy linkLink copied to clipboard!
This chapter discusses how to use AMQ C++ in combination with other AMQ components. For an overview of the compatibility of AMQ components, see the product introduction.
8.1. Interoperating with other AMQP clients Copy linkLink copied to clipboard!
AMQP messages are composed using the AMQP type system. This common format is one of the reasons AMQP clients in different languages are able to interoperate with each other.
When sending messages, AMQ C++ automatically converts language-native types to AMQP-encoded data. When receiving messages, the reverse conversion takes place.
More information about AMQP types is available at the interactive type reference maintained by the Apache Qpid project.
| AMQP type | Description |
|---|---|
| An empty value | |
| A true or false value | |
| A single Unicode character | |
| A sequence of Unicode characters | |
| A sequence of bytes | |
| A signed 8-bit integer | |
| A signed 16-bit integer | |
| A signed 32-bit integer | |
| A signed 64-bit integer | |
| An unsigned 8-bit integer | |
| An unsigned 16-bit integer | |
| An unsigned 32-bit integer | |
| An unsigned 64-bit integer | |
| A 32-bit floating point number | |
| A 64-bit floating point number | |
| A sequence of values of a single type | |
| A sequence of values of variable type | |
| A mapping from distinct keys to values | |
| A universally unique identifier | |
| A 7-bit ASCII string from a constrained domain | |
| An absolute point in time |
| AMQP type | AMQ C++ type before encoding | AMQ C++ type after decoding |
|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| AMQ C++ type before encoding | AMQ JavaScript type | AMQ .NET type |
|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| AMQ C++ type before encoding | AMQ Python type | AMQ Ruby type |
|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| - | - |
|
|
|
|
|
|
|
|
8.2. Interoperating with AMQ JMS Copy linkLink copied to clipboard!
AMQP defines a standard mapping to the JMS messaging model. This section discusses the various aspects of that mapping. For more information, see the AMQ JMS Interoperability chapter.
JMS message types
AMQ C++ provides a single message type whose body type can vary. By contrast, the JMS API uses different message types to represent different kinds of data. The table below indicates how particular body types map to JMS message types.
For more explicit control of the resulting JMS message type, you can set the x-opt-jms-msg-type message annotation. See the AMQ JMS Interoperability chapter for more information.
| AMQ C++ body type | JMS message type |
|---|---|
|
| |
|
| |
|
| |
| Any other type |
8.3. Connecting to AMQ Broker Copy linkLink copied to clipboard!
AMQ Broker is designed to interoperate with AMQP 1.0 clients. Check the following to ensure the broker is configured for AMQP messaging:
- Port 5672 in the network firewall is open.
- The AMQ Broker AMQP acceptor is enabled. See Default acceptor settings.
- The necessary addresses are configured on the broker. See Addresses, Queues, and Topics.
- The broker is configured to permit access from your client, and the client is configured to send the required credentials. See Broker Security.
8.4. Connecting to AMQ Interconnect Copy linkLink copied to clipboard!
AMQ Interconnect works with any AMQP 1.0 client. Check the following to ensure the components are configured correctly:
- Port 5672 in the network firewall is open.
- The router is configured to permit access from your client, and the client is configured to send the required credentials. See Securing network connections.
Appendix A. Using your subscription Copy linkLink copied to clipboard!
AMQ is provided through a software subscription. To manage your subscriptions, access your account at the Red Hat Customer Portal.
Accessing your account
- Go to access.redhat.com.
- If you do not already have an account, create one.
- Log in to your account.
Activating a subscription
- Go to access.redhat.com.
- Navigate to My Subscriptions.
- Navigate to Activate a subscription and enter your 16-digit activation number.
Downloading ZIP and TAR files
To access ZIP or TAR files, use the customer portal to find the relevant files for download. If you are using RPM packages, this step is not required.
- Open a browser and log in to the Red Hat Customer Portal Product Downloads page at access.redhat.com/downloads.
- Locate the Red Hat AMQ entries in the JBOSS INTEGRATION AND AUTOMATION category.
- Select the desired AMQ product. The Software Downloads page opens.
- Click the Download link for your component.
Registering your system for packages
To install RPM packages on Red Hat Enterprise Linux, your system must be registered. If you are using ZIP or TAR files, this step is not required.
- Go to access.redhat.com.
- Navigate to Registration Assistant.
- Select your OS version and continue to the next page.
- Use the listed command in your system terminal to complete the registration.
To learn more see How to Register and Subscribe a System to the Red Hat Customer Portal.
Revised on 2019-07-31 16:08:03 UTC