<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"><head><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=us-ascii"><meta name=Generator content="Microsoft Word 12 (filtered medium)"><style><!--
/* Font Definitions */
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri","sans-serif";}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
span.EmailStyle17
        {mso-style-type:personal-compose;
        font-family:"Calibri","sans-serif";
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]--></head><body lang=EN-US link=blue vlink=purple><div class=WordSection1><p class=MsoNormal>Hi,<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>Iam new to RabbitMQ. I am developing my test applications and I have a problem that maybe you can help me.<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>I have a Producer, in Flex, that send a message to a direct exchange, sendMsgExc and a routing key with same name. The consumer, in C#, binds the exchange and receives the message and sends a reply like this:<o:p></o:p></p><p class=MsoNormal><span style='font-size:10.0pt;font-family:"Courier New"'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:10.0pt;font-family:"Courier New"'>The message is received in an thread like this:<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'><o:p> </o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'> <span style='color:blue'>private</span> <span style='color:blue'>void</span> receiveMessagesAmqp() {<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'><o:p> </o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'> <span style='color:#2B91AF'>IConnection</span> connection = <span style='color:blue'>null</span>;<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'> <span style='color:#2B91AF'>IModel</span> channel = <span style='color:blue'>null</span>;<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'><o:p> </o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'> <span style='color:blue'>try</span> {<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'><o:p> </o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'> <span style='color:blue'>var</span> connectionFactory = <span style='color:blue'>new</span> <span style='color:#2B91AF'>ConnectionFactory</span>();<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'> connectionFactory.HostName = <span style='color:#A31515'>"localhost"</span>;<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'> connectionFactory.UserName = <span style='color:#A31515'>"guest"</span>;<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'> connectionFactory.Password = <span style='color:#A31515'>"guest"</span>;<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'><o:p> </o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'> connection = connectionFactory.CreateConnection();<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'> channel = connection.CreateModel();<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'> }<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:10.0pt;font-family:"Courier New"'> <span style='color:blue'>catch</span>( <span style='color:#2B91AF'>Exception</span> ) { }<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'><o:p> </o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'> <span style='color:blue'>try</span> {<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'><o:p> </o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'> channel.ExchangeDeclare( <span style='color:#A31515'>"sendMsgExc"</span>, <span style='color:#2B91AF'>ExchangeType</span>.Direct, <span style='color:blue'>false</span> );<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'> channel.QueueDeclare( <span style='color:#A31515'>"myQueue"</span>, <span style='color:blue'>false</span>, <span style='color:blue'>false</span>, <span style='color:blue'>false</span>, <span style='color:blue'>null</span> );<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'> channel.QueueBind( <span style='color:#A31515'>"myQueue"</span>, <span style='color:#A31515'>"sendMsgExc"</span>, <span style='color:#A31515'>"sendMsgExc"</span> );<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'><o:p> </o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'> <span style='color:#2B91AF'>QueueingBasicConsumer</span> consumer = <span style='color:blue'>new</span> <span style='color:#2B91AF'>QueueingBasicConsumer</span>( channel );<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'> <span style='color:#2B91AF'>String</span> consumerTag = channel.BasicConsume( <span style='color:#A31515'>"myQueue"</span>, <span style='color:blue'>false</span>, consumer );<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'> RabbitMQ.Client.Events.<span style='color:#2B91AF'>BasicDeliverEventArgs</span> basicDeliveryEventArgs = <span style='color:blue'>null</span>;<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'><o:p> </o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'> <span style='color:blue'>while</span>( <span style='color:blue'>true</span> ) {<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'> <span style='color:blue'>try</span> {<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'> basicDeliveryEventArgs = (RabbitMQ.Client.Events.<span style='color:#2B91AF'>BasicDeliverEventArgs</span>) consumer.Queue.Dequeue();<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'><o:p> </o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'> <span style='color:#2B91AF'>IBasicProperties</span> props = basicDeliveryEventArgs.BasicProperties;<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'> <span style='color:blue'>byte</span>[] body = basicDeliveryEventArgs.Body;<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'><o:p> </o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'> <span style='color:blue'>string</span> messageContent = <span style='color:#2B91AF'>Encoding</span>.UTF8.GetString( basicDeliveryEventArgs.Body );<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'> channel.BasicAck( basicDeliveryEventArgs.DeliveryTag, <span style='color:blue'>false</span> );<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'><o:p> </o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'> </span><span lang=PT-BR style='font-size:10.0pt;font-family:"Courier New";color:#2B91AF'>MessageVO</span><span lang=PT-BR style='font-size:10.0pt;font-family:"Courier New"'> message = <span style='color:#2B91AF'>JsonConvert</span>.DeserializeObject<<span style='color:#2B91AF'>MessageVO</span>>( messageContent );<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span lang=PT-BR style='font-size:10.0pt;font-family:"Courier New"'> </span><span style='font-size:10.0pt;font-family:"Courier New"'>sendReply( message );<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'> }<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'> <span style='color:blue'>catch</span>( <span style='color:#2B91AF'>OperationInterruptedException</span> ex ) {<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'> <span style='color:green'>// The consumer was removed, either through<o:p></o:p></span></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'> <span style='color:green'>// channel or connection closure, or through the<o:p></o:p></span></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'> <span style='color:green'>// action of IModel.BasicCancel().<o:p></o:p></span></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'> <span style='color:blue'>break</span>;<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'> }<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'> }<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'> }<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'> <span style='color:blue'>catch</span>( <span style='color:#2B91AF'>Exception</span> ) {<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'> }<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'> <span style='color:blue'>finally</span> {<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'><o:p> </o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'> channel.Close();<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'> connection.Close();<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'> }<o:p></o:p></span></p><p class=MsoNormal> }<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>The sendReply method sends a message back to the Producer, like this:<o:p></o:p></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'><o:p> </o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'> <span style='color:blue'>private</span> <span style='color:blue'>void</span> sendReply( <span style='color:#2B91AF'>MessageVO</span> messageVO ) {<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'><o:p> </o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'> <span style='color:blue'>var</span> connectionFactory = <span style='color:blue'>new</span> <span style='color:#2B91AF'>ConnectionFactory</span>();<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'> connectionFactory.HostName = <span style='color:#A31515'>"localhost"</span>;<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'> connectionFactory.UserName = <span style='color:#A31515'>"guest"</span>;<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'> connectionFactory.Password = <span style='color:#A31515'>"guest"</span>;<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'><o:p> </o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'> <span style='color:blue'>using</span>( <span style='color:#2B91AF'>IConnection</span> connection = connectionFactory.CreateConnection() ) {<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'> <span style='color:blue'>using</span>( <span style='color:#2B91AF'>IModel</span> model = connection.CreateModel() ) {<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'><o:p> </o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'> messageVO.task.question = 8;<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'> messageVO.task.answer += 10;<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'> messageVO.task.msg = messageVO.task.msg + <span style='color:#A31515'>" - Message Reply"</span>;<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'><o:p> </o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'> messageVO.task.errorMessage = <span style='color:blue'>new</span> <span style='color:#2B91AF'>ErrorMessageVO</span>();<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'> messageVO.task.errorMessage.errorNumber = -10;<o:p></o:p></span></p><p class=MsoNormal style='margin-left:1.5in;text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'> messageVO.task.errorMessage.errorMessage = messageVO.task.errorMessage.errorMessage <o:p></o:p></span></p><p class=MsoNormal style='margin-left:5.0in;text-indent:.5in;text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'>+ <span style='color:#A31515'>" - Message Reply"</span>;<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'><o:p> </o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'> <span style='color:blue'>string</span> message = <span style='color:#2B91AF'>JsonConvert</span>.SerializeObject( messageVO );<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'> <span style='color:#2B91AF'>IBasicProperties</span> basicProperties = model.CreateBasicProperties();<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'> model.BasicPublish( <span style='color:#A31515'>"ReplyExc"</span>, <span style='color:#A31515'>"ReplyExc"</span>, <span style='color:blue'>false</span>, <span style='color:blue'>false</span>, basicProperties, <span style='color:#2B91AF'>Encoding</span>.UTF8.GetBytes( <o:p></o:p></span></p><p class=MsoNormal style='margin-left:7.5in;text-indent:.5in;text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'> message ) );<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'> }<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'> }<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;font-family:"Courier New"'> }<o:p></o:p></span></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>As you can see the sendReply opens another connection and channel to send the reply back to the consumer. I know I can use the RPC pattern but I need to send a message back like this.<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>When I try to define one connection that would be used by the receiveMessageAmqp and the sendReply it stops working. <o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>That is the connection becomes global to the class, that implements a thread, receiveMessageAmqp creates the connection and channel exactly in the same manner. The sendReply will now only to create the channel, will not create a connection, and It will use the same connection created by receiveMessageAmqp.<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal style='text-autospace:none'>What happens is that the first message received everything works ok, the message is received and the reply is sent. When another message is sent by the producer the consumer receives the message and the line <span style='font-size:10.0pt;font-family:"Courier New"'>channel.BasicAck( basicDeliveryEventArgs.DeliveryTag, <span style='color:blue'>false</span> );</span> is executed an exception is thrown:<o:p></o:p></p><p class=MsoNormal>base {RabbitMQ.Client.Exceptions.OperationInterruptedException} = {"The AMQP operation was interrupted: AMQP close-reason, initiated by Application, code=200, text=\"Connection close forced\", classId=0, methodId=0, cause="}<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>Debugging the code I could verify that the finally block did not close neither the connection nor the channel.<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>I presumed I could use one connection per thread and open as many as I need channels. What is wrong here please ???<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>Thanks in advance.<o:p></o:p></p></div></body></html>