Working with Message Properties
1. Setting Delivery Mode
| Property | Values | Description |
|---|---|---|
| delivery_mode | 1 or 2 |
Defines if the message is persistent (2) or transient (1) |
Example: Setting Delivery Mode in Python
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
# Declare a queue
channel.queue_declare(queue='my_queue')
# Publish message with persistent delivery mode
message = 'Hello, RabbitMQ!'
channel.basic_publish(
exchange='',
routing_key='my_queue',
body=message,
properties=pika.BasicProperties(
delivery_mode=2 # Make message persistent
)
)
print(" [x] Sent %r" % message)
connection.close()
2. Setting Content Type
| Property | Value | Description |
|---|---|---|
| content-type | application/json |
Specifies the MIME type of the message body |
| content-encoding | utf-8 |
Encoding of the message body (e.g., gzip, base64) |
Example: Setting Content Type in a Message
const amqp = require('amqplib');
async function sendMessage() {
const connection = await amqp.connect('amqp://localhost');
const channel = await connection.createChannel();
const queue = 'my-queue';
await channel.assertQueue(queue, { durable: true });
const message = {
id: '12345',
data: { name: 'John', age: 30 }
};
channel.sendToQueue(
queue,
Buffer.from(JSON.stringify(message)),
{
contentType: 'application/json',
contentEncoding: 'utf-8'
}
);
await channel.close();
await connection.close();
}
sendMessage().catch(console.error);
3. Setting Content Encoding
| Property | Value | Description |
|---|---|---|
| content_encoding | utf-8 |
Specifies the content encoding of the message body. Common values include utf-8, ascii, and base64. |
| content_type | application/json |
Defines the media type of the message body, such as application/json, text/plain, or application/xml. |
Example: Setting Content Encoding in a Message
const amqp = require('amqplib');
async function sendMessage() {
const connection = await amqp.connect('amqp://localhost');
const channel = await connection.createChannel();
const queue = 'my-queue';
await channel.assertQueue(queue, { durable: true });
const message = {
id: '12345',
data: 'Hello, RabbitMQ!',
};
const content = JSON.stringify(message);
const options = {
contentType: 'application/json',
contentEncoding: 'utf-8',
};
channel.sendToQueue(queue, Buffer.from(content), options);
console.log('Message sent with content encoding set to utf-8');
await channel.close();
await connection.close();
}
sendMessage().catch(console.error);
4. Setting Message ID
| Property | Description | Values |
|---|---|---|
| messageId | Unique identifier for the message | string |
Example: Setting Message ID in Java
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
public class MessageIdExample {
public static void main(String[] args) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
try (Connection connection = factory.newConnection();
Channel channel = connection.createChannel()) {
String message = "Hello RabbitMQ!";
// Setting a custom message ID
channel.basicPublish("", "queue_name", null, message.getBytes());
}
}
}
5. Setting Correlation ID
| Property | Description | Value |
|---|---|---|
| correlation_id | Identifier used to correlate related messages | string |
Example: Setting Correlation ID in a Message
const amqp = require('amqplib');
async function sendMessage() {
const connection = await amqp.connect('amqp://localhost');
const channel = await connection.createChannel();
const queue = 'task_queue';
await channel.assertQueue(queue, { durable: true });
const message = {
text: 'Process this task',
correlation_id: 'req-12345'
};
channel.sendToQueue(queue, Buffer.from(JSON.stringify(message)), {
persistent: true,
correlationId: message.correlation_id
});
console.log(' [x] Sent %s', message.text);
await channel.close();
await connection.close();
}
sendMessage().catch(console.error);
6. Setting Reply-To Queue
| Property | Value | Description |
|---|---|---|
| reply_to | string |
Specifies the queue name to which the reply should be sent. If not set, no reply is expected. |
Example: Setting Reply-To Queue in Message Properties
const amqp = require('amqplib');
async function sendMessage() {
const connection = await amqp.connect('amqp://localhost');
const channel = await connection.createChannel();
const queueName = 'replyQueue';
const message = {
text: 'Hello, World!',
reply_to: queueName
};
await channel.assertQueue(queueName, { durable: true });
channel.sendToQueue('myQueue', Buffer.from(JSON.stringify(message)), {
reply_to: queueName
});
await channel.close();
await connection.close();
}
sendMessage().catch(console.error);
7. Setting Expiration TTL
| Property | Value | Description |
|---|---|---|
| expiration | 3600000 |
Time-to-live in milliseconds for a message |
| x-message-ttl | 3600000 |
Alternative way to set TTL on a queue level |
Example: Setting Message TTL in Queue Declaration
const queueOptions = {
durable: true,
arguments: {
'x-message-ttl': 3600000 // 1 hour in milliseconds
}
};
channel.assertQueue('my-queue', queueOptions);
Example: Setting Message TTL in Message Properties
const message = 'Hello, RabbitMQ!';
const options = {
expiration: '3600000' // 1 hour in milliseconds
};
channel.publish('', 'my-queue', Buffer.from(message), options);
8. Setting Timestamp
| Property | Value | Description |
|---|---|---|
| timestamp | number |
Unix timestamp indicating when the message was sent |
Example: Setting Timestamp in RabbitMQ Message
const amqp = require('amqplib');
const timestamp = Math.floor(Date.now() / 1000);
async function sendMessage() {
const connection = await amqp.connect('amqp://localhost');
const channel = await connection.createChannel();
const queue = 'my-queue';
await channel.assertQueue(queue, { durable: true });
const message = {
id: 'msg-123',
content: 'Hello, RabbitMQ!',
timestamp: timestamp
};
channel.sendToQueue(queue, Buffer.from(JSON.stringify(message)), {
timestamp: timestamp
});
console.log('Message sent with timestamp:', timestamp);
await channel.close();
await connection.close();
}
sendMessage().catch(console.error);
9. Setting User ID
| Property | Value | Description |
|---|---|---|
| user_id | null |
Identifier of the user who sent the message |
Example: Setting User ID in a Message
const amqp = require('amqplib');
async function sendMessage() {
const connection = await amqp.connect('amqp://localhost');
const channel = await connection.createChannel();
const queue = 'my-queue';
await channel.assertQueue(queue);
const message = {
content: Buffer.from('Hello, RabbitMQ!'),
options: {
userId: 'user-123'
}
};
channel.sendToQueue(queue, message.content, message.options);
console.log('Message sent with user ID: user-123');
await channel.close();
await connection.close();
}
sendMessage().catch(console.error);
10. Setting App ID
| Property | Value | Description |
|---|---|---|
| app_id | null |
Application identifier for the message |
Example: Setting App ID in a Message
const amqp = require('amqplib');
async function sendMessage() {
const connection = await amqp.connect('amqp://localhost');
const channel = await connection.createChannel();
const exchange = 'my_exchange';
const routingKey = 'my_routing_key';
const message = {
text: 'Hello, RabbitMQ!',
app_id: 'my-app-123'
};
channel.publish(exchange, routingKey, Buffer.from(JSON.stringify(message)), {
app_id: 'my-app-123'
});
await channel.close();
await connection.close();
}
sendMessage().catch(console.error);
11. Setting Message Type
| Property | Value | Description |
|---|---|---|
| type | string |
Defines the type of message for routing and processing purposes. |
Example: Setting Message Type
const amqp = require('amqplib');
async function sendMessage() {
const connection = await amqp.connect('amqp://localhost');
const channel = await connection.createChannel();
const queue = 'my-queue';
await channel.assertQueue(queue, { durable: true });
const message = {
type: 'user.signup',
data: { userId: '12345', email: 'user@example.com' }
};
channel.sendToQueue(queue, Buffer.from(JSON.stringify(message)), {
contentType: 'application/json',
type: message.type
});
await channel.close();
await connection.close();
}
sendMessage().catch(console.error);
12. Using Custom Headers
| Header Name | Type | Description | Example Value |
|---|---|---|---|
| custom.header1 | String | A custom header for routing messages | priority.high |
| message.correlation.id | String | Used to correlate messages in a workflow | msg-12345 |
| message.reply.to | String | Specifies the queue to reply to | reply.queue |
Example: Setting Custom Headers in a Message
const amqp = require('amqplib');
async function sendMessage() {
const connection = await amqp.connect('amqp://localhost');
const channel = await connection.createChannel();
const queue = 'my-queue';
const message = {
text: 'Hello RabbitMQ',
headers: {
'custom.header1': 'priority.high',
'message.correlation.id': 'msg-12345'
}
};
channel.assertQueue(queue, { durable: true });
channel.sendToQueue(queue, Buffer.from(JSON.stringify(message)), {
headers: message.headers
});
await channel.close();
await connection.close();
}
sendMessage().catch(console.error);
Note: Custom headers are useful for implementing message routing logic and tracking message flow through the system.