Connecting to Amazon MQ
The following design patterns can improve the effectiveness of your application's connection to your Amazon MQ broker.
Topics
- Never Modify or Delete the Amazon MQ Elastic Network Interface
- Always Use Connection Pooling
- Always Use the Failover Transport to Connect to Multiple Broker Endpoints
- Avoid Using Message Selectors
- Prefer Virtual Destinations to Durable Subscriptions
- If using Amazon VPC peering, avoid client IPs in CIDR range 10.0.0.0/16
Never Modify or Delete the Amazon MQ Elastic Network Interface
When you first create an Amazon MQ broker, Amazon MQ provisions an elastic network interface in the Virtual Private Cloud (VPC) under your account and, thus, requires a number of EC2 permissions. The network interface allows your client (producer or consumer) to communicate with the Amazon MQ broker. The network interface is considered to be within the service scope of Amazon MQ, despite being part of your account's VPC.
Warning
You must not modify or delete this network interface. Modifying or deleting the network interface can cause a permanent loss of connection between your VPC and your broker.

Always Use Connection Pooling
In a scenario with a single producer and single consumer (such as the Getting Started with Amazon MQ
tutorial), you can use a single ActiveMQConnectionFactory
// Create a connection factory.
final ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(wireLevelEndpoint);
// Pass the sign-in credentials.
connectionFactory.setUserName(activeMqUsername);
connectionFactory.setPassword(activeMqPassword);
// Establish a connection for the consumer.
final Connection consumerConnection = connectionFactory.createConnection();
consumerConnection.start();
However, in more realistic scenarios with multiple producers and consumers, it can
be costly and inefficient to create a large number of connections for multiple
producers. In these scenarios, you should group multiple producer requests using the
PooledConnectionFactory
Note
Message consumers should never use the
PooledConnectionFactory
class.
// Create a connection factory.
final ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(wireLevelEndpoint);
// Pass the sign-in credentials.
connectionFactory.setUserName(activeMqUsername);
connectionFactory.setPassword(activeMqPassword);
// Create a pooled connection factory.
final PooledConnectionFactory pooledConnectionFactory = new PooledConnectionFactory();
pooledConnectionFactory.setConnectionFactory(connectionFactory);
pooledConnectionFactory.setMaxConnections(10);
// Establish a connection for the producer.
final Connection producerConnection = pooledConnectionFactory.createConnection();
producerConnection.start();
Always Use the Failover Transport to Connect to Multiple Broker Endpoints
If you need your application to connect to multiple broker endpoints—for example,
when you use an active/standby deployment mode or when you migrate from an on-premises message broker to
Amazon MQ—use the Failover
Transport
failover:(ssl://b-1234a5b6-78cd-901e-2fgh-3i45j6k178l9-1.mq.us-east-2.amazonaws.com:61617,ssl://b-9876l5k4-32ji-109h-8gfe-7d65c4b132a1-2.mq.us-east-2.amazonaws.com:61617)?randomize=true
Avoid Using Message Selectors
It is possible to use JMS
selectors
In general, avoid letting consumers route messages because, for optimal decoupling of consumers and producers, both the consumer and the producer should be ephemeral.
Prefer Virtual Destinations to Durable Subscriptions
A durable subscription
If using Amazon VPC peering, avoid client IPs in CIDR range 10.0.0.0/16
If you are setting up Amazon VPC peering between on-premise infrastructure and your Amazon MQ broker, you must not configure client connections with IPs in CIDR range 10.0.0.0/16
.