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.

{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(queue_manager, config, logger_obj) ⇒ Dispatchers

Returns a new instance of Dispatchers.



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

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

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



24
25
26
# File 'lib/ap4r/dispatcher.rb', line 24

def config
  @config
end

#groupObject (readonly)

Returns the value of attribute group.



24
25
26
# File 'lib/ap4r/dispatcher.rb', line 24

def group
  @group
end

Class Method Details

.loggerObject



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

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.



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

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.



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

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



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

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.



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

def targets
  @dispatch_targets
end