[rabbitmq-discuss] rabbitmq-c should I start a thread for amqp_simple_wait_frame() to get mesage?

Alan Antonuk alan.antonuk at gmail.com
Tue Jul 2 06:40:13 BST 2013


The easiest way to accomplish what you're trying to do is to run each
consumer in its own connection, which is running in its own thread.

So for each consumer you want to start, do the following:
- Create a new thread
- Create a new connection
- Declare your queue
- Start the consumer
- Consume messages

To deal with canceling the consumer I see two ways to go:
- Have the entity that is sending messages to the queue send a "stop
message", so that when you receive that message you know to call
amqp_basic_cancel() before calling amqp_simple_wait_frame() to start
waiting for your next message.
- Enable Consumer Cancellation notifications (
http://www.rabbitmq.com/consumer-cancel.html), then have some other entity
delete the queue that the consumer is attached to. Then in your message
read loop, when you receive a AMQP_BASIC_CANCEL_METHOD you know to stop
reading.


Note that this is likely far from an optimal solution to what your problem
is. I probably can give you a better solution if you can describe to me at
a high-level what message pattern you're trying to implement (again here
are some examples: http://www.rabbitmq.com/getstarted.html). When I say
message pattern I don't mean rabbitmq-c code, I mean a description of how
clients and servers in your environment want to talk to each other.

-Alan


On Mon, Jul 1, 2013 at 7:38 PM, 3k4b251 <314992959 at qq.com> wrote:

> for  example.   here  is   a    simple   receive server  code.
>
> //connection
> amqp_connection_state_t    conn  =   amqp_new_connection();
>
> //socket
> amqp_socket_t *socket  = amqp_tcp_socket_new();
>
> //login
> amqp_login(conn, "/", 0, 131072, 0, AMQP_SASL_METHOD_PLAIN, "guest",
> "guest")
>
> //open socket
> amqp_socket_open(socket, hostname, port)
>
> //set soket for connection
> amqp_set_socket(conn, socket);
>
>
>
>
> //after  link  to  rabbitmq server. we  start  to  listen  to the  queue
> consumer_1
>
>
>
> //open  one  channel       one question:  why we need channel....
> amqp_channel_open(conn, 1);
>
> //declare queue  "test_1"   .   I   usually try to  declare  queue  before
> declare consumer   to make sure the queue  already  be  there.
>
>
> amqp_queue_declare(conn,1,amqp_cstring_bytes("consumer_1"),0,0,0,1,amqp_empty_table);
>
> //request  consume
> amqp_basic_consume(conn, 1, amqp_cstring_bytes("consumer_1"),
> amqp_cstring_bytes("consumer_1"),  0, 1, 0, amqp_empty_table);
>
> // now  I  will  try  to receive  message
> while(1)
> {
> amqp_frame_t frame;
> amqp_simple_wait_frame(conn,&frame);
> .........................
> ........................
> ...................
> }
>
> //So   when  the  client  send  message  to  the  queue   "consumer_1",  I
> will   receive  the message. And if there  is no message,  it will block
>  at
> amqp_simple_wait_frame();
>
>
> //but  what  I want to do  in  online server is that   one  consumer
> login,I  create a  queue  "consumer_1"  for him,  and  request queue
> listener  by  amqp_basic_consume().  when  the consumer quit, I  want  to
> remove  the listener  by amqp_basic_cancel().    but  I can't  do this  in
> the simple  example.  I  will  block  in  the amqp_simple_wait_frame(). I
> could  just  wait  the someone  send  message to queue "consumer_1"So  I
> try
> to  start    thread  for   message receving.  if  I do  like  this, I share
> connection in two  threads.   I feel very confused .........
>
>
>
> --
> 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-tp27666p27729.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/20130701/5fea5f6d/attachment.htm>


More information about the rabbitmq-discuss mailing list