Class: StompMessage::StompZActiveRecordServer

Inherits:
StompServer
  • Object
show all
Defined in:
lib/stomp_message/stomp_z_active_record_server.rb

Overview

statistics listening class. Updates the statistics at every message/event had to name it Z due to order problems. not sure why.

Instance Attribute Summary collapse

Attributes inherited from StompServer

#conn, #exception_count, #guard, #host, #jms_source, #login, #msg_count, #mythreads, #password, #port, #queue, #ss_start_time, #thread_count, #topic, #variables

Instance Method Summary collapse

Methods inherited from StompServer

#check_origin, #check_session, #check_thread, #close_topic, #connect_connection, #connect_topic, #create_dest_conn, #create_jms_mdb_connections, #disconnect_stomp, #do_jms_setup, #get_id, #handle_exception, #handle_message, #java?, #jms_auto_close, #jms_close_connections, #jms_message_handling, #method_missing, #monitor_queue_status, #reconnect, #run, #send_reply, #setup_auto_close, #setup_thread_specific_items, #stomp_DEBUG, #stomp_PING, #stomp_RECONNECT, #version_number

Methods included from JmsTools

#define_source_id, #jms_close_producer_session, #jms_create_consumer, #jms_create_consumer_session, #jms_create_destination_connection, #jms_create_producer, #jms_create_producer_session, #jms_create_session, #jms_kill_logging, #jms_manage_headers, #jms_message_for_me, #jms_my_id, #jms_next_transaction_id, #jms_on_message, #jms_persistent, #jms_send_ack, #jms_send_message, #jms_set_debug, #jms_shutdown, #jms_start, #unique_source_id

Constructor Details

#initialize(options = {}) ⇒ StompZActiveRecordServer

Returns a new instance of StompZActiveRecordServer.



12
13
14
15
16
17
18
# File 'lib/stomp_message/stomp_z_active_record_server.rb', line 12

def initialize(options={})
  super(options)
  puts "root_path: #{options[:root_path]} rails_environment #{options[:env]}"
  setup_active_record(options[:root_path], options[:env])
  puts "#{self.class}: finished Active Record initializing"
 # self.model_list = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class StompMessage::StompServer

Instance Attribute Details

#database_envObject (readonly)

Returns the value of attribute database_env.



11
12
13
# File 'lib/stomp_message/stomp_z_active_record_server.rb', line 11

def database_env
  @database_env
end

#model_listObject

Returns the value of attribute model_list.



10
11
12
# File 'lib/stomp_message/stomp_z_active_record_server.rb', line 10

def model_list
  @model_list
end

Instance Method Details

#check_active_record_connection(my_class) ⇒ Object

note this is specific tomysql… need to fix later



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/stomp_message/stomp_z_active_record_server.rb', line 21

def check_active_record_connection(my_class)
	 begin
	   ar_conn=my_class.new.connection
     puts " checking AR connection status: #{ar_conn.active?}"
     puts " AR down... reconnecting"  if !ar_conn.active?
   #  puts "after first check"
     ar_conn.reconnect! if !ar_conn.active?
   # puts "after reconnect"
  rescue Mysql::Error
     puts "reconnecting due to Mysql:Error"
     ar_conn.reconnect
  rescue Exception => e
    puts "ActiveRecord found exception that should not be here #{e.message}"
  end
end

#check_ar_connectionObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/stomp_message/stomp_z_active_record_server.rb', line 36

def check_ar_connection
	 begin
	       ActiveRecord::Base.establish_connection(self.database_env)    if !ActiveRecord::Base.connected?
	       
	   ar_conn=ActiveRecord::Base.connection
     puts " checking AR connection status: #{ar_conn.active?}"
     puts " AR down... reconnecting"  if !ar_conn.active?
   #  puts "after first check"
     ar_conn.reconnect! if !ar_conn.active?
   # puts "after reconnect"
  rescue Mysql::Error
     puts "reconnecting due to Mysql:Error"
     ar_conn.reconnect
  rescue Exception => e
    puts "ActiveRecord found exception that should not be here #{e.message}"
  end
end

