Class: Bisques::QueueListener
- Inherits:
-
Object
- Object
- Bisques::QueueListener
- Defined in:
- lib/bisques/queue_listener.rb
Overview
Listen for messages on a queue and execute a block when they arrive.
Instance Method Summary collapse
-
#initialize(queue, poll_time = 5) ⇒ QueueListener
constructor
A new instance of QueueListener.
-
#listen {|Message| ... } ⇒ Object
Listen for messages.
-
#listening? ⇒ Boolean
Returns true while the listener is active.
-
#stop ⇒ Object
Stop listening for messages.
Constructor Details
#initialize(queue, poll_time = 5) ⇒ QueueListener
Returns a new instance of QueueListener.
9 10 11 |
# File 'lib/bisques/queue_listener.rb', line 9 def initialize(queue, poll_time = 5) @queue, @poll_time = queue, poll_time end |
Instance Method Details
#listen {|Message| ... } ⇒ Object
Note:
Note that the block you give to this method is executed in a new thread.
Listen for messages. This is asynchronous and returns immediately.
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/bisques/queue_listener.rb', line 34 def listen(&block) return if @listening @listening = true @thread = Thread.new do while @listening = @queue.retrieve(@poll_time) block.call() if .present? end end end |
#listening? ⇒ Boolean
Returns true while the listener is active.
14 15 16 |
# File 'lib/bisques/queue_listener.rb', line 14 def listening? @listening end |
#stop ⇒ Object
Stop listening for messages.
47 48 49 50 |
# File 'lib/bisques/queue_listener.rb', line 47 def stop @listening = false @thread.join if @thread end |