Using Cloud Message Queue Service
About Using Cloud Message Queue Service
- The transport endpoint for ActiveMQ Advanced Message Queuing Protocol (AMQP) starts with
amqp+ssl
. When creating an AMQP client, you must replaceamqp+ssl
withamqps
in your application code. - If you choose to subscribe to a High Availability (HA) plan, you must use the Failover Transport protocol. For more information on the Failover Transport protocol, refer to the ActiveMQ documentation.
Connect to ActiveMQ Broker using Java Message Service (JMS)
Before You Begin
- Ensure that Java is installed and configured in your system.
- In the project directory, modify the pom.xml file as follows to add the activemq-client.jar and activemq-pool.jar packages to your Java class path:
<dependencies> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-client</artifactId> <version>5.15.8</version> </dependency> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-pool</artifactId> <version>5.15.8</version> </dependency> </dependencies>
About This Task
Cloud Message Queue supports JMS, which allows you to create message queues, and send or receive messages within a distributed communication system that is loosely coupled, reliable, and asynchronous. This topic describes how to connect your application with the message broker using JMS.
Procedure
Create Message Producer using Java Message Service (JMS)
Before You Begin
- Ensure that Java is installed and configured in your system.
- In the project directory, ensure that the pom.xml file is modified as follows to add the activemq-client.jar and activemq-pool.jar packages to your Java class path:
<dependencies> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-client</artifactId> <version>5.15.8</version> </dependency> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-pool</artifactId> <version>5.15.8</version> </dependency> </dependencies>
About This Task
This topic describes how to create a message producer using JMS. Additionally, you can create a connection to the message broker, create a queue, and send messages to the message consumer.
Procedure
Create Message Consumer using Java Message Service (JMS)
Before You Begin
- Ensure that Java is installed and configured in your system.
- In the project directory, ensure that the pom.xml file is modified as follows to add the activemq-client.jar and activemq-pool.jar packages to your Java class path:
<dependencies> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-client</artifactId> <version>5.15.8</version> </dependency> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-pool</artifactId> <version>5.15.8</version> </dependency> </dependencies>
About This Task
This topic describes how to create a message consumer using JMS. Additionally, you can create a connection to the message broker, create a queue, and receive messages from the ActiveMQ message queue.
Procedure
Connect to ActiveMQ Broker with Golang
Before You Begin
import "pack.ag/amqp"
About This Task
The Cloud Message Queue service supports AMQP 1.0, which allows you to connect your application with the message queue broker, create message queues, and send or receive messages.
Code Examples for Connecting to ActiveMQ Broker using AMQP 1.0
- The following code example demonstrates how to create a message producer, and then send messages to the destination application using the message queue:
// Create session client, err := amqp.Dial("amqps://"+host+":"+port, amqp.ConnSASLPlain(username, password), ) if err != nil { fmt.Println(fmt.Sprintf("Dialing AMQP server:", err)) } defer client.Close() // Open a session session, err := client.NewSession() ctx := context.Background() queue := "test-queue" sender, err := session.NewSender( amqp.LinkTargetAddress(queue), ) if err != nil { fmt.Println(fmt.Sprintf("Cannot create session, error:", err)) } ctx, cancel := context.WithTimeout(ctx, 30*time.Second) Sending messages to queue: totalSeconds := 0 maxSeconds := 30 for totalSeconds < maxSeconds { // Generate random UUID for message body. Convert to []byte randomUUID := uuid.NewV4() body := []byte(randomUUID.String()) // Publish a message output := fmt.Sprintf("Published message to test-queue: %s", body) fmt.Println(output) err = sender.Send(ctx, amqp.NewMessage([]byte(body))) if err != nil { fmt.Println(fmt.Sprintf("Failed to publish message, error:", err)) } time.Sleep(5 * time.Second) totalSeconds += 5 } // Close connection sender.Close(ctx) cancel()
- The following code example demonstrates how to create a message receiver, and then receive messages using the message queue:
// Create client client, err := amqp.Dial("amqps://"+host+":"+port, amqp.ConnSASLPlain(username, password), ) if err != nil { fmt.Println(fmt.Sprintf("Dialing AMQP server:", err)) } defer client.Close() // Open a session session, err := client.NewSession() if err != nil { fmt.Println(fmt.Sprintf("Cannot create session, error:", err)) } ctx := context.Background() queue := "test-queue" Create receiver and receive mesages // Create a receiver receiver, err := session.NewReceiver( amqp.LinkSourceAddress(queue), amqp.LinkCredit(10), ) if err != nil { fmt.Println(fmt.Sprintf("Creating receiver link, error:", err)) } defer func() { ctx, cancel := context.WithTimeout(ctx, 35*time.Second) receiver.Close(ctx) cancel() }() for { // Receive next message msg, err := receiver.Receive(ctx) if err != nil { fmt.Println(fmt.Sprintf("Reading message from AMQP error:", err)) } // Accept message msg.Accept() fmt.Println(fmt.Sprintf("Message received: %s\n", msg.GetData())) }
About Connecting the ActiveMQ Broker with Ruby
If your application is built on Ruby, you can use one of these protocols to connect to the ActiveMQ broker and send or receive messages.
Connect to ActiveMQ Broker using Simple (or Streaming) Text Oriented Messaging Protocol (STOMP)
Before You Begin
require 'stomp'
About This Task
Procedure
Connect to ActiveMQ Broker using Message Queuing Telemetry Transport (MQTT)
Before You Begin
require 'mqtt'