0

I use librabbitmq C library to deal with AMQP-complaint brokers (RabbitMQ in my case) and i try to add headers onto the c client, for rabbitmq.

I modified amqp_sendstring.c

amqp_basic_properties_t props;
    props._flags = AMQP_BASIC_CONTENT_TYPE_FLAG | AMQP_BASIC_DELIVERY_MODE_FLAG |  AMQP_BASIC_HEADERS_FLAG;
    props.content_type = amqp_cstring_bytes("text/plain");
    props.delivery_mode = 2; /* persistent delivery mode */
    amqp_table_t *table=&props.headers;
    props.headers.num_entries=2;
    props.headers.entries=calloc(props.headers.num_entries, sizeof(amqp_table_entry_t));
    strcpy(&(table->entries[0]).key,"id1");
    ((table->entries[0]).value).kind=AMQP_FIELD_KIND_I32;
    ((table->entries[0]).value).value.i32=1234;
    strcpy(&(table->entries[1]).key,"id2");
    (table->entries[1]).value.kind=AMQP_FIELD_KIND_I32;
    (table->entries[1]).value.value.i32=5678;
    die_on_error(amqp_basic_publish(conn,
                                    1,
                                    amqp_cstring_bytes(exchange),
                                    amqp_cstring_bytes(routingkey),
                                    0,
                                    0,
                                    &props,

and in amqp_listen.c:

  132       printf("Num headers received %d \n", envelope.message.properties.headers.num_entries);

However the listener doesn't seem to receive any headers. Any body have any suggestions? Other sample code?

3
  • Have you checked from RabbitMQ Management panel whether headers are sent with messages? Have you tried to send message from management panel with headers manually set and receive that message with you modified consumer? Commented May 9, 2014 at 21:40
  • With hope it will help: look through this real-life headers usage from php-amqp extension amqp_exchange.c#L758 Commented May 9, 2014 at 21:43
  • Thanks for the help! Is there a function to convert from bytes to char *? I did'nt see one in amqp.h. Commented May 12, 2014 at 18:16

1 Answer 1

3

The .key member of amqp_table_entry_t is an amqp_bytes_t not a char*, so you should use amqp_cstring_bytes() to set it instead of strcpy().

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. Is there a function to convert from bytes to char *? I did'nt see one in amqp.h.
amqp_bytes_t can point to a buffer containing embedded \0 characters. There is no function to convert from an amqp_bytes_t to a c-style string, though it would not be hard to create one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.