[rabbitmq-discuss] event-based use of the RabbitMQ C library

David Wragg david at rabbitmq.com
Sun Feb 6 17:03:26 GMT 2011


Hi Mike,

mike castleman <m at mlcastle.net> writes:
> I'm looking at using RabbitMQ and the C library for a project.
>
> In all three of the consumer examples provided with the C library
> (amqp_consumer.c, amqp_listen.c, amqp_listenq.c), it seems like
> consuming messages blocks on calling amqp_simple_wait_frame().
>
> Is there any way we can select(2) on something or otherwise be notified
> when a message is ready to be received?

librabbitmq does not currently support non-blocking operation.

One workaround is to call librabbitmq from within a thread, and run the
rest of your application within other threads.  But note that
librabbitmq does not allow you to share a connection between threads, so
you won't be able to consume and publish concurrently within a single
thread.  You could open multiple connections, and use one for
publishing, and another for consuming.

And I think you can make the select approach work, but it's a hack.

> As far as I can tell, just select()ing on the socket is not sufficient,
> because if amqp_simple_wait_frame() reads a partial frame, it will still
> block.

You will need to set O_NONBLOCK on the socket.  Then in the partial
frame case, the recv call will return EGAIN or EWOULDBLOCK, and
amqp_simple_wait_frame will return this value in a translated form
(-(errno | ERR_CATEGORY_OS)).  You could detect that and proceed
accordingly.

As I said, it's a hack.

> Are there examples anywhere of event-driven uses of the RabbitMQ C
> library which send and receive on different channels on the same
> connection based on some kind of external events?

librabbitmq does not currently support such cases well.  That might
change in the future, but that's the situation today.

David

-- 
David Wragg
Staff Engineer, RabbitMQ
SpringSource, a division of VMware


More information about the rabbitmq-discuss mailing list