Class: Synapse::Command::SimpleCommandBus

Inherits:
CommandBus
  • Object
show all
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

AsynchronousCommandBus

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(unit_factory) ⇒ undefined

Parameters:

  • unit_factory (UnitOfWorkFactory)


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

#filtersArray (readonly)

Returns:

  • (Array)


10
11
12
# File 'lib/synapse/command/simple_command_bus.rb', line 10

def filters
  @filters
end

#interceptorsArray (readonly)

Returns:

  • (Array)


13
14
15
# File 'lib/synapse/command/simple_command_bus.rb', line 13

def interceptors
  @interceptors
end

#rollback_policyRollbackPolicy

Returns:



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

Parameters:

Returns:

  • (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

Parameters:

Returns:

  • (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

Parameters:

Returns:

  • (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

Parameters:

Returns:

  • (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