Add a main_thread method to allow for serializing some invocations on the main thread

This wlil help different worker threads perform client acknowledgements or any
other STOMP client work, which should be done in the main thread to ensure that
we're not stomping on other messages
This commit is contained in:
R. Tyler Croy 2013-02-13 04:20:45 -08:00
parent 9bb3079b1f
commit f90fe9e763
2 changed files with 18 additions and 1 deletions

View File

@ -21,7 +21,9 @@ class Worker < Stapfen::Worker
end
consume 'jms.topic.foo', {:ack => 'client'} do |message|
client.acknowledge(message)
main_thread do
client.acknowledge(message)
end
end
end

View File

@ -29,9 +29,14 @@ module Stapfen
def initialize
@pool = []
@workqueue = Queue.new
handle_signals!
end
def main_thread(&block)
@workqueue << block
end
def run
@client = Stomp::Client.new(self.class.configuration)
@ -49,6 +54,16 @@ module Stapfen
# close the connection, and an infinite Client#join call
while client.open? do
client.join(1)
until @workqueue.empty?
block = @workqueue.pop
begin
block.call
rescue => e
puts "Exception! #{e}"
end
end
@pool = @pool.select { |t| t.alive? }
end
rescue Interrupt