class NebulousStomp::Listener
Implements the Request-Response use case; consume Requests from an input queue and send Responses.
listener = NebulousStomp::Listener.new(target) listener.consume_messages do |msg| begin case msg.verb when "ping" listener.reply *msg.respond_with_success when "time" listener.reply *msg.respond_with_protocol("timeresponce", Time.now) else listener.reply *msg.respond_with_error("Bad verb #{msg.verb}") end rescue listener.reply *msg.respond_with_error($!) end end loop { sleep 5 }
Attributes
the queue name we are listening to
Insert a StompHandler
object for test purposes
Public Class Methods
When creating a Listener
, pass the queue name to listen on.
This can be something stringlike, or a Target
(in which case we listen on the target's receiving queue).
# File lib/nebulous_stomp/listener.rb, line 46 def initialize(queue) case when queue.respond_to?(:receive_queue) then @queue = queue.receive_queue when queue.respond_to?(:to_s) then @queue = queue.to_s else fail ArgumentError, "Unknown object passed as queue" end NebulousStomp.logger.debug(__FILE__) { "Listening on #@queue" } end
Public Instance Methods
Consume messages from the queue, yielding each.
Note that we don't block for input here. Just as with the Stomp gem, and with StompHandler
, you will need to take your own measures to ensure that your program does not end when it should be waiting for messages to arrive. The simplest solution is something like:
loop { sleep 5 }
Note also that this method runs inside a Thread, and so does the block you pass to it. By default threads do not report errors, so you must arrange to do that yourself.
# File lib/nebulous_stomp/listener.rb, line 71 def consume_messages stomp_handler.listen(@queue) {|msg| yield msg } end
Disconnect from Stomp.
You probably don't need this; Stomp connections are quite short lived.
# File lib/nebulous_stomp/listener.rb, line 92 def quit stomp_handler.stomp_disconnect self end
Send a message in reply
Queue must be a queue name; message must be a Message
. The usual way to get these is from the Message
class, for example by calling `message.respond_with_success`.
# File lib/nebulous_stomp/listener.rb, line 81 def reply(queue, message) NebulousStomp.logger.debug(__FILE__) { "Replying to #{queue}" } stomp_handler.send_message(queue, message) self end
Private Instance Methods
# File lib/nebulous_stomp/listener.rb, line 99 def stomp_handler @stomp_handler ||= StompHandler.new(Param.get :stompConnectHash) end