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.<div>
<br></div><div>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.</div>
<div><br></div><div><div> amqp_rpc_reply_t reply = </div><div> amqp_basic_get(</div><div> connection,</div><div> 1, /* channel id */</div><div> amqp_cstring_bytes("queue name"),</div><div> 0); /* no_ack, the message will automatically be acked as its delievered */</div>
<div><br></div><div> if (reply.reply_type != AMQP_RESPONSE_NORMAL)</div><div> return; /* bad reponse from broker */</div><div><br></div><div> if (<a href="http://reply.reply.id">reply.reply.id</a> == AMQP_BASIC_GET_EMPTY_METHOD)</div>
<div> return; /* no messages in the queue to get */</div><div><br></div><div> /* Read the header (1 frame) and the body (1 + x frames) */</div><div><br></div><div> size_t body_remaining;</div><div> amqp_frame_t frame;</div>
<div><br></div><div> int res = amqp_simple_wait_frame(connection, &frame);</div><div> if (res != 0)</div><div> return; /* failure waiting for frame */</div><div> if (frame.frame_type != AMQP_FRAME_HEADER)</div><div>
return; /* expecting AMQP_FRAME_HEADER type of frame */</div><div><br></div><div> /* This memory is valid until you call amqp_maybe_release_buffers() */</div><div> amqp_basic_properties_t *props = </div><div> (amqp_basic_properties_t*)frame.payload.properties.decoded;</div>
<div> body_remaining = frame.payload.properties.body_size;</div><div><br></div><div> char* body = malloc(body_remaining);</div><div> char* current_body_ptr = body;</div><div><br></div><div> while (body_remaining)</div>
<div> {</div><div> res = amqp_simple_wait_frame(connection, &frame);</div><div> if (res != 0)</div><div> return; /* failure waiting for frame */</div><div> if (frame.frame_type != AMQP_FRAME_BODY)</div>
<div> return; /* expecting AMQP_BODY_HEADER */</div><div> memcpy(current_body_ptr, frame.payload.body_fragment.bytes, frame.payload.body_fragment.len);</div><div><br></div><div> current_body_ptr += frame.payload.body_fragment.len;</div>
<div> body_remaining -= frame.payload.body_fragment.len;</div><div> }</div></div><div><br></div><div><br></div><div>HTH</div><div>-Alan</div><div><br></div><div><div class="gmail_quote">On Fri, Jun 8, 2012 at 9:19 AM, Mike Aubury <span dir="ltr"><<a href="mailto:mike@aubit.com" target="_blank">mike@aubit.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi, sorry for such a simple question...<br>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...<br>
<br>I can get my head around putting stuff into the exchanges/queues etc<i> </i>(amq_basic_publish) from this code : <div>
<br></div><div><br></div><div> char *messagebody="Hello World";</div><div> ...</div><div><div> amqp_basic_properties_t props;</div><div> props._flags = AMQP_BASIC_CONTENT_TYPE_FLAG | AMQP_BASIC_DELIVERY_MODE_FLAG;</div>
<div> props.content_type = amqp_cstring_bytes("text/plain");</div><div> props.delivery_mode = 2; /* persistent delivery mode */</div><div> die_on_error(amqp_basic_publish(conn,</div><div> 1,</div>
<div> amqp_cstring_bytes(exchange),</div><div> amqp_cstring_bytes(routingkey),</div><div> 0,</div><div> 0,</div>
<div> &props,</div><div> amqp_cstring_bytes(messagebody)),</div><div> "Publishing");</div></div><div><br></div><div><br>
</div>
<div>but pulling data off is escaping me atm..<br>
<br>Whats the **simplest way** of getting that data, as a full string I can then process ?<div>(All the examples seem to do strange things with frames/frame headers etc) ..</div><div><br></div><div>Ideally - I want to replace my existing : </div>
<div><br></div><div>#define BUFFSIZE 10000</div><div> char message_text[BUFFSIZE];</div><div><br></div><div> content = amq_client_session_basic_arrived (session);</div><div><div> message_size = amq_content_basic_get_body (content,</div>
<div> (byte *)</div><div> message_text,</div><div> sizeof (message_text));</div></div><div>
<br></div><div><br></div><div><br></div><div>and - so far I've only got as far as : </div><div><br></div><div><div> #define BUFFSIZE 10000</div><div> char message_text[BUFFSIZE];</div></div><div><br></div><div><div>
amqp_simple_wait_frame(conn, &frame);</div></div><div><br></div><div><br></div><div>;)</div><div><br></div><div><br></div><div><br></div><div>Any help, or pointers to some documentation would be much appreciated !
</div></div>
<br>_______________________________________________<br>
rabbitmq-discuss mailing list<br>
<a href="mailto:rabbitmq-discuss@lists.rabbitmq.com">rabbitmq-discuss@lists.rabbitmq.com</a><br>
<a href="https://lists.rabbitmq.com/cgi-bin/mailman/listinfo/rabbitmq-discuss" target="_blank">https://lists.rabbitmq.com/cgi-bin/mailman/listinfo/rabbitmq-discuss</a><br>
<br></blockquote></div><br></div>