Tutorial: Connecting a Java Application to Your Amazon MQ Broker
After you create an Amazon MQ broker, you can connect your application to it. The following examples show how you can use the Java Message Service (JMS) to create a connection to the broker, create a queue, and send a message. For a complete, working Java example, see Working Java Example.
You can connect to ActiveMQ brokers using various ActiveMQ clients. We recommend using the ActiveMQ Client.
Important
To ensure that your broker is accessible within your VPC, you must enable the enableDnsHostnames and enableDnsSupport
VPC attributes. For more information, see DNS Support in your VPC in the Amazon VPC User Guide.
Topics
Prerequisites
Enable Inbound Connections
-
Sign in to the Amazon MQ console.
-
From the broker list, choose the name of your broker (for example, MyBroker).
-
On the
MyBrokerpage, in the Connections section, note the addresses and ports of the broker's ActiveMQ Web Console URL and wire-level protocols. -
In the Details section, choose the name of your security group or
.
The Security Groups section of the VPC Dashboard is displayed.
-
Choose your security group, choose Inbound Rules, and then choose Edit.
-
Create an inbound rule for each address and port that you want to be publicly accessible (the following example shows how to do this for an ActiveMQ Web Console).
-
For Type, select Custom TCP Rule.
TCP (6) is selected for Protocol.
-
For Port, type the ActiveMQ Web Console port (
8162). -
For Source, type the IP address of the system that you want to be able to access the ActiveMQ Web Console (for example,
192.0.2.1). -
Choose Save.
Your broker can now accept inbound connections.
-
Add Java Dependencies
To allow your application to work with ActiveMQ, add the activemq-client.jar
and activemq-pool.jar packages to your Java build class path. The following
example shows these dependencies in your Maven project's pom.xml file.
<dependencies> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-client</artifactId> <version>5.15.0</version> </dependency> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-pool</artifactId> <version>5.15.0</version> </dependency> <dependencies>
For more information about activemq-client.jar, see Initial
Configuration in the Apache ActiveMQ documentation.
To create a message producer and send a message
-
Create a JMS pooled connection factory for the message producer using your broker's endpoint and then call the
createConnectionmethod against the factory.Note
For an active/standby broker for high availability, Amazon MQ provides two ActiveMQ Web Console URLs, but only one URL is active at a time. Likewise, Amazon MQ provides two endpoints for each wire-level protocol, but only one endpoint is active in each pair at a time. The
-1and-2suffixes denote a redundant pair. For more information, see Amazon MQ Broker Architecture).For wire-level protocol endpoints, you can allow your application to connect to either endpoint by using the Failover Transport.
// Create a connection factory. ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("ssl://b-1234a5b6-78cd-901e-2fgh-3i45j6k178l9-1.mq.us-east-1.amazonaws.com:61617"); // Specify the username and password. connectionFactory.setUserName("MyUsername123"); connectionFactory.setPassword("MyPassword456"); // Create a pooled connection factory. PooledConnectionFactory pooledConnectionFactory = new PooledConnectionFactory(); pooledConnectionFactory.setConnectionFactory(connectionFactory); pooledConnectionFactory.setMaxConnections(10); // Establish a connection for the producer. Connection producerConnection = pooledConnectionFactory.createConnection(); producerConnection.start();Note
Message producers should use the
PooledConnectionFactoryclass. For more information, see Always Use Connection Pooling. -
Create a session, a queue named
MyQueue, and a message producer.// Create a session. Session producerSession = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE); // Create a queue named "MyQueue". Destination producerDestination = producerSession.createQueue("MyQueue"); // Create a producer from the session to the queue. MessageProducer producer = producerSession.createProducer(producerDestination); producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); -
Create the message string
"Hello from Amazon MQ!"and then send the message.// Create a message. String text = "Hello from Amazon MQ!"; TextMessage producerMessage = producerSession.createTextMessage(text); // Send the message. producer.send(producerMessage); System.out.println("Message sent."); -
Clean up the producer.
producer.close(); producerSession.close(); producerConnection.close();
To create a message consumer and receive the message
-
Create a JMS connection factory for the message producer using your broker's endpoint and then call the
createConnectionmethod against the factory.// Create a connection factory. ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("ssl://b-1234a5b6-78cd-901e-2fgh-3i45j6k178l9-1.mq.us-east-1.amazonaws.com:61617"); // Specify the username and password. connectionFactory.setUserName("MyUserName"); connectionFactory.setPassword("MyPassWord123"); // Establish a connection for the consumer. Connection consumerConnection = connectionFactory.createConnection(); consumerConnection.start();Note
Message consumers should not use the
PooledConnectionFactoryclass. For more information, see Always Use Connection Pooling. -
Create a session, a queue named
MyQueue, and a message consumer.// Create a session. Session consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE); // Create a queue named "MyQueue". Destination consumerDestination = consumerSession.createQueue("MyQueue"); // Create a message consumer from the session to the queue. MessageConsumer consumer = consumerSession.createConsumer(consumerDestination); -
Begin to wait for messages and receive the message when it arrives.
// Begin to wait for messages. Message consumerMessage = consumer.receive(1000); // Receive the message when it arrives. TextMessage consumerTextMessage = (TextMessage) consumerMessage; System.out.println("Message received: " + consumerTextMessage.getText());Note
Unlike AWS messaging services (such as Amazon SQS), the consumer is constantly connected to the broker.
-
Close the consumer, session, and connection.
consumer.close(); consumerSession.close(); consumerConnection.close(); pooledConnectionFactory.stop();




