Class: MessageDriver::Broker
- Inherits:
-
Object
- Object
- MessageDriver::Broker
- 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
-
#adapter ⇒ Object
readonly
Returns the value of attribute adapter.
-
#configuration ⇒ Object
readonly
Returns the value of attribute configuration.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
-
.broker(name = DEFAULT_BROKER_NAME) ⇒ Broker
The specified broker.
- .configure(name = DEFAULT_BROKER_NAME, options) ⇒ Object
-
.define(name = DEFAULT_BROKER_NAME) {|Broker| ... } ⇒ Object
Yields the specified broker so that destinations and consumers can be defined on it.
-
.reset ⇒ Object
Stops and un-configures all the brokers.
-
.reset_after_tests ⇒ Object
Resets all the brokers for testing purposes.
-
.restart_all ⇒ Object
restarts all the brokers.
-
.stop_all ⇒ Object
stops all the brokers.
Instance Method Summary collapse
- #bunny_adapter ⇒ Object
-
#client ⇒ MessageDriver::Client
The client module for this broker.
- #consumer(key, &block) ⇒ Object
- #destination(key, dest_name, dest_options = {}, message_props = {}) ⇒ Object
- #dynamic_destination(dest_name, dest_options = {}, message_props = {}) ⇒ Object
- #find_consumer(consumer_name) ⇒ Object
-
#find_destination(destination_name) ⇒ Destination::Base
Find a previously declared Destination.
- #in_memory_adapter ⇒ Object
-
#restart ⇒ Adapter::Base
Restarts the Broker, stopping it first if needed.
- #stomp_adapter ⇒ Object
-
#stop ⇒ Object
stops the adapter for this Broker.
-
#stopped? ⇒ Boolean
True if the broker is currently stopped.
Instance Attribute Details
#adapter ⇒ Object (readonly)
Returns the value of attribute adapter.
7 8 9 |
# File 'lib/message_driver/broker.rb', line 7 def adapter @adapter end |
#configuration ⇒ Object (readonly)
Returns the value of attribute configuration.
8 9 10 |
# File 'lib/message_driver/broker.rb', line 8 def configuration @configuration end |
#name ⇒ Object (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
.broker ⇒ Broker .broker(name) ⇒ Broker
Returns the specified broker.
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
19 20 21 22 23 24 |
# File 'lib/message_driver/broker.rb', line 19 def configure(name = DEFAULT_BROKER_NAME, ) if brokers.keys.include? name raise BrokerAlreadyConfigured, "there is already a broker named #{name} configured" end brokers[name] = new(name, ) end |
.define ⇒ Object .define(name) ⇒ Object
Yields the specified broker so that destinations and consumers can be defined on it.
46 47 48 |
# File 'lib/message_driver/broker.rb', line 46 def define(name = DEFAULT_BROKER_NAME) yield broker(name) end |
.reset ⇒ Object
Stops and un-configures all the brokers
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.("error stopping broker #{brk.name}", e) end end brokers.clear clients.clear end |
.reset_after_tests ⇒ Object
Resets all the brokers for testing purposes.
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_all ⇒ Object
restarts all the brokers
66 67 68 |
# File 'lib/message_driver/broker.rb', line 66 def restart_all each_broker(&:restart) end |
.stop_all ⇒ Object
stops all the brokers
60 61 62 |
# File 'lib/message_driver/broker.rb', line 60 def stop_all each_broker(&:stop) end |
Instance Method Details
#bunny_adapter ⇒ Object
6 7 8 |
# File 'lib/message_driver/adapters/bunny_adapter.rb', line 6 def bunny_adapter MessageDriver::Adapters::BunnyAdapter end |
#client ⇒ MessageDriver::Client
Returns the client module for this broker.
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 = dynamic_destination(dest_name, , ) @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, = {}, = {}) client.dynamic_destination(dest_name, , ) 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
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_adapter ⇒ Object
5 6 7 |
# File 'lib/message_driver/adapters/in_memory_adapter.rb', line 5 def in_memory_adapter MessageDriver::Adapters::InMemoryAdapter end |
#restart ⇒ Adapter::Base
Restarts the Broker, stopping it first if needed. This results in a new
adapter instance being constructed.
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_adapter ⇒ Object
6 7 8 |
# File 'lib/message_driver/adapters/stomp_adapter.rb', line 6 def stomp_adapter MessageDriver::Adapters::StompAdapter end |
#stop ⇒ Object
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.
133 134 135 |
# File 'lib/message_driver/broker.rb', line 133 def stopped? @stopped end |