Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use message headers in RabbitMQ's Erlang client?

I'm trying to send a message with metadata through the Erlang client, and I can't understand how should I set custom application headers in the message's basic properties record. I've tried all these options with no success:

#'P_basic'{headers = [{<<"key">>, <<"value">>}]}
#'P_basic'{headers = [{"key", <<"value">>}]}
#'P_basic'{headers = [{key, <<"value">>}]}

It seems that headers use some special data structure, an AMQP table - but I couldn't find any documentation or examples on this matter.

What is a correct way to send a message with headers?

Update: A stack trace (actually, it's not relevant - the cause of that error is the silently closed channel) and the source code.

like image 912
NikitaBaksalyar Avatar asked Oct 16 '13 16:10

NikitaBaksalyar


1 Answers

Do you get any errors trying to send messages with headers?

Did you try to use string type for both key and value?

#'P_basic'{headers = [{"key", "value"}]}

Update: I investigated the source code of the package rabbit_common and I found out something about headers' type. There is a type headers() in rabbit_basic.erl:

-type(headers() :: rabbit_framing:amqp_table() | 'undefined').

And there are definition of the types in module rabbit_framing_amqp:

-type(amqp_field_type() ::
      'longstr' | 'signedint' | 'decimal' | 'timestamp' |
      'table' | 'byte' | 'double' | 'float' | 'long' |
      'short' | 'bool' | 'binary' | 'void' | 'array').
-type(amqp_property_type() ::
      'shortstr' | 'longstr' | 'octet' | 'shortint' | 'longint' |
      'longlongint' | 'timestamp' | 'bit' | 'table').

-type(amqp_table() :: [{binary(), amqp_field_type(), amqp_value()}]).
-type(amqp_array() :: [{amqp_field_type(), amqp_value()}]).
-type(amqp_value() :: binary() |    % longstr
                      integer() |   % signedint
                      {non_neg_integer(), non_neg_integer()} | % decimal
                      amqp_table() |
                      amqp_array() |
                      byte() |      % byte
                      float() |     % double
                      integer() |   % long
                      integer() |   % short
                      boolean() |   % bool
                      binary() |    % binary
                      'undefined' | % void
                      non_neg_integer() % timestamp
     ).

So the header is a tuple of three items (not two), which are binary, type of value, value. So you have to define each header the way like that:

BooleanHeader = {<<"my-boolean">>, bool, true}.
StringHeader = {<<"my-string">>, longstr, <<"value">>}.
IntHeader = {<<"my-int">>, long, 1000}.
like image 190
Danil Onishchenko Avatar answered Oct 04 '22 18:10

Danil Onishchenko