RabbitMQ
Key takeaways
RabbitMQMessageQueue | Component used to send and receive messages via RabbitMQ. |
Open() | Method used to connect to RabbitMQ. |
Send() | Method used to send messages to RabbitMQ. |
Receive() | Method used to receive messages from RabbitMQ. |
queue, exchange, host, port (or uri) | Required configuration parameters. |
Introduction
In this tutorial, you will learn how to send and receive messages via RabbitMQ. First, we will see the necessary pre-requisites. Then, we will learn how to implement this component, connect to RabbitMQ and send and receive messages from it. Finally, we will combine the different sections into a program and summarize all the learned concepts.
The RabbitMQMessageQueue component
This component is part of the RabbitMQ module and represents a message queue that sends and receives messages via the RabbitMQ message broker. Furthermore, it is a subclass of MessageQueue. The following sections explain how to use it.
Pre-requisites
In order to use this component, we need to import it first. The following code shows how to do this:
import { RabbitMQMessageQueue } from 'pip-services4-rabbitmq-node'
import (
rqueues "github.com/pip-services4/pip-services4-go/pip-services4-rabbitmq-go/queues"
)
Implementing our component
First, and after having imported our component, we create an instance of it:
let queue = new RabbitMQMessageQueue();
queue := rqueues.NewEmptyRabbitMQMessageQueue("my_topic")
Configuring our component
After creating an instance of our component, we need to configure it. Here, an important point is to understand the different parameters involved in this operation. The following table summarizes them:
To configure our object, we use the configure() method, which accepts a ConfigParams object as input. In our example, we define the RabbitMQ exchange, the queue name, the host, and the port. We also assign the value true to auto_create. In this manner, if the queue doesn’t exist in RabbitMQ, it is created. Furthermore, for the example’s purpose, we consider the guest user. But, if we want to refer to another user, we also need to specify the necessary credentials (username and password).
import { ConfigParams } from "pip-services4-components-node";
queue.configure(ConfigParams.fromTuples(
"exchange", "myqueue", // rabbitmq exchange type
"queue", "myqueue", // queue name
"options.auto_create", true, // autocreate queue
"connection.host", "localhost",
"connection.port", 5672
// if need credentials
//"credential.username", "user",
//"credential.password", "pass123"
));
import (
"context"
cconf "github.com/pip-services4/pip-services4-go/pip-services4-components-go/config"
rqueues "github.com/pip-services4/pip-services4-go/pip-services4-rabbitmq-go/queues"
)
queue.Configure(context.Background(), cconf.NewConfigParamsFromTuples(
"exchange", "myqueue", // rabbitmq exchange type
"queue", "myqueue", // queue name
"options.auto_create", true, // autocreate queue
"connection.host", "localhost",
"connection.port", 5672,
// if need credentials
//"credential.username", "user",
//"credential.password", "pass123"
))
Connecting to RabbitMQ
To connect to RabbitMQ, we use the Open() method, which requires the context as an input parameter. The following example shows how to connect our previously defined queue:
await queue.open(ctx);
err := queue.Open(context.Background())
if err != nil {
panic(err)
}
Creating and sending a message
Once connected, we can send a message to RabbitMQ. For this, we use the Send() method, which accepts the corntext and a MessageEnvelope object as inputs. This last object contains the context, message type, and message content as inputs. The following code shows how to do this:
import { MessageEnvelope } from 'pip-services4-messaging-node';
await queue.send(ctx, new MessageEnvelope(null, "mymessage", "ABC"));
import cqueues "github.com/pip-services4/pip-services4-go/pip-services4-messaging-go/queues"
err = queue.Send(context.Background(), cqueues.NewMessageEnvelope("123", "mymessage", []byte("ABC")))
Receiving a message
To receive a message, we use the Receive() method, which has the context and the waiting time in milliseconds as input parameters. The following example shows how to use it:
let received = await queue.receive(ctx, 10000);
received, err := queue.Receive(context.Background(), 0)
Final code
Now, we assemble all that was learned into one program. The result is:
import { ConfigParams } from "pip-services4-components-node";
import { RabbitMQMessageQueue } from 'pip-services4-rabbitmq-node'
import { MessageEnvelope } from 'pip-services4-messaging-node';
export async function main() {
let queue = new RabbitMQMessageQueue();
queue.configure(ConfigParams.fromTuples(
"exchange", "myqueue", // rabbitmq exchange type
"queue", "myqueue", // queue name
"options.auto_create", true, // autocreate queue
"connection.host", "localhost",
"connection.port", 5672
// if need credentials
//"credential.username", "user",
//"credential.password", "pass123"
));
await queue.open(ctx);
await queue.send(ctx, new MessageEnvelope(null, "mymessage", "ABC"));
let received = await queue.receive(ctx, 10000);
console.log(received.getMessageAsString());
console.log("Task Completed");
}
import (
"context"
"fmt"
cconf "github.com/pip-services4/pip-services4-go/pip-services4-components-go/config"
cqueues "github.com/pip-services4/pip-services4-go/pip-services4-messaging-go/queues"
rqueues "github.com/pip-services4/pip-services4-go/pip-services4-rabbitmq-go/queues"
)
func main() {
// Create and configure a component
queue := rqueues.NewEmptyRabbitMQMessageQueue("my_topic")
queue.Configure(context.Background(), cconf.NewConfigParamsFromTuples(
"exchange", "myqueue", // rabbitmq exchange type
"queue", "myqueue", // queue name
"options.auto_create", true, // autocreate queue
"connection.host", "localhost",
"connection.port", 5672,
// if need credentials
"credential.username", "user",
"credential.password", "password",
))
err := queue.Open(context.Background())
if err != nil {
panic(err)
}
err = queue.Send(context.Background(), cqueues.NewMessageEnvelope("123", "mymessage", []byte("ABC")))
if err != nil {
panic(err)
}
received, err := queue.Receive(context.Background(), 10000)
if err != nil {
panic(err)
}
fmt.Println(received.GetMessageAsString())
fmt.Println("Task completed")
}
Which, after running, produces the following output that confirms the message was received and sent by RabbitMQ:
Wrapping up
In this tutorial, we have seen how to communicate with RabbitMQ by sending and receiving messages. First, we learned the basics of the RabbitMQMessageQueue component and how to import, implement and configure it. Then, we understood the different configuration parameters. Finally, we saw how to send and receive messages and created a program that combined all the learned concepts.