Search code examples
c++11rabbitmqrabbitmq-c

RabbitMQ - SimpleAmqpClient - I am trying to send headers along with my message but the headers are not being sent; what am I doing wrong?


I am using the following: https://github.com/alanxz/SimpleAmqpClient

I am trying to send headers along with my message but the headers are not being sent; what am I doing wrong?

Here is how my code looks like. I have a configuration object with some basic configuration values.

auto channel = AmqpClient::Channel::Create("localhost", 5672, configuration.UserName, configuration.Password, configuration.VirtualHost, 131072);

channel->DeclareQueue(configuration.QueueName, false, true, false, true);
auto messageBody = "simple json string message nothing fancy"
auto message = AmqpClient::BasicMessage::Create(messageBody);
message->DeliveryMode(AmqpClient::BasicMessage::delivery_mode_t::dm_nonpersistent);
message->ContentType("application/json");
message->Type("XYZRequest");
message->AppId("a guid");
auto headerTable = message->HeaderTable();
headersTable.insert(std::pair<string, string>("Key-1", "value-1"));
headersTable.insert(std::pair<string, string>("Key-2", "value-2"));

channel->BasicPublish(std::string(), configuration.ScoreQueueName, message);

This sends the message to the queue and I can see all the details (AppID, Type, Message Body, etc.) on the RabbitMq management portal except the headers.

What am I missing? Is it some configuration or what is it?

I would appreciate if some one can give me a link to a basic tutorial on how to send headers.

I am stuck. Please help.


Solution

  • message->HeaderTable() does not return a reference to the headers, it returns a copy of it.

    To set the headers you must construct the headersTable first, then use message->HeadersTable(headersTable).

    Table headersTable;
    headersTable.insert(std::pair<string, string>("Key-1", "value-1"));
    headersTable.insert(std::pair<string, string>("Key-2", "value-2"));
    
    message->HeadersTable(headersTable);