Class: ActiveMessaging::Gateway
- Inherits:
-
Object
- Object
- ActiveMessaging::Gateway
- Defined in:
- lib/activemessaging/gateway.rb,
lib/activemessaging/test_helper.rb
Class Attribute Summary collapse
-
.adapters ⇒ Object
Returns the value of attribute adapters.
-
.connections ⇒ Object
Returns the value of attribute connections.
-
.filters ⇒ Object
Returns the value of attribute filters.
-
.named_destinations ⇒ Object
Returns the value of attribute named_destinations.
-
.processor_groups ⇒ Object
Returns the value of attribute processor_groups.
-
.subscriptions ⇒ Object
Returns the value of attribute subscriptions.
Class Method Summary collapse
- ._dispatch(message) ⇒ Object
-
.abort_message(subscription, message) ⇒ Object
abort_message is called when procesing the message raises a ActiveMessaging::AbortMessageException indicating the message should be returned to the destination so it can be tried again, later.
-
.acknowledge_message(subscription, message) ⇒ Object
acknowledge_message is called when the message has been processed w/o error by at least one processor.
- .apply_filter?(direction, details, options) ⇒ Boolean
- .connection(broker_name = 'default') ⇒ Object
- .create_filter(filter, options) ⇒ Object
- .current_processor_group ⇒ Object
- .define {|_self| ... } ⇒ Object
- .destination(destination_name, destination, publish_headers = {}, broker = 'default') ⇒ Object (also: queue)
- .disconnect ⇒ Object
- .dispatch(message) ⇒ Object
- .execute_filter_chain(direction, message, details = {}) {|message| ... } ⇒ Object
- .filter(filter, options = {}) ⇒ Object
- .find_destination(destination_name) ⇒ Object (also: find_queue)
- .load_connection_configuration(label = 'default') ⇒ Object
- .prepare_application ⇒ Object
- .processor_group(group_name, *processors) ⇒ Object
- .publish(destination_name, body, publisher = nil, headers = {}, timeout = 10) ⇒ Object
- .receive(destination_name, receiver = nil, subscribe_headers = {}, timeout = 10) ⇒ Object
- .register_adapter(adapter_name, adapter_class) ⇒ Object
- .reset ⇒ Object
- .reset_application ⇒ Object
-
.start ⇒ Object
Starts up an message listener to start polling for messages on each configured connection, and dispatching processing.
- .stop ⇒ Object
- .subscribe ⇒ Object
- .subscribe_to(destination_name, processor, headers = {}) ⇒ Object
- .unsubscribe ⇒ Object
Class Attribute Details
.adapters ⇒ Object
Returns the value of attribute adapters.
25 26 27 |
# File 'lib/activemessaging/gateway.rb', line 25 def adapters @adapters end |
.connections ⇒ Object
Returns the value of attribute connections.
25 26 27 |
# File 'lib/activemessaging/gateway.rb', line 25 def connections @connections end |
.filters ⇒ Object
Returns the value of attribute filters.
25 26 27 |
# File 'lib/activemessaging/gateway.rb', line 25 def filters @filters end |
.named_destinations ⇒ Object
Returns the value of attribute named_destinations.
25 26 27 |
# File 'lib/activemessaging/gateway.rb', line 25 def named_destinations @named_destinations end |
.processor_groups ⇒ Object
Returns the value of attribute processor_groups.
25 26 27 |
# File 'lib/activemessaging/gateway.rb', line 25 def processor_groups @processor_groups end |
.subscriptions ⇒ Object
Returns the value of attribute subscriptions.
25 26 27 |
# File 'lib/activemessaging/gateway.rb', line 25 def subscriptions @subscriptions end |
Class Method Details
._dispatch(message) ⇒ Object
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/activemessaging/gateway.rb', line 228 def _dispatch() abort = false processed = false subscriptions.each do |key, subscription| if .matches_subscription?(subscription) then processed = true routing = { :receiver => subscription.processor_class, :destination => subscription.destination, :direction => :incoming } begin execute_filter_chain(:incoming, .dup, routing) do |m| result = subscription.processor_class.new.process!(m) end rescue ActiveMessaging::AbortMessageException subscription, abort = true return ensure subscription, unless abort end end end ActiveMessaging.logger.error("No-one responded to #{}") unless processed end |
.abort_message(subscription, message) ⇒ Object
abort_message is called when procesing the message raises a ActiveMessaging::AbortMessageException indicating the message should be returned to the destination so it can be tried again, later
265 266 267 |
# File 'lib/activemessaging/gateway.rb', line 265 def subscription, connection(subscription.destination.broker_name).unreceive , subscription.subscribe_headers end |
.acknowledge_message(subscription, message) ⇒ Object
acknowledge_message is called when the message has been processed w/o error by at least one processor
259 260 261 |
# File 'lib/activemessaging/gateway.rb', line 259 def subscription, connection(subscription.destination.broker_name).received , subscription.subscribe_headers end |
.apply_filter?(direction, details, options) ⇒ Boolean
167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/activemessaging/gateway.rb', line 167 def apply_filter?(direction, details, ) # check that it is the correct direction result = if direction.to_sym == [:direction] || [:direction] == :bidirectional if .has_key?(:only) && [[:only]].flatten.include?(details[:destination].name) true elsif .has_key?(:except) && ![[:except]].flatten.include?(details[:destination].name) true elsif !.has_key?(:only) && !.has_key?(:except) true end end result end |
.connection(broker_name = 'default') ⇒ Object
122 123 124 125 126 127 128 |
# File 'lib/activemessaging/gateway.rb', line 122 def connection broker_name='default' return @connections[broker_name] if @connections.has_key?(broker_name) config = load_connection_configuration(broker_name) adapter_class = Gateway.adapters[config[:adapter]] raise "Unknown messaging adapter #{config[:adapter].inspect}!" if adapter_class.nil? @connections[broker_name] = adapter_class.new(config) end |
.create_filter(filter, options) ⇒ Object
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/activemessaging/gateway.rb', line 181 def create_filter(filter, ) filter_class = if filter.is_a?(String) or filter.is_a?(Symbol) filter.to_s.camelize.constantize elsif filter.is_a?(Class) filter end if filter_class if filter_class.respond_to?(:process) && (filter_class.method(:process).arity.abs > 0) filter_class elsif filter_class.instance_method(:initialize).arity.abs == 1 filter_class.new() elsif filter_class.instance_method(:initialize).arity == 0 filter_class.new else raise "Filter #{filter} could not be created, no 'initialize' matched." end else raise "Filter #{filter} could not be loaded, created, or used!" end end |
.current_processor_group ⇒ Object
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 |
# File 'lib/activemessaging/gateway.rb', line 348 def current_processor_group if ARGV.length > 0 && !@current_processor_group ARGV.each {|arg| pair = arg.split('=') if pair[0] == 'process-group' group_sym = pair[1].to_sym if processor_groups.has_key? group_sym @current_processor_group = group_sym else ActiveMessaging.logger.error "Unrecognized process-group." ActiveMessaging.logger.error "You specified process-group #{pair[1]}, make sure this is specified in config/messaging.rb" ActiveMessaging.logger.error " ActiveMessaging::Gateway.define do |s|" ActiveMessaging.logger.error " s.processor_groups = { :group1 => [:foo_bar1_processor], :group2 => [:foo_bar2_processor] }" ActiveMessaging.logger.error " end" exit end end } end @current_processor_group end |
.define {|_self| ... } ⇒ Object
269 270 271 272 |
# File 'lib/activemessaging/gateway.rb', line 269 def define #run the rest of messaging.rb yield self end |
.destination(destination_name, destination, publish_headers = {}, broker = 'default') ⇒ Object Also known as: queue
274 275 276 277 |
# File 'lib/activemessaging/gateway.rb', line 274 def destination destination_name, destination, publish_headers={}, broker='default' raise "You already defined #{destination_name} to #{named_destinations[destination_name].value}" if named_destinations.has_key?(destination_name) named_destinations[destination_name] = Destination.new(destination_name, destination, publish_headers, broker) end |
.disconnect ⇒ Object
147 148 149 150 |
# File 'lib/activemessaging/gateway.rb', line 147 def disconnect @connections.each { |key,connection| connection.disconnect } @connections = {} end |
.dispatch(message) ⇒ Object
216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/activemessaging/gateway.rb', line 216 def dispatch() prepare_application _dispatch() rescue Object => exc ActiveMessaging.logger.error "Dispatch exception: #{exc}" ActiveMessaging.logger.error exc.backtrace.join("\n\t") raise exc ensure ActiveMessaging.logger.flush rescue nil reset_application end |
.execute_filter_chain(direction, message, details = {}) {|message| ... } ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/activemessaging/gateway.rb', line 152 def execute_filter_chain(direction, , details={}) filters.each do |filter, | if apply_filter?(direction, details, ) begin filter_obj = create_filter(filter, ) filter_obj.process(, details) rescue ActiveMessaging::StopFilterException => sfe ActiveMessaging.logger.error "Filter: #{filter_obj.inspect} threw StopFilterException: #{sfe.}" return end end end yield() end |
.filter(filter, options = {}) ⇒ Object
134 135 136 137 |
# File 'lib/activemessaging/gateway.rb', line 134 def filter filter, = {} [:direction] = :bidirectional if [:direction].nil? filters << [filter, ] end |
.find_destination(destination_name) ⇒ Object Also known as: find_queue
281 282 283 284 285 |
# File 'lib/activemessaging/gateway.rb', line 281 def find_destination destination_name real_destination = named_destinations[destination_name] raise "You have not yet defined a destination named #{destination_name}. Destinations currently defined are [#{named_destinations.keys.join(',')}]" if real_destination.nil? real_destination end |
.load_connection_configuration(label = 'default') ⇒ Object
370 371 372 373 374 375 376 377 378 379 380 |
# File 'lib/activemessaging/gateway.rb', line 370 def load_connection_configuration(label='default') @broker_yml = YAML::load(ERB.new(IO.read(File.join(ActiveMessaging.app_root, 'config', 'broker.yml'))).result) if @broker_yml.nil? if label == 'default' config = @broker_yml[ActiveMessaging.app_env].symbolize_keys else config = @broker_yml[ActiveMessaging.app_env][label].symbolize_keys end config[:adapter] = config[:adapter].to_sym if config[:adapter] config[:adapter] ||= :stomp return config end |
.prepare_application ⇒ Object
203 204 205 206 207 208 209 210 211 |
# File 'lib/activemessaging/gateway.rb', line 203 def prepare_application return unless defined?(ActiveRecord) if ActiveRecord::VERSION::MAJOR >= 4 ActiveRecord::Base.connection_pool.connections.map(&:verify!) else ActiveRecord::Base.verify_active_connections! end end |
.processor_group(group_name, *processors) ⇒ Object
340 341 342 343 344 345 346 |
# File 'lib/activemessaging/gateway.rb', line 340 def processor_group group_name, *processors if processor_groups.has_key? group_name processor_groups[group_name] = processor_groups[group_name] + processors else processor_groups[group_name] = processors end end |
.publish(destination_name, body, publisher = nil, headers = {}, timeout = 10) ⇒ Object
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 |
# File 'lib/activemessaging/gateway.rb', line 297 def publish destination_name, body, publisher=nil, headers={}, timeout=10 raise "You cannot have a nil or empty destination name." if destination_name.nil? raise "You cannot have a nil or empty message body." if (body.nil? || body.empty?) real_destination = find_destination(destination_name) details = { :publisher => publisher, :destination => real_destination, :direction => :outgoing } = OpenStruct.new(:body => body, :headers => headers.reverse_merge(real_destination.publish_headers)) begin Timeout.timeout timeout do execute_filter_chain(:outgoing, , details) do || connection(real_destination.broker_name).send real_destination.value, .body, .headers end end rescue Timeout::Error=>toe ActiveMessaging.logger.error("Timed out trying to send the message #{} to destination #{destination_name} via broker #{real_destination.broker_name}") raise toe end end |
.receive(destination_name, receiver = nil, subscribe_headers = {}, timeout = 10) ⇒ Object
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 |
# File 'lib/activemessaging/gateway.rb', line 320 def receive destination_name, receiver=nil, subscribe_headers={}, timeout=10 raise "You cannot have a nil or empty destination name." if destination_name.nil? conn = nil dest = find_destination destination_name config = load_connection_configuration(dest.broker_name) subscribe_headers['id'] = receiver.name.underscore unless (receiver.nil? or subscribe_headers.key? 'id') Timeout.timeout timeout do conn = Gateway.adapters[config[:adapter]].new(config) conn.subscribe(dest.value, subscribe_headers) = conn.receive conn.received , subscribe_headers return end rescue Timeout::Error=>toe ActiveMessaging.logger.error("Timed out trying to receive a message on destination #{destination_name}") raise toe ensure conn.disconnect unless conn.nil? end |
.register_adapter(adapter_name, adapter_class) ⇒ Object
130 131 132 |
# File 'lib/activemessaging/gateway.rb', line 130 def register_adapter adapter_name, adapter_class Gateway.adapters[adapter_name] = adapter_class end |
.reset ⇒ Object
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/activemessaging/test_helper.rb', line 17 def self.reset unsubscribe disconnect @filters = [] @subscriptions = {} @named_destinations = {} @processor_groups = {} @current_processor_group = nil @connections = {} end |
.reset_application ⇒ Object
213 214 |
# File 'lib/activemessaging/gateway.rb', line 213 def reset_application end |
.start ⇒ Object
Starts up an message listener to start polling for messages on each configured connection, and dispatching processing
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/activemessaging/gateway.rb', line 28 def start # subscribe - creating connections along the way subscribe # for each connection, start a thread @connections.each do |name, conn| @connection_threads[name] = Thread.start do while @running begin Thread.current[:message] = nil Thread.current[:message] = conn.receive #catch these but then stop looping rescue StopProcessingException=>spe ActiveMessaging.logger.error "ActiveMessaging: thread[#{name}]: Processing Stopped - receive interrupted, will process last message if already received" # break #catch all others, but go back and try and recieve again rescue Object=>exception ActiveMessaging.logger.error "ActiveMessaging: thread[#{name}]: Exception from connection.receive: #{exception.}\n" + exception.backtrace.join("\n\t") ensure if Thread.current[:message] @guard.synchronize { dispatch Thread.current[:message] } Thread.current[:message] = nil else # if there is no message at all, sleep # maybe this should be configurable sleep(1) end end Thread.pass end ActiveMessaging.logger.error "ActiveMessaging: thread[#{name}]: receive loop terminated" end end while @running trap("TERM", "EXIT") living = false @connection_threads.each { |name, thread| living ||= thread.alive? } @running = living sleep(1) end ActiveMessaging.logger.error "All connection threads have died..." rescue Interrupt ActiveMessaging.logger.error "\n<<Interrupt received>>\n" rescue Object=>exception ActiveMessaging.logger.error "#{exception.class.name}: #{exception.}\n\t#{exception.backtrace.join("\n\t")}" raise exception ensure ActiveMessaging.logger.error "Cleaning up..." stop ActiveMessaging.logger.error "=> END" end |
.stop ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/activemessaging/gateway.rb', line 84 def stop # first tell the threads to stop their looping, so they'll stop when next complete a receive/dispatch cycle @running = false # if they are dispatching (i.e. !thread[:message].nil?), wait for them to finish # if they are receiving (i.e. thread[:message].nil?), stop them by raising exception dispatching = true while dispatching dispatching = false @connection_threads.each do |name, thread| if thread[:message] dispatching = true # if thread got killed, but dispatch not done, try it again if thread.alive? ActiveMessaging.logger.error "Waiting on thread #{name} to finish processing last message..." else ActiveMessaging.logger.error "Starting thread #{name} to finish processing last message..." msg = thread[:message] thread.exit thread = Thread.start do begin Thread.current[:message] = msg dispatch Thread.current[:message] ensure Thread.current[:message] = nil end end end else thread.raise StopProcessingException, "Time to stop." if thread.alive? end end sleep(1) end unsubscribe disconnect end |
.subscribe ⇒ Object
139 140 141 |
# File 'lib/activemessaging/gateway.rb', line 139 def subscribe subscriptions.each { |key, subscription| subscription.subscribe } end |
.subscribe_to(destination_name, processor, headers = {}) ⇒ Object
289 290 291 292 293 294 295 |
# File 'lib/activemessaging/gateway.rb', line 289 def subscribe_to destination_name, processor, headers={} proc_name = processor.name.underscore proc_sym = processor.name.underscore.to_sym if (!current_processor_group || processor_groups[current_processor_group].include?(proc_sym)) @subscriptions["#{proc_name}:#{destination_name}"]= Subscription.new(find_destination(destination_name), processor, headers) end end |
.unsubscribe ⇒ Object
143 144 145 |
# File 'lib/activemessaging/gateway.rb', line 143 def unsubscribe subscriptions.each { |key, subscription| subscription.unsubscribe } end |