[rabbitmq-discuss] rabbitmq-c should I start a thread for amqp_simple_wait_frame() to get mesage?
Alan Antonuk
alan.antonuk at gmail.com
Wed Jul 3 06:59:10 BST 2013
Ok, now I have a better understanding of what you're trying to accomplish.
First a bit of a warning: as you've already noticed, the rabbitmq-c API
doesn't lend itself to doing RPC-style commands (such as
amqp_basic_consume, or amqp_queue_declare, etc) while waiting on a message
to be delivered with amqp_simple_wait_frame(). So the following solutions
won't be the most natural solutions out there, they may also may suffer
from performance issues. If these solutions don't work for you - I would
encourage you to explore using other RabbitMQ clients.
My first suggestion would be to declare a command queue on the broker and
have your message reading thread consume from that command queue. When a
user logs into the chat, using a different connection publish a message to
that command queue "start consuming from queue X". Your message reading
thread should then read that message, then create the consumer, and go back
to waiting for messages. You could use a similar process to stop a
consumer.
The upside is this is fairly easy to implement, the downside (and you would
need to do some performance testing) is that there is a certain cost for
having the message go to the broker and back, also if your message reading
thread is consuming a lot of messages from different consumers, there could
be a significant delay between the time you send the message from one
thread, and the time the message is received in your message reading thread.
My second suggestion would be to create a pipe (
http://linux.die.net/man/7/pipe) and before the first
amqp_simple_wait_frame() call, extract the socket fd from the
amqp_connection_state_t with amqp_get_sockfd() and do a select() call with
both the read-end of the pipe and the socket fd. If the pipe can be read
that will be the signal that a consumer should be started or stopped,
otherwise a message should be read.
In the other thread, when a new user logs in write a command to the pipe
indicating what other amqp commands the message reader thread should do
(start or stop a consumer).
The upside is that this will likely be more performant than my first
suggestion. The downside is that this is a fair bit more complicated in
terms of programming effort, and understanding how pipes work, and outside
of Linux i'm not 100% sure this will work (I know it will require a
different approach on Win32).
HTH
-Alan
On Tue, Jul 2, 2013 at 8:51 PM, 3k4b251 <314992959 at qq.com> wrote:
>
> 1. 【 For example when a new person signs on to this chat, what happens on
> the broker? Do new queues get created? Do you start some consumer?】
>
> Re: when a new person login in the online server, the online
> Server
> ask rabbitmq-server to create a queue for him. and start a
> consumer for the queue. (if he already have the queue on mq server,
> the create fail, but we don't care about that. the only thing we care
> about is that when a new person login in ,there will be a queue
> server for him) every person only have one queue and one consumer
> for the queue in rabbitmq-server.
>
>
>
> 2. 【When a person sends a message to another person over this chat what
> queues does it get routed to? How does the message get back to the
> application the user is using.】
>
> Re: when he want to chart to another person A. he send message to A's
> only queue. and A may receive the message by him only consumer. if A want
> to reply to sender, he need to send message to sender's only queue. it's
> seems like email. maybe we need talk group, so I'd like to create
> exchange for talk group. we can bind the group memebers to a exchange.
> and
> send message to the exchange.
>
>
>
>
>
> --
> View this message in context:
> http://rabbitmq.1065348.n5.nabble.com/rabbitmq-c-should-I-start-a-thread-for-amqp-simple-wait-frame-to-get-mesage-tp27666p27760.html
> Sent from the RabbitMQ mailing list archive at Nabble.com.
> _______________________________________________
> 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/20130702/90c00a3a/attachment.htm>
More information about the rabbitmq-discuss
mailing list