How to use message headers in the RabbitMQ Erlang client?

I am trying to send a message with metadata through the Erlang client , and I canโ€™t understand how I should configure my own application headers in the main message properties record. I tried all of these options without success:

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

The headers seem to use some special data structure, the AMQP table, but I could not find any documentation or examples on this.

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

Update: A stack trace (actually this is not relevant - the cause of this error is a silent channel) and the source code .

+6
source share
1 answer

Do you have errors sending messages with headers?

Have you tried to use string type for key and value?

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

Update: I examined the source code of the rabbit_common package, and I learned something about the type of headers. There are type () headers in rabbit_basic.erl:

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

And there is a type definition in the rabbit_framing_amqp module:

 -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 ). 

Thus, the header is a set of three elements (not two) that are binary, type of value, value. Therefore, you should define each heading like this:

 BooleanHeader = {<<"my-boolean">>, bool, true}. StringHeader = {<<"my-string">>, longstr, <<"value">>}. IntHeader = {<<"my-int">>, long, 1000}. 
+5
source

Source: https://habr.com/ru/post/956101/


All Articles