Class: RMB::ListenerMain
- Inherits:
-
Object
- Object
- RMB::ListenerMain
- Defined in:
- lib/listener_main.rb
Overview
This is the class that gets instantiated from the listener daemon It contains the worker code of the daemon
Instance Attribute Summary collapse
-
#hash ⇒ Object
Returns the value of attribute hash.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#submitter ⇒ Object
Returns the value of attribute submitter.
-
#subscriber ⇒ Object
Returns the value of attribute subscriber.
Instance Method Summary collapse
- #app_name ⇒ Object
-
#initialize(root, key) ⇒ ListenerMain
constructor
initialize
Given root (the working directory or RAILS_ROOT) and key (the unique identifier that denotes a particular Listener instance), the initialize method sets up the entire daemon. -
#run ⇒ Object
run
* The subscriber is connected to a message broker, * the submitter is connected to the rails controller, * then an infinite loop is entered * subscriber waits to receive a message * when a message arrives, it is sent to the rails controller via the submitter * …and loop forever.
Constructor Details
#initialize(root, key) ⇒ ListenerMain
initialize
Given root (the working directory or RAILS_ROOT) and key (the unique identifier that denotes a particular Listener instance), the initialize method sets up the entire daemon.
-
The daemon name is created from the key
-
A logger object is instantiated for the use of the daemon components
-
A hash of properties is read from a properties file, whose name is also derived from the root and key.
-
An instance of a subclass of Subscriber is created to act as the front end of the daemon, listening on a topic or queue for the arrival of a message
-
An instance of a subclass of Submitter is created to send messages on to a parent rails app, using RESTful techniques to send the message to a rails controller.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/listener_main.rb', line 17 def initialize(root, key) @key = key @logger = Logger.new("#{root}/log/#{app_name}.log") @logger.info "\nStarting #{File.basename(__FILE__)} --> #{app_name}..." @hash = nil # load the marshalled hash of properties f = File.join("#{root}", "tmp", "properties", "#{app_name}.properties") File.open(f) do |props| @hash = Marshal.load(props) end @hash[:logger] = @logger # instantiate the two objects that comprise the front-end and the back-end of # this listener daemon front = @hash[:subscriber] @subscriber = Kernel.eval "#{front[:class_name]}.new" @subscriber.properties=@hash back = @hash[:submitter] @submitter = Kernel.eval "#{back[:class_name]}.new" @submitter.properties=@hash end |
Instance Attribute Details
#hash ⇒ Object
Returns the value of attribute hash.
7 8 9 |
# File 'lib/listener_main.rb', line 7 def hash @hash end |
#logger ⇒ Object
Returns the value of attribute logger.
7 8 9 |
# File 'lib/listener_main.rb', line 7 def logger @logger end |
#submitter ⇒ Object
Returns the value of attribute submitter.
7 8 9 |
# File 'lib/listener_main.rb', line 7 def submitter @submitter end |
#subscriber ⇒ Object
Returns the value of attribute subscriber.
7 8 9 |
# File 'lib/listener_main.rb', line 7 def subscriber @subscriber end |
Instance Method Details
#app_name ⇒ Object
78 79 80 |
# File 'lib/listener_main.rb', line 78 def app_name "#{RMB::Properties.daemon_prefix}#{@key}" end |
#run ⇒ Object
run
-
The subscriber is connected to a message broker,
-
the submitter is connected to the rails controller,
-
then an infinite loop is entered
-
subscriber waits to receive a message
-
when a message arrives, it is sent to the rails controller via the submitter
-
…and loop forever
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 |
# File 'lib/listener_main.rb', line 46 def run begin subscriber.connect logger.info "subscriber connected." rescue Exception logger.fatal "#{__FILE__}:#{__LINE__} Exception #{$!}" raise end begin submitter.connect logger.info "submitter logged in." rescue Exception logger.fatal "#{__FILE__}:#{__LINE__} Exception #{$!}" raise end logger.info "Waiting for messages in #{subscriber.url}." loop do begin = subscriber.receive rescue logger.fatal "#{__FILE__}:#{__LINE__} Exception #{$!}" raise end begin submitter.send() rescue Exception logger.fatal "#{__FILE__}:#{__LINE__} Exception #{$!}" raise end end end |