Basic.Publish isn&#39;t pseudo-synchronous, it&#39;s really asynchronous. Exchange#publish doesn&#39;t take nowait option (so no, your code isn&#39;t safe), it simply send the data and that&#39;s it. Then you have no idea whether everything went OK or not. That&#39;s why you can use either transactions (yes, you&#39;d have to wrap the code in it as you&#39;ve mentioned) or publisher confirms (which I&#39;d personally prefer as transactions are really slow and fairly tricky). The documentation for publisher confirms in AMQP gem is here: <a href="http://rdoc.info/github/ruby-amqp/amqp/master/file/docs/Durability.textile">http://rdoc.info/github/ruby-amqp/amqp/master/file/docs/Durability.textile</a><div>

<br clear="all">Jakub<div><div><br></div><div><a href="http://www.flickr.com/photos/jakub-stastny/" target="_blank">http://www.flickr.com/photos/jakub-stastny</a></div></div><div><a href="http://twitter.com/botanicus" target="_blank">http://twitter.com/botanicus</a></div>

<br>
<br><br><div class="gmail_quote">2011/7/8 Michael Nacos <span dir="ltr">&lt;<a href="mailto:m.nacos@gmail.com">m.nacos@gmail.com</a>&gt;</span><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

<div>so, what&#39;s currently the best way to publish messages consumed from one queue into another with the amqp gem? This is quite important in processing scenaria when no messages may be lost. In particular, is the following code safe? Will the pseudo-synchronous <b>publish</b> call return only after the message has been accepted in the second queue (:nowait =&gt; false)? otherwise, what would be the point of wrapping the publish call with @channel.tx_select / @channel.tx_commit ?</div>


<div><br></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div><font face="&#39;courier new&#39;, monospace"># consumer which publishes each message to another queue</font></div>
<div><font face="&#39;courier new&#39;, monospace"># ------------------------------------------------------</font></div><div><font face="&#39;courier new&#39;, monospace"><br>
</font></div><div><font face="&#39;courier new&#39;, monospace">@consume_from = &#39;first&#39;</font></div><div><font face="&#39;courier new&#39;, monospace">@deliver_to = &#39;second&#39;</font></div>
<div><font face="&#39;courier new&#39;, monospace"><br></font></div><div><font face="&#39;courier new&#39;, monospace">EM.run do</font></div><div><font face="&#39;courier new&#39;, monospace">  AMQP.connect do |connection|</font></div>


<div><font face="&#39;courier new&#39;, monospace">    @channel = MQ.new(connection)</font></div><div><font face="&#39;courier new&#39;, monospace">    # making sure the relevant queues exist</font></div>
<div><font face="&#39;courier new&#39;, monospace">    @queue1 = @channel.queue(@consume_from, :passive =&gt; false, :durable =&gt; true)</font></div><div><font face="&#39;courier new&#39;, monospace">    @queue2 = @channel.queue(@deliver_to, :passive =&gt; false, :durable =&gt; true)</font></div>


<div><span style="font-family:&#39;courier new&#39;, monospace">    # setting up the consumer loop</span></div><div><font face="&#39;courier new&#39;, monospace">    @queue1.subscribe(:ack =&gt; true) do |metadata, data|</font></div>


<div><font face="&#39;courier new&#39;, monospace">      @channel.default_exchange.publish(data, \</font></div><div><font face="&#39;courier new&#39;, monospace">        :routing_key =&gt; @deliver_to, \</font></div>
<div><font face="&#39;courier new&#39;, monospace">        :persistent =&gt; true, \</font></div><div><font face="&#39;courier new&#39;, monospace">        :nowait =&gt; false)</font></div>
<div><span style="font-family:&#39;courier new&#39;, monospace">      puts &#39;.&#39;</span></div><div><font face="&#39;courier new&#39;, monospace">      metadata.ack</font></div>
<div><font face="&#39;courier new&#39;, monospace">    end</font></div><div><font face="&#39;courier new&#39;, monospace">  end</font></div><div><font face="&#39;courier new&#39;, monospace">end</font></div>
</blockquote><div><div></div><div class="h5"><div><br></div><div class="gmail_quote">2011/7/4 Jakub Šťastný <span dir="ltr">&lt;<a href="mailto:stastny@101ideas.cz" target="_blank">stastny@101ideas.cz</a>&gt;</span><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">


