1. 소개

Apache ActiveMQ 기반의 서비스인 Amazon MQ가 발표되었다. 즉, 매니지드 메세지 브로커 서비스이다.


2. 서비스

  • ActiveMQ 콘솔
  • 프로토콜 : JMS, NMS, AMQP, STOMP, MQTT 및 WebSocket

3. 콘솔


4. 애플리케이션에서 Amazon MQ 연결 및 메세지 전송

4-1. pom.xml 파일에 추가

<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>

4-2. JMS Connection 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("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();

4-3. Session 및 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);

4-4. 메세지 생성 및 전송

// 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.");

4-5. Producer 초기화

producer.close();
producerSession.close();
producerConnection.close();