lib/stomper/receivers/threaded.rb in stomper-2.0.0 vs lib/stomper/receivers/threaded.rb in stomper-2.0.1
- old
+ new
@@ -1,9 +1,12 @@
# -*- encoding: utf-8 -*-
# Basic threaded receiver
class Stomper::Receivers::Threaded
+ # Stop Receiver
+ class StopReceiver < StandardError; end
+
# Returns true if the receiver is currently running, false otherwise.
# If the polling thread is terminated due to a raised exception, this
# attribute will be false.
# @return [true,false]
attr_reader :running
@@ -18,11 +21,10 @@
def initialize(connection)
@connection = connection
@running = false
@run_mutex = ::Mutex.new
@run_thread = nil
- @raised_while_running = nil
end
# Starts the receiver by creating a new thread to continually poll the
# {Stomper::Connection connection} for new Stomp frames. If an error is
# raised while calling {Stomper::Connection#receive}, the polling thread
@@ -30,18 +32,19 @@
# @return [self]
def start
is_starting = @run_mutex.synchronize { @running = true unless @running }
if is_starting
@run_thread = Thread.new do
- while @running
- begin
- @running = false if @connection.receive.nil?
- rescue Exception => ex
- @running = false
- raise ex
+ begin
+ until @connection.receive.nil?
end
+ rescue ::Stomper::Receivers::Threaded::StopReceiver
+ rescue Exception => ex
+ @running = false
+ raise ex
end
+ @running = false
end
end
self
end
@@ -56,14 +59,15 @@
# @return [self]
# @raise [Exception]
def stop
stopped = @run_mutex.synchronize { @run_thread.nil? }
unless stopped
- @running = false
+ @run_thread.raise(::Stomper::Receivers::Threaded::StopReceiver.new)
begin
@run_thread.join
- rescue IOError
+ rescue ::IOError, ::SystemCallError
raise if @connection.connected?
+ rescue ::Stomper::Receivers::Threaded::StopReceiver => ex
end
@run_thread = nil
end
self
end