Class: Ap4r::Dispatchers

Inherits:
Object show all
Defined in:
lib/ap4r/dispatcher.rb

Overview

Represents a group of dispatchers. Responsibilities are follows:

  • polls target queues,

  • gets messages from queues, and

  • calls a Dispatchers::Base‘s instance.

Defined Under Namespace

Classes: Base, Druby, Http, SOAP, XmlRpc

Constant Summary collapse

@@sleep_inverval =
0.1
@@logger =
nil
@@subclasses =

storage for Dispatchers::Base‘s subclasses.

{}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(queue_manager, config, logger_obj) ⇒ Dispatchers

Returns a new instance of Dispatchers.



45
46
47
48
49
50
51
52
53
# File 'lib/ap4r/dispatcher.rb', line 45

def initialize(queue_manager, config, logger_obj)
  @qm = queue_manager
  @config = config  # (typically) dispatcher section of queues.cfg
  @@logger ||= logger_obj
  raise "no configuration specified" unless @config
  @group = ThreadGroup.new
  # TODO: needs refinement 2007/05/30 by shino
  @dispatch_targets = ""
end

Class Method Details

.loggerObject



27
28
29
# File 'lib/ap4r/dispatcher.rb', line 27

def self.logger
  @@logger
end

.register_dispatcher_class(mode, klass) ⇒ Object

Stores klass for a dispatch mode mode Each klass is used to create instances to handle dispatching messages.



36
37
38
# File 'lib/ap4r/dispatcher.rb', line 36

def self.register_dispatcher_class(mode, klass)
  @@subclasses[mode] = klass
end

Instance Method Details

#startObject

Starts every dispatcher. If an exception is detected, this method raise it through with logging.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/ap4r/dispatcher.rb', line 58

def start
  begin
    logger.info{ "about to start dispatchers with config\n#{@config.to_yaml}" }
    @config.each{ |conf|
      conf["threads"].to_i.times { |index|
        Thread.fork(@group, conf, index){|group, conf, index|
          dispatching_loop(group, conf, index)
        }
      }
      @dispatch_targets.concat(conf["targets"]).concat(';')
      logger.debug{ "dispatch targets are : #{@dispatch_targets}" }
    }
    logger.info "queue manager has forked dispatchers"
  rescue Exception => err
    logger.warn{"Error in starting dipatchers #{err}"}
    logger.warn{err.backtrace.join("\n")}
    raise err
  end
end

#stopObject

Stops every dispatcher. Current implementation makes just dying flags up. Some threads don’t stop quickly in some cases such as blocking at socket read. – TODO: needs forced mode? 2007/05/09 by shino



83
84
85
86
87
88
89
# File 'lib/ap4r/dispatcher.rb', line 83

def stop
  logger.info{"stop_dispatchers #{@group}"}
  return unless @group
  @group.list.each {|d| d[:dying] = true}
  @group.list.each {|d| d.join }
  @dispatch_targets = ""
end

#targetsObject

Sum of each dispatcher’s target queues.



41
42
43
# File 'lib/ap4r/dispatcher.rb', line 41

def targets
  @dispatch_targets
end