Class: Synapse::Command::SimpleCommandBus
- Inherits:
-
CommandBus
- Object
- CommandBus
- Synapse::Command::SimpleCommandBus
- Defined in:
- lib/synapse/command/simple_command_bus.rb
Overview
TODO:
Thread safety?
Implementation of a command bus that dispatches commands in the calling thread
Direct Known Subclasses
Instance Attribute Summary collapse
- #filters ⇒ Array<CommandFilter> readonly
- #interceptors ⇒ Array<DispatchInterceptor> readonly
- #rollback_policy ⇒ RollbackPolicy
Instance Method Summary collapse
- #dispatch(command) ⇒ undefined
- #dispatch_with_callback(command, callback) ⇒ undefined
- #initialize(unit_factory) ⇒ undefined constructor
-
#subscribe(command_type, handler) ⇒ CommandHandler
The command handler being replaced, if any.
-
#unsubscribe(command_type, handler) ⇒ Boolean
True if command handler was unsubscribed from command handler.
Constructor Details
#initialize(unit_factory) ⇒ undefined
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/synapse/command/simple_command_bus.rb', line 17 def initialize(unit_factory) @unit_factory = unit_factory @handlers = Hash.new @filters = Array.new @interceptors = Array.new @rollback_policy = RollbackOnAnyExceptionPolicy.new @logger = Logging.logger[self.class] end |
Instance Attribute Details
#filters ⇒ Array<CommandFilter> (readonly)
10 11 12 |
# File 'lib/synapse/command/simple_command_bus.rb', line 10 def filters @filters end |
#interceptors ⇒ Array<DispatchInterceptor> (readonly)
13 14 15 |
# File 'lib/synapse/command/simple_command_bus.rb', line 13 def interceptors @interceptors end |
#rollback_policy ⇒ RollbackPolicy
7 8 9 |
# File 'lib/synapse/command/simple_command_bus.rb', line 7 def rollback_policy @rollback_policy end |
Instance Method Details
#dispatch(command) ⇒ undefined
32 33 34 |
# File 'lib/synapse/command/simple_command_bus.rb', line 32 def dispatch(command) dispatch_with_callback command, VoidCallback.new end |
#dispatch_with_callback(command, callback) ⇒ undefined
40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/synapse/command/simple_command_bus.rb', line 40 def dispatch_with_callback(command, callback) begin result = perform_dispatch command callback.on_success result rescue => exception backtrace = exception.backtrace.join $RS @logger.error "Exception occured while dispatching command {#{command.payload_type}} {#{command.id}}:\n" + "#{exception.inspect} #{backtrace}" callback.on_failure exception end end |
#subscribe(command_type, handler) ⇒ CommandHandler
Returns The command handler being replaced, if any.
57 58 59 60 61 62 63 64 |
# File 'lib/synapse/command/simple_command_bus.rb', line 57 def subscribe(command_type, handler) current = @handlers.fetch command_type, nil @handlers.store command_type, handler @logger.debug "Command handler {#{handler.class}} subscribed to command type {#{command_type}}" current end |
#unsubscribe(command_type, handler) ⇒ Boolean
Returns True if command handler was unsubscribed from command handler.
70 71 72 73 74 75 76 77 78 79 |
# File 'lib/synapse/command/simple_command_bus.rb', line 70 def unsubscribe(command_type, handler) current = @handlers.fetch command_type, nil return false unless current === handler @handlers.delete command_type @logger.debug "Command handler {#{handler.class}} unsubscribed from command type {#{command_type}}" return true end |