[rabbitmq-discuss] Newbie - using rabbitmq-c

Alan Antonuk alan.antonuk at gmail.com
Fri Jun 8 15:52:19 BST 2012


Unfortunately the API for rabbitmq-c is rather low-level when it comes to
reading messages from the broker, so when using amqp_basic_get() or
amqp_basic_consume(), you will have to deal with reading individual frames
to read the header and message body.

Off the top of my head here's what you'd need to read a message off the
wire using amqp_basic_get() (Note: this is off the top of my head, and
doesn't deal with error handling well (just returns).  This is also using
basic.get - which is a synchronous method of getting messages. For anything
non-trivial, you probably want use amqp_basic_consume.

  amqp_rpc_reply_t reply =
    amqp_basic_get(
      connection,
      1, /* channel id */
      amqp_cstring_bytes("queue name"),
      0); /* no_ack, the message will automatically be acked as its
delievered */

  if (reply.reply_type != AMQP_RESPONSE_NORMAL)
    return; /* bad reponse from broker */

  if (reply.reply.id == AMQP_BASIC_GET_EMPTY_METHOD)
    return; /* no messages in the queue to get */

  /* Read the header (1 frame) and the body (1 + x frames) */

  size_t body_remaining;
  amqp_frame_t frame;

  int res = amqp_simple_wait_frame(connection, &frame);
  if (res != 0)
    return; /* failure waiting for frame */
  if (frame.frame_type != AMQP_FRAME_HEADER)
    return; /* expecting AMQP_FRAME_HEADER type of frame */

  /* This memory is valid until you call amqp_maybe_release_buffers() */
  amqp_basic_properties_t *props =
    (amqp_basic_properties_t*)frame.payload.properties.decoded;
  body_remaining = frame.payload.properties.body_size;

  char* body = malloc(body_remaining);
  char* current_body_ptr = body;

  while (body_remaining)
  {
    res = amqp_simple_wait_frame(connection, &frame);
    if (res != 0)
      return; /* failure waiting for frame */
    if (frame.frame_type != AMQP_FRAME_BODY)
      return; /* expecting AMQP_BODY_HEADER */
    memcpy(current_body_ptr, frame.payload.body_fragment.bytes,
frame.payload.body_fragment.len);

    current_body_ptr += frame.payload.body_fragment.len;
    body_remaining -= frame.payload.body_fragment.len;
  }


HTH
-Alan

On Fri, Jun 8, 2012 at 9:19 AM, Mike Aubury <mike at aubit.com> wrote:

> Hi, sorry for such a simple question...
> I had written some interface code in C using openAMQ - but I've now
> realised that looks like its been canned - so I'm looking at porting to
> something else...
>
> I can get my head around putting stuff into the exchanges/queues etc* *(amq_basic_publish)
> from this code :
>
>
>   char *messagebody="Hello World";
>   ...
>     amqp_basic_properties_t props;
>     props._flags = AMQP_BASIC_CONTENT_TYPE_FLAG |
> AMQP_BASIC_DELIVERY_MODE_FLAG;
>     props.content_type = amqp_cstring_bytes("text/plain");
>     props.delivery_mode = 2; /* persistent delivery mode */
>     die_on_error(amqp_basic_publish(conn,
>                                     1,
>                                     amqp_cstring_bytes(exchange),
>                                     amqp_cstring_bytes(routingkey),
>                                     0,
>                                     0,
>                                     &props,
>                                     amqp_cstring_bytes(messagebody)),
>                  "Publishing");
>
>
> but pulling data off is escaping me atm..
>
> Whats the **simplest way** of getting that data, as a full string I can
> then process ?
> (All the examples seem to do strange things with frames/frame headers etc)
> ..
>
> Ideally - I want to replace my existing :
>
> #define BUFFSIZE 10000
>  char message_text[BUFFSIZE];
>
>  content = amq_client_session_basic_arrived (session);
>  message_size = amq_content_basic_get_body (content,
>                                              (byte *)
>                                              message_text,
>                                              sizeof (message_text));
>
>
>
> and - so far I've only got as far as :
>
>    #define BUFFSIZE 10000
>    char message_text[BUFFSIZE];
>
>    amqp_simple_wait_frame(conn, &frame);
>
>
> ;)
>
>
>
> Any help, or pointers to some documentation would be much appreciated !
>
> _______________________________________________
> rabbitmq-discuss mailing list
> rabbitmq-discuss at lists.rabbitmq.com
> https://lists.rabbitmq.com/cgi-bin/mailman/listinfo/rabbitmq-discuss
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rabbitmq.com/pipermail/rabbitmq-discuss/attachments/20120608/8df79418/attachment.htm>


More information about the rabbitmq-discuss mailing list