Class: MessageDriver::Broker

Inherits:
Object
  • Object
show all
Defined in:
lib/message_driver/broker.rb,
lib/message_driver/adapters/bunny_adapter.rb,
lib/message_driver/adapters/stomp_adapter.rb,
lib/message_driver/adapters/in_memory_adapter.rb

Constant Summary collapse

DEFAULT_BROKER_NAME =
:default

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



7
8
9
# File 'lib/message_driver/broker.rb', line 7

def adapter
  @adapter
end

#configurationObject (readonly)

Returns the value of attribute configuration.



8
9
10
# File 'lib/message_driver/broker.rb', line 8

def configuration
  @configuration
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/message_driver/broker.rb', line 9

def name
  @name
end

Class Method Details

.brokerBroker .broker(name) ⇒ Broker

Returns the specified broker.

Parameters:

  • name (Symbol) (defaults to: DEFAULT_BROKER_NAME)

    the name of the broker you wish to define

Returns:

  • (Broker)

    the specified broker

Raises:



31
32
33
34
35
36
37
38
# File 'lib/message_driver/broker.rb', line 31

def broker(name = DEFAULT_BROKER_NAME)
  result = brokers[name]
  if result.nil?
    raise BrokerNotConfigured,
          "There is no broker named #{name} configured. The configured brokers are #{brokers.keys}"
  end
  result
end

.configure(options) ⇒ Object .configure(name, options) ⇒ Object

Parameters:

  • name (Symbol) (defaults to: DEFAULT_BROKER_NAME)

    when configuring multiple brokers, this symbol will differentiate between brokers

  • options (Hash)

    options to be passed to the adapter class



19
20
21
22
23
24
# File 'lib/message_driver/broker.rb', line 19

def configure(name = DEFAULT_BROKER_NAME, options)
  if brokers.keys.include? name
    raise BrokerAlreadyConfigured, "there is already a broker named #{name} configured"
  end
  brokers[name] = new(name, options)
end

.defineObject .define(name) ⇒ Object

Yields the specified broker so that destinations and consumers can be defined on it.

Yields:

  • (Broker)

    the specified broker



46
47
48
# File 'lib/message_driver/broker.rb', line 46

def define(name = DEFAULT_BROKER_NAME)
  yield broker(name)
end

.resetObject

Stops and un-configures all the brokers

See Also:



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/message_driver/broker.rb', line 80

def reset
  each_broker do |brk|
    begin
      brk.stop
    rescue => e
      Logging.logger.warn Logging.message_with_exception("error stopping broker #{brk.name}", e)
    end
  end
  brokers.clear
  clients.clear
end

.reset_after_testsObject

Resets all the brokers for testing purposes.

See Also:

  • Adapter::Base#reset_after_tests


72
73
74
75
76
# File 'lib/message_driver/broker.rb', line 72

def reset_after_tests
  each_broker do |brk|
    brk.adapter.reset_after_tests
  end
end

.restart_allObject

restarts all the brokers

See Also:



66
67
68
# File 'lib/message_driver/broker.rb', line 66

def restart_all
  each_broker(&:restart)
end

.stop_allObject

stops all the brokers

See Also:



60
61
62
# File 'lib/message_driver/broker.rb', line 60

def stop_all
  each_broker(&:stop)
end

Instance Method Details

#bunny_adapterObject



6
7
8
# File 'lib/message_driver/adapters/bunny_adapter.rb', line 6

def bunny_adapter
  MessageDriver::Adapters::BunnyAdapter
end

#clientMessageDriver::Client

Returns the client module for this broker.

Returns:



121
122
123
# File 'lib/message_driver/broker.rb', line 121

def client
  @client ||= self.class.client(name)
end

#consumer(key, &block) ⇒ Object



156
157
158
159
# File 'lib/message_driver/broker.rb', line 156

def consumer(key, &block)
  raise MessageDriver::Error, 'you must provide a block' unless block_given?
  @consumers[key] = block
end

#destination(key, dest_name, dest_options = {}, message_props = {}) ⇒ Object



151
152
153
154
# File 'lib/message_driver/broker.rb', line 151

def destination(key, dest_name, dest_options = {}, message_props = {})
  dest = dynamic_destination(dest_name, dest_options, message_props)
  @destinations[key] = dest
end

#dynamic_destination(dest_name, dest_options = {}, message_props = {}) ⇒ Object



147
148
149
# File 'lib/message_driver/broker.rb', line 147

def dynamic_destination(dest_name, dest_options = {}, message_props = {})
  client.dynamic_destination(dest_name, dest_options, message_props)
end

#find_consumer(consumer_name) ⇒ Object



173
174
175
176
177
# File 'lib/message_driver/broker.rb', line 173

def find_consumer(consumer_name)
  consumer = @consumers[consumer_name]
  raise MessageDriver::NoSuchConsumerError, "no consumer #{consumer_name} has been configured" if consumer.nil?
  consumer
end

#find_destination(destination_name) ⇒ Destination::Base

Find a previously declared Destination

Parameters:

  • destination_name (Symbol)

    the name of the destination

Returns:

Raises:



165
166
167
168
169
170
171
# File 'lib/message_driver/broker.rb', line 165

def find_destination(destination_name)
  destination = @destinations[destination_name]
  if destination.nil?
    raise MessageDriver::NoSuchDestinationError, "no destination #{destination_name} has been configured"
  end
  destination
end

#in_memory_adapterObject



5
6
7
# File 'lib/message_driver/adapters/in_memory_adapter.rb', line 5

def in_memory_adapter
  MessageDriver::Adapters::InMemoryAdapter
end

#restartAdapter::Base

Restarts the Broker, stopping it first if needed. This results in a new

adapter instance being constructed.

Returns:

  • (Adapter::Base)

    the newly constructed adapter



140
141
142
143
144
145
# File 'lib/message_driver/broker.rb', line 140

def restart
  @adapter.stop unless stopped?
  @adapter = resolve_adapter(@configuration[:adapter], @configuration)
  @stopped = false
  @adapter
end

#stomp_adapterObject



6
7
8
# File 'lib/message_driver/adapters/stomp_adapter.rb', line 6

def stomp_adapter
  MessageDriver::Adapters::StompAdapter
end

#stopObject

stops the adapter for this Broker



127
128
129
130
# File 'lib/message_driver/broker.rb', line 127

def stop
  @adapter.stop
  @stopped = true
end

#stopped?Boolean

Returns true if the broker is currently stopped.

Returns:

  • (Boolean)

    true if the broker is currently stopped



133
134
135
# File 'lib/message_driver/broker.rb', line 133

def stopped?
  @stopped
end