Class: NebulousStomp::Listener
- Inherits:
-
Object
- Object
- NebulousStomp::Listener
- Defined in:
- lib/nebulous_stomp/listener.rb
Overview
Implements the Request-Response use case; consume Requests from an input queue and send Responses.
listener = NebulousStomp::Listener.new(target)
listener. 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 }
Instance Attribute Summary collapse
-
#queue ⇒ Object
readonly
the queue name we are listening to.
-
#stomp_handler ⇒ Object
writeonly
Insert a StompHandler object for test purposes.
Instance Method Summary collapse
-
#consume_messages ⇒ Object
:call-seq: listener.consume_message(queue) {|msg| … }.
-
#initialize(queue) ⇒ Listener
constructor
When creating a Listener, pass the queue name to listen on.
-
#quit ⇒ Object
Disconnect from Stomp.
-
#reply(queue, message) ⇒ Object
Send a message in reply .
Constructor Details
#initialize(queue) ⇒ Listener
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).
46 47 48 49 50 51 52 53 54 |
# 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 |
Instance Attribute Details
#queue ⇒ Object (readonly)
the queue name we are listening to
35 36 37 |
# File 'lib/nebulous_stomp/listener.rb', line 35 def queue @queue end |
#stomp_handler=(value) ⇒ Object
Insert a StompHandler object for test purposes
38 39 40 |
# File 'lib/nebulous_stomp/listener.rb', line 38 def stomp_handler=(value) @stomp_handler = value end |
Instance Method Details
#consume_messages ⇒ Object
:call-seq: listener.consume_message(queue) {|msg| … }
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.
71 72 73 |
# File 'lib/nebulous_stomp/listener.rb', line 71 def stomp_handler.listen(@queue) {|msg| yield msg } end |
#quit ⇒ Object
Disconnect from Stomp.
You probably don’t need this; Stomp connections are quite short lived.
92 93 94 95 |
# File 'lib/nebulous_stomp/listener.rb', line 92 def quit stomp_handler.stomp_disconnect self end |
#reply(queue, message) ⇒ Object
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`.
81 82 83 84 85 |
# File 'lib/nebulous_stomp/listener.rb', line 81 def reply(queue, ) NebulousStomp.logger.debug(__FILE__) { "Replying to #{queue}" } stomp_handler.(queue, ) self end |