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<CommandFilter> (readonly)

Returns:



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

def filters
  @filters
end

#interceptorsArray<DispatchInterceptor> (readonly)

Returns:



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, VoidCallback.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 $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.

Parameters:

Returns:



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.

Parameters:

Returns:

  • (Boolean)

    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