<div dir="ltr">The easiest way to accomplish what you&#39;re trying to do is to run each consumer in its own connection, which is running in its own thread.�<div><br></div><div style>So for each consumer you want to start, do the following:</div>
<div style>- Create a new thread</div><div style>- Create a new connection</div><div style>- Declare your queue</div><div style>- Start the consumer</div><div style>- Consume messages</div><div style><br></div><div style>
To deal with canceling the consumer I see two ways to go:</div><div style>- Have the entity that is sending messages to the queue send a &quot;stop message&quot;, 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.</div>
<div style>- Enable Consumer Cancellation notifications (<a href="http://www.rabbitmq.com/consumer-cancel.html">http://www.rabbitmq.com/consumer-cancel.html</a>), 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.</div>
<div style><br></div><div style><br></div><div style>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&#39;re trying to implement (again here are some examples:�<a href="http://www.rabbitmq.com/getstarted.html" target="_blank" style="font-size:13px;font-family:arial,sans-serif">http://www.rabbitmq.com/getstarted.html</a>). When I say message pattern I don&#39;t mean rabbitmq-c code, I mean a description of how clients and servers in your environment want to talk to each other.</div>
<div style><br></div><div style>-Alan</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Mon, Jul 1, 2013 at 7:38 PM, 3k4b251 <span dir="ltr">&lt;<a href="mailto:314992959@qq.com" target="_blank">314992959@qq.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">for �example. � here �is � a � �simple � receive server �code.<br>
<br>
//connection<br>
amqp_connection_state_t � �conn �= � amqp_new_connection();<br>
<br>
//socket<br>
amqp_socket_t *socket �= amqp_tcp_socket_new();<br>
<br>
//login<br>
amqp_login(conn, &quot;/&quot;, 0, 131072, 0, AMQP_SASL_METHOD_PLAIN, &quot;guest&quot;,<br>
&quot;guest&quot;)<br>
<br>
//open socket<br>
amqp_socket_open(socket, hostname, port)<br>
<br>
//set soket for connection<br>
amqp_set_socket(conn, socket);<br>
<br>
<br>
<br>
<br>
//after �link �to �rabbitmq server. we �start �to �listen �to the �queue<br>
consumer_1<br>
<br>
<br>
<br>
//open �one �channel � � � one question: �why we need channel....<br>
amqp_channel_open(conn, 1);<br>
<br>
//declare queue �&quot;test_1&quot; � . � I � usually try to �declare �queue �before<br>
declare consumer � to make sure the queue �already �be �there.<br>
<br>
amqp_queue_declare(conn,1,amqp_cstring_bytes(&quot;consumer_1&quot;),0,0,0,1,amqp_empty_table);<br>
<br>
//request �consume<br>
amqp_basic_consume(conn, 1, amqp_cstring_bytes(&quot;consumer_1&quot;),<br>
amqp_cstring_bytes(&quot;consumer_1&quot;), �0, 1, 0, amqp_empty_table);<br>
<br>
// now �I �will �try �to receive �message<br>
while(1)<br>
{<br>
amqp_frame_t frame;<br>
amqp_simple_wait_frame(conn,&amp;frame);<br>
.........................<br>
........................<br>
...................<br>
}<br>
<br>
//So � when �the �client �send �message �to �the �queue � &quot;consumer_1&quot;, �I<br>
will � receive �the message. And if there �is no message, �it will block �at<br>
amqp_simple_wait_frame();<br>
<br>
<br>
//but �what �I want to do �in �online server is that � one �consumer<br>
login,I �create a �queue �&quot;consumer_1&quot; �for him, �and �request queue<br>
listener �by �amqp_basic_consume(). �when �the consumer quit, I �want �to<br>
remove �the listener �by amqp_basic_cancel(). � �but �I can&#39;t �do this �in<br>
the simple �example. �I �will �block �in �the amqp_simple_wait_frame(). I<br>
could �just �wait �the someone �send �message to queue &quot;consumer_1&quot;So �I try<br>
to �start � �thread �for � message receving. �if �I do �like �this, I share<br>
connection in two �threads. � I feel very confused .........<br>
<br>
<br>
<br>
--<br>
View this message in context: <a href="http://rabbitmq.1065348.n5.nabble.com/rabbitmq-c-should-I-start-a-thread-for-amqp-simple-wait-frame-to-get-mesage-tp27666p27729.html" target="_blank">http://rabbitmq.1065348.n5.nabble.com/rabbitmq-c-should-I-start-a-thread-for-amqp-simple-wait-frame-to-get-mesage-tp27666p27729.html</a><br>

<div class="HOEnZb"><div class="h5">Sent from the RabbitMQ mailing list archive at Nabble.com.<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>
</div></div></blockquote></div><br></div>