AMQP 0.8 is available as RC, it&#39;s way more stable than 0.7, so I would recommend to just use the RC.<div><br></div><div>Jakub<div><div><br></div><div><a href="http://www.flickr.com/photos/jakub-stastny/" target="_blank">http://www.flickr.com/photos/jakub-stastny</a></div>




</div><div><a href="http://twitter.com/botanicus" target="_blank">http://twitter.com/botanicus</a></div><div><div></div><div><br>
<br><br><div class="gmail_quote">2011/7/4 Michael Nacos <span dir="ltr">&lt;<a href="mailto:m.nacos@gmail.com" target="_blank">m.nacos@gmail.com</a>&gt;</span><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">




there seems to be some support for tx_select, tx_commit, tx_rollback in the master branch of the amqp gem, but not in the 0.7.x-stable branch, which is what most people are using<br><br><div class="gmail_quote"><div><div>




</div><div>2011/5/6 David Wragg <span dir="ltr">&lt;<a href="mailto:david@rabbitmq.com" target="_blank">david@rabbitmq.com</a>&gt;</span><br>
</div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div></div><div><div>Simon MacMullen &lt;<a href="mailto:simon@rabbitmq.com" target="_blank">simon@rabbitmq.com</a>&gt; writes:<br>





&gt; On 05/05/11 22:33, Elias Levy wrote:<br>
&gt;&gt; While writing some code using the Ruby AMQP gem against RabbitMQ, I&#39;ve<br>
&gt;&gt; noticed that if I publish a message and quickly close the connection,<br>
&gt;&gt; even though I&#39;ve received a publish-ok response from the server, the<br>
&gt;&gt; message fails to be queued by the broker.<br>
&gt;<br>
&gt; I&#39;m not at all familiar with the Ruby client, but I should point out<br>
&gt; that unlike many of the other AMQP methods, basic.publish does not<br>
&gt; have a corresponding basic.publish-ok method; it&#39;s always<br>
&gt; asynchronous. So I imagine the post-publish callback fires<br>
&gt; immediately.<br>
&gt;<br>
&gt; In order to be able to know when the broker has taken responsibility<br>
&gt; for a message you can either wrap the publish in a transaction (when<br>
&gt; you see tx.commit-ok you know the server has the message) or use the<br>
&gt; rather more lightweight publish confirms:<br>
</div>o&gt;<br>
<div>&gt; <a href="http://www.rabbitmq.com/blog/2011/02/10/introducing-publisher-confirms/" target="_blank">http://www.rabbitmq.com/blog/2011/02/10/introducing-publisher-confirms/</a><br>
<br>
</div>Another way to solve the problem is to do a synchronous AMQP method<br>
rather than abruptly closing the connection.  If this the sync method<br>
completes successfully, you can be sure that your published messages<br>
have reached the broker (it doesn&#39;t give you all the guarantees of<br>
transactions, but it is much lighter weight).<br>
<br>
An easy way to do this with all versions of the AMQP gem (even 0.6.7) is<br>
to use the AMQP#close callback.  E.g., add something like this to your<br>
code:<br>
<br>
  client.close { puts &quot;Closed ok&quot; ; EM.stop }<br>
<br>
David<br>
<font color="#888888"><br>
--<br>
David Wragg<br>
Staff Engineer, RabbitMQ<br>
VMware, Inc.<br>
</font></div></div><div><div></div><div>_______________________________________________<br>
rabbitmq-discuss mailing list<br>
<a href="mailto:rabbitmq-discuss@lists.rabbitmq.com" target="_blank">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>
</blockquote></div><br></div></div></div>
</blockquote></div><br>
</div></div></blockquote></div><br></div>