stompgem / stomp

A ruby gem for sending and receiving messages from a Stomp protocol compliant message queue. Includes: failover logic, ssl support.
http://stomp.github.com
Apache License 2.0
152 stars 80 forks source link

ERROR frame handling. #81

Closed ismith closed 10 years ago

ismith commented 10 years ago

The handler will raise an exception in the thread that has the Stomp::Client object on receipt of ERROR frames.

(ppaul/6540400, #3)

kovyrin commented 10 years ago

How a stomp client user is supposed to handle those exceptions, that could be raised seconds after a publish call? In our case we publish data from within a web request handling code, but if there is an issue with the broker, we get a random exception much later in a code that knows nothing about stomp and its errors.

Is there a way to block on publish to make sure there won't be an exception (since as far as I can see, there is no way to disable those exceptions or funnel them into a piece of code/callback that actually knows what to do with them)?

Thanks for any clarification you could provide.

PaulGale commented 10 years ago

How are you publishing messages, blocking or non-blocking?

I generally recommend a blocking approach. This involves the use of read receipts that function as broker acknowledgements. Without read receipts one is essentially firing a message into the ether with no way of knowing if the broker received it or not; in other words all notions of guaranteed delivery are lost without them.

By blocking for a read receipt one's client will receive either an ACK frame or, when something goes wrong, an ERROR frame. Receipt of either frame type will cause the client to unblock. As an ERROR frame is converted into a local exception the client knows immediately, rather than later, that something went wrong.

As the client doesn't block when read receipts are not turned on the client won't read any ERROR frame response until some time later (when the local exception is raised), which is probably accounting for the time interval that you're witnessing.

kovyrin commented 10 years ago

@PaulGale Thanks for helping with this. Now the question is, how do I enable blocking mode for my publish calls?

PaulGale commented 10 years ago

I tend to do something like this:

begin
  receipt = false
  client = Stomp::Client.new(args)
  client.publish(destination, message, headers) do |response|
    receipt = response
  end

  Timeout.timeout(seconds) do
    Thread.pass until receipt
  end
rescue => e
  # Rescue Stomp gem exceptions here...
end

So the client doesn't block automatically when requesting a read-receipt. It would nice if the gem handled that for you. Unfortunately it doesn't. Therefore you have to write that timeout handling code yourself.

HTH

PaulGale commented 10 years ago

@kovyrin Did this work for you?