#establish_ar_jdbc_poolObject



118
119
120
121
# File 'lib/stomp_message/stomp_z_active_record_server.rb', line 118

def establish_ar_jdbc_pool
   puts "----- establish jdbc pool" 
   check_ar_connection
end

#free_ar_jdbc_poolObject



122
123
124
125
126
# File 'lib/stomp_message/stomp_z_active_record_server.rb', line 122

def free_ar_jdbc_pool
  puts "---- free pool"
 # ActiveRecord::Base.connection.disconnect!
#  ActiveRecord::Base.remove_connection
end

#load_models(root_path) ⇒ Object

ActiveRecord::Base.remove_connection



127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/stomp_message/stomp_z_active_record_server.rb', line 127

def load_models(root_path)
     model_path = root_path  + "app/models/"
  # puts "model path is #{model_path}"
  last_model=""
  self.model_list.each { |model_file|
      lib = model_path + model_file
      last_model = require lib 
     puts "loading required model: is #{lib}" if @debug
     # last_model=lib  #last one needs to be active_record  (need to fix)
      }
      puts "last model is #{last_model}"
     monitor_ar_status(eval("#{last_model[0]}")) if !self.java?
end

#monitor_ar_status(connection) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/stomp_message/stomp_z_active_record_server.rb', line 53

def monitor_ar_status(connection)
  puts "starting up active record monitor status"
  self.mythreads << Thread.new { while true
                 begin
                   sleep(1000)
                   check_active_record_connection(connection)
                 rescue Exception => e
                          handle_exception(e)
                 end
               end  # while
                }              
end

#onMessage(msg_body, msg_hash) ⇒ Object

load the AR models… override if necessary model list is an array of models to load



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/stomp_message/stomp_z_active_record_server.rb', line 107

def onMessage(msg_body,msg_hash)
    puts "----> Stomp Z AR on Message"
    if check_origin(msg_hash)
       establish_ar_jdbc_pool
       super(msg_body,msg_hash)
       free_ar_jdbc_pool
     end
     msg_body=msg_hash=nil
      puts "<---- Stomp Z AR on Message exit"
      
end

#server_shutdownObject



140
141
142
143
144
145
146
# File 'lib/stomp_message/stomp_z_active_record_server.rb', line 140

def server_shutdown
  puts "---->shutting down AR"
  free_ar_jdbc_pool
  super
   puts "<----shut down AR"
  
end

#setup_active_record(root_path, tenv) ⇒ Object

table names if needed but easiest and simplest is to give path to to the rails application



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/stomp_message/stomp_z_active_record_server.rb', line 68

def setup_active_record(root_path,tenv)
 require 'rubygems'
  if self.java?
    env = 'development'
    env = 'production' if Socket.gethostname=='svbalance.cure.com.ph'
    env = tenv if tenv!=nil
  end
 # env = 'production'  #now using connection pools so always production
 #ENV['RAILS_ENV'] ||= env
 #RAILS_ENV=env
 gem 'activerecord'
 require 'active_record'
 
 # ENV['RAILS_ENV'] ||= env
 # RAILS_ENV=env
  path= root_path + "config/database.yml"
  puts "path is #{path}" if @debug
  data=File.open(path).readlines.join
 # puts "file open"
  result=ERB.new(data).result
 # puts "after erb #{result}"
  parsed=YAML.load(result)
#  puts " values are: #{parsed.to_s}" # if @debug
  puts "env is #{env} values are: #{parsed[env]}"  if @debug
 
  @database_env=parsed[env]
   # puts "db temp #{self.database_env.to_s}"
   puts "Database settings: #{self.database_env.inspect} from environment #{env} in database.yml"
   puts "JNDI Needed: #{self.database_env['jndi']} please ensure configured!" if self.database_env['jndi'] !=nil
  ActiveRecord::Base.allow_concurrency = true
  establish_ar_jdbc_pool
  ActiveRecord::Base.logger = Logger.new(STDOUT)  #RAILS_DEFAULT_LOGGER
  # grab all the models
#   model_path = root_path + "app/models/*.rb"
  load_models(root_path)
  #puts "after DIR"
end