Chapter 7. Network Connections: Protocols
AMQ Broker has a pluggable protocol architecture, so that you can easily enable one or more protocols for a network connection.
The broker supports the following protocols:
In addition to the protocols above, the broker also supports its own native protocol known as "Core". Past versions of this protocol were known as "HornetQ" and used by Red Hat JBoss Enterprise Application Platform.
7.1. Configuring a Network Connection to Use a Protocol
You must associate a protocol with a network connection before you can use it. (See Network Connections: Acceptors and Connectors for more information about how to create and configure network connections.) The default configuration, located in the file BROKER_INSTANCE_DIR/etc/broker.xml
, includes several connections already defined. For convenience, AMQ Broker includes an acceptor for each supported protocol, plus a default acceptor that supports all protocols.
Default acceptors in broker.xml
<configuration> <core> ... <acceptors> <!-- Default ActiveMQ Artemis Acceptor. Multi-protocol adapter. Currently supports ActiveMQ Artemis Core, OpenWire, STOMP, AMQP, MQTT, and HornetQ Core. --> <acceptor name="artemis">tcp://0.0.0.0:61616?tcpSendBufferSize=1048576;tcpReceiveBufferSize=1048576</acceptor> <!-- AMQP Acceptor. Listens on default AMQP port for AMQP traffic.--> <acceptor name="amqp">tcp://0.0.0.0:5672?protocols=AMQP</acceptor> <!-- STOMP Acceptor. --> <acceptor name="stomp">tcp://0.0.0.0:61613?protocols=STOMP</acceptor> <!-- HornetQ Compatibility Acceptor. Enables HornetQ Core and STOMP for legacy HornetQ clients. --> <acceptor name="hornetq">tcp://0.0.0.0:5445?protocols=HORNETQ,STOMP</acceptor> <!-- MQTT Acceptor --> <acceptor name="mqtt">tcp://0.0.0.0:1883?protocols=MQTT</acceptor> </acceptors> ... </core> </configuration>
The only requirement to enable a protocol is to add the protocols
parameter to the URI query string. The value of the parameter must be a comma separated list of protocol names. If the protocol parameter is omitted from the URI all protocols are enabled.
For example, to create an acceptor for receiving messages on port 3232 using the AMQP protocol, follow these steps:
-
Open the configuration file
BROKER_INSTANCE_DIR/etc/broker.xml
-
Add the following line to the
<acceptors>
stanza:
<acceptor name="ampq">tcp://0.0.0.0:3232?protocols=AMQP</acceptor>
7.2. Using AMQP with a Network Connection
The broker supports the AMQP 1.0 specification. An AMQP link is a uni-directional protocol for messages between a source and a target, that is, a client and the broker.
Procedure
-
Open the configuration file
BROKER_INSTANCE_DIR/etc/broker.xml
-
Add or configure an
acceptor
to receive AMQP clients by including theprotocols
parameter with a value ofAMQP
as part of the URI, as shown in the following example:
<acceptors> <acceptor name="amqp-acceptor">tcp://localhost:5672?protocols=AMQP</acceptor> ... </acceptors>
In the preceding example, the broker accepts AMQP 1.0 clients on port 5672, which is the default AMQP port.
An AMQP link has two endpoints, a sender and a receiver. When senders transmit a message, the broker converts it into an internal format, so it can be forwarded to its destination on the broker. Receivers connect to the destination at the broker and convert the messages back into AMQP before they are delivered.
If an AMQP link is dynamic, a temporary queue is created and either the remote source or the remote target address is set to the name of the temporary queue. If the link is not dynamic, the address of the remote target or source is used for the queue. If the remote target or source does not exist, an exception is sent.
A link target can also be a Coordinator, which is used to handle the underlying session as a transaction, either rolling it back or committing it.
AMQP allows the use of multiple transactions per session, amqp:multi-txns-per-ssn
, however the current version of AMQ Broker will support only single transactions per session.
The details of distributed transactions (XA) within AMQP are not provided in the 1.0 version of the specification. If your environment requires support for distributed transactions, it is recommended that you use the AMQ Core Protocol JMS.
See the AMQP 1.0 specification for more information about the protocol and its features.
7.2.1. Using an AMQP Link as a Topic
Unlike JMS, the AMQP protocol does not include topics. However, it is still possible to treat AMQP consumers or receivers as subscriptions rather than just consumers on a queue. By default, any receiving link that attaches to an address with the prefix jms.topic.
is treated as a subscription, and a subscription queue is created. The subscription queue is made durable or volatile, depending on how the Terminus Durability is configured, as captured in the following table:
To create this kind of subscription for a multicast-only queue… | Set Terminus Durability to this… |
---|---|
Durable |
|
Non-durable |
|
The name of a durable queue is composed of the container ID and the link name, for example my-container-id:my-link-name
.
AMQ Broker also supports the qpid-jms client and will respect its use of topics regardless of the prefix used for the address.
7.2.2. Configuring AMQP Security
The broker supports AMQP SASL Authentication. See Security for more information about how to configure SASL-based authentication on the broker.
7.3. Using MQTT with a Network Connection
The broker supports MQTT v3.1.1 (and also the older v3.1 code message format). MQTT is a lightweight, client to server, publish/subscribe messaging protocol. MQTT reduces messaging overhead and network traffic, as well as a client’s code footprint. For these reasons, MQTT is ideally suited to constrained devices such as sensors and actuators and is quickly becoming the de facto standard communication protocol for Internet of Things(IoT).
Procedure
-
Open the configuration file
BROKER_INSTANCE_DIR/etc/broker.xml
- Add an acceptor with the MQTT protocol enabled. For example:
<acceptors> <acceptor name="mqtt">tcp://localhost:1883?protocols=MQTT</acceptor> ... </acceptors>
MQTT comes with a number of useful features including:
- Quality of Service
- Each message can define a quality of service that is associated with it. The broker will attempt to deliver messages to subscribers at the highest quality of service level defined.
- Retained Messages
- Messages can be retained for a particular address. New subscribers to that address receive the last-sent retained message before any other messages, even if the retained message was sent before the client connected.
- Wild card subscriptions
- MQTT addresses are hierarchical, similar to the hierarchy of a file system. Clients are able to subscribe to specific topics or to whole branches of a hierarchy.
- Will Messages
- Clients are able to set a "will message" as part of their connect packet. If the client abnormally disconnects, the broker will publish the will message to the specified address. Other subscribers receive the will message and can react accordingly.
The best source of information about the MQTT protocol is in the specification. The MQTT v3.1.1 specification can be downloaded from the OASIS website.
7.4. Using OpenWire with a Network Connection
The broker supports the OpenWire protocol, which allows a JMS client to talk directly to a broker. Use this protocol to communicate with older versions of AMQ Broker.
Currently AMQ Broker supports OpenWire clients that use standard JMS APIs only.
Procedure
-
Open the configuration file
BROKER_INSTANCE_DIR/etc/broker.xml
Add or modify an
acceptor
so that it includesOPENWIRE
as part of theprotocol
parameter, as shown in the following example:<acceptors> <acceptor name="openwire-acceptor">tcp://localhost:61616?protocols=OPENWIRE</acceptor> ... </acceptors>
In the preceding example, the broker will listen on port 61616 for incoming OpenWire commands.
For more details, see the examples located under INSTALL_DIR/examples/protocols/openwire
.
7.5. Using STOMP with a Network Connection
STOMP is a text-orientated wire protocol that allows STOMP clients to communicate with STOMP Brokers. The broker supports STOMP 1.0, 1.1 and 1.2. STOMP clients are available for several languages and platforms making it a good choice for interoperability.
Procedure
-
Open the configuration file
BROKER_INSTANCE_DIR/etc/broker.xml
-
Configure an existing
acceptor
or create a new one and include aprotocols
parameter with a value ofSTOMP
, as below.
<acceptors> <acceptor name="stomp-acceptor">tcp://localhost:61613?protocols=STOMP</acceptor> ... </acceptors>
In the preceding example, the broker accepts STOMP connections on the port 61613
, which is the default.
See the stomp
example located under INSTALL_DIR/examples/protocols
for an example of how to configure a broker with STOMP.
7.5.1. Knowing the Limitations When Using STOMP
When using STOMP, the following limitations apply:
-
The broker currently does not support virtual hosting, which means the
host
header inCONNECT
frames are ignored. -
Message acknowledgements are not transactional. The
ACK
frame cannot be part of a transaction, and it is ignored if itstransaction
header is set).
7.5.2. Providing IDs for STOMP Messages
When receiving STOMP messages through a JMS consumer or a QueueBrowser, the messages do not contain any JMS properties, for example JMSMessageID
, by default. However, you can set a message ID on each incoming STOMP message by using a broker paramater.
Procedure
-
Open the configuration file
BROKER_INSTANCE_DIR/etc/broker.xml
-
Set the
stompEnableMessageId
parameter totrue
for theacceptor
used for STOMP connections, as shown in the following example:
<acceptors> <acceptor name="stomp-acceptor">tcp://localhost:61613?protocols=STOMP;stompEnableMessageId=true</acceptor> ... </acceptors>
By using the stompEnableMessageId
parameter, each stomp message sent using this acceptor has an extra property added. The property key is amq-message-id
and the value is a String representation of an internal message id prefixed with “STOMP”, as shown in the following example:
amq-message-id : STOMP12345
If stompEnableMessageId
is not specified in the configuration, the default value is false
.
7.5.3. Setting a Connection’s Time to Live (TTL)
STOMP clients must send a DISCONNECT
frame before closing their connections. This allows the broker to close any server-side resources, such as sessions and consumers. However, if STOMP clients exit without sending a DISCONNECT frame, or if they fail, the broker will have no way of knowing immediately whether the client is still alive. STOMP connections therefore are configured to have a "Time to Live" (TTL) of 1 minute. The means that the broker stops the connection to the STOMP client if it has been idle for more than one minute.
Procedure
-
Open the configuration file
BROKER_INSTANCE_DIR/etc/broker.xml
-
Add the
connectionTtl
parameter to URI of theacceptor
used for STOMP connections, as shown in the following example:
<acceptors> <acceptor name="stomp-acceptor">tcp://localhost:61613?protocols=STOMP;connectionTtl=20000</acceptor> ... </acceptors>
In the preceding example, any stomp connection that using the stomp-acceptor
will have its TTL set to 20 seconds.
Version 1.0 of the STOMP protocol does not contain any heartbeat frame. It is therefore the user’s responsibility to make sure data is sent within connection-ttl or the broker will assume the client is dead and clean up server-side resources. With version 1.1, you can use heart-beats to maintain the life cycle of stomp connections.
Overriding the Broker’s Default Time to Live (TTL)
As noted, the default TTL for a STOMP connection is one minute. You can override this value by adding the connection-ttl-override
attribute to the broker configuration.
Procedure
-
Open the configuration file
BROKER_INSTANCE_DIR/etc/broker.xml
-
Add the
connection-ttl-override
attribute and provide a value in milliseconds for the new default. It belongs inside the<core>
stanza, as below.
<configuration ...> ... <core ...> ... <connection-ttl-override>30000</connection-ttl-override> ... </core> <configuration>
In the preceding example, the default Time to Live (TTL) for a STOMP connection is set to 30 seconds, 30000
milliseconds.
7.5.4. Sending and Consuming STOMP Messages from JMS
STOMP is mainly a text-orientated protocol. To make it simpler to interoperate with JMS, the STOMP implementation checks for presence of the content-length
header to decide how to map a STOMP message to JMS.
If you want a STOMP message to map to a … | The message should…. |
---|---|
JMS TextMessage |
Not include a |
JMS BytesMessage |
Include a |
The same logic applies when mapping a JMS message to STOMP. A STOMP client can confirm the presence of the content-length
header to determine the type of the message body (string or bytes).
See the STOMP specification for more information about message headers.
7.5.5. Mapping STOMP Destinations to AMQ Broker Addresses and Queues
When sending messages and subscribing, STOMP clients typically include a destination
header. Destination names are string values, which are mapped to a destination on the broker. In AMQ Broker, these destinations are mapped to addresses and queues. See the STOMP specification for more information about the destination frame.
Take for example a STOMP client that sends the following message (headers and body included):
SEND destination:/my/stomp/queue hello queue a ^@
In this case, the broker will forward the message to any queues associated with the address /my/stomp/queue
.
For example, when a STOMP client sends a message (by using a SEND
frame), the specified destination is mapped to an address.
It works the same way when the client sends a SUBSCRIBE
or UNSUBSCRIBE
frame, but in this case AMQ Broker maps the destination
to a queue.
SUBSCRIBE destination: /other/stomp/queue ack: client ^@
In the preceding example, the broker will map the destination
to the queue /other/stomp/queue
.
Mapping STOMP Destinations to JMS Destinations
JMS destinations are also mapped to broker addresses and queues. If you want to use STOMP to send messages to JMS destinations, the STOMP destinations must follow the same convention:
Send or subscribe to a JMS Queue by prepending the queue name by
jms.queue.
. For example, to send a message to theorders
JMS Queue, the STOMP client must send the frame:SEND destination:jms.queue.orders hello queue orders ^@
Send or subscribe to a JMS Topic by prepending the topic name by
jms.topic.
. For example, to subscribe to thestocks
JMS Topic, the STOMP client must send a frame similar to the following:SUBSCRIBE destination:jms.topic.stocks ^@