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 readonly
- #interceptors ⇒ Array 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) ⇒ undefined
- #unsubscribe(command_type, handler) ⇒ undefined
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 (readonly)
10 11 12 |
# File 'lib/synapse/command/simple_command_bus.rb', line 10 def filters @filters end |
#interceptors ⇒ Array (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, CommandCallback.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 $/ @logger.error 'Exception occured while dispatching command [%s] [%s]: %s %s' % [command.payload_type, command.id, exception.inspect, backtrace] callback.on_failure exception end end |
#subscribe(command_type, handler) ⇒ undefined
57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/synapse/command/simple_command_bus.rb', line 57 def subscribe(command_type, handler) if @handlers.has_key? command_type current_handler = @handlers.fetch command_type @logger.info 'Command handler [%s] is being replaced by [%s] for command type [%s]' % [current_handler.class, handler.class, command_type] else @logger.debug 'Command handler [%s] subscribed to command type [%s]' % [handler.class, command_type] end @handlers.store command_type, handler end |
#unsubscribe(command_type, handler) ⇒ undefined
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/synapse/command/simple_command_bus.rb', line 74 def unsubscribe(command_type, handler) if @handlers.has_key? command_type current_handler = @handlers.fetch command_type if current_handler.equal? handler @handlers.delete command_type @logger.debug 'Command handler [%s] unsubscribed from command type [%s]' % [handler.class, command_type] else @logger.info 'Command type [%s] subscribed to handler [%s] not [%s]' % [command_type, current_handler.class, handler.class] end else @logger.info 'Command type [%s] not subscribed to any handler' % command_type end end |