Menu
Amazon MQ
Developer Guide

Working Example of Using Java Message Service (JMS) with ActiveMQ

The following example Java code connects to a broker, creates a queue, and sends and receives a message. For a detailed breakdown and explanation of this code, see Connecting a Java Application to Your Broker.

Quick Start

If you are experienced with Java Message Service (JMS) and ActiveMQ, you can quickly try out Amazon MQ functionality using Apache Maven.

  1. Download AmazonMQExample_1.1.zip

  2. Add your broker endpoint and your username and password to the AmazonMQExample.java file.

  3. From the directory with the pom.xml file, run the mvn compile exec:java command.

Prerequisites

Enable Inbound Connections

  1. Sign in to the Amazon MQ console.

  2. From the broker list, choose the name of your broker (for example, MyBroker).

  3. On the MyBroker page, in the Connections section, note the addresses and ports of the broker's ActiveMQ Web Console URL and wire-level protocols.

  4. In the Details section, under Security and network, choose the name of your security group or .

    The Security Groups page of the EC2 Dashboard is displayed.

  5. From the security group list, choose your security group.

  6. At the bottom of the page, choose Inbound, and then choose Edit.

  7. In the Edit inbound rules dialog box, add a rule for every URL or endpoint that you want to be publicly accessible (the following example shows how to do this for an ActiveMQ Web Console).

    1. Choose Add Rule.

    2. For Type, select Custom TCP.

    3. For Port Range, type the ActiveMQ Web Console port (8162).

    4. For Source, leave Custom selected and then 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).

    5. Choose Save.

      Your broker can now accept inbound connections.

Add Java Dependencies

To allow your application to work with ActiveMQ, ensure that the activemq-client.jar and activemq-pool.jar packages are added 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.

Working Java Example

/* * Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. * A copy of the License is located at * * http://aws.amazon.com/apache2.0 * * or in the "license" file accompanying this file. This file is distributed * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either * express or implied. See the License for the specific language governing * permissions and limitations under the License. * */ import org.apache.activemq.jms.pool.PooledConnectionFactory; import org.apache.activemq.ActiveMQConnectionFactory; import javax.jms.Connection; import javax.jms.DeliveryMode; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageConsumer; import javax.jms.MessageProducer; import javax.jms.Session; import javax.jms.TextMessage; public class AmazonMQExample { public static void main(String[] args) throws JMSException { // 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(); // 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 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(); // Establish a connection for the consumer. // Note: Consumers should not use PooledConnectionFactory. Connection consumerConnection = connectionFactory.createConnection(); consumerConnection.start(); // 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. Message consumerMessage = consumer.receive(1000); // Receive the message when it arrives. TextMessage consumerTextMessage = (TextMessage) consumerMessage; System.out.println("Message received: " + consumerTextMessage.getText()); // Clean up the consumer. consumer.close(); consumerSession.close(); consumerConnection.close(); pooledConnectionFactory.stop(); } }