stomp_message 0.1.2 → 0.1.8
Sign up to get free protection for your applications and to get access to all the features.
- data/Manifest.txt +1 -0
- data/README.txt +6 -0
- data/bin/stomp_message_send.rb +24 -57
- data/lib/stomp_message/message.rb +112 -5
- data/lib/stomp_message/stomp_participant.rb +0 -2
- data/lib/stomp_message/stomp_send_topic.rb +9 -6
- data/lib/stomp_message/stomp_server.rb +46 -19
- data/lib/stomp_message/stomp_z_active_record_server.rb +81 -0
- data/lib/stomp_message/version.rb +1 -1
- data/test/test_stomp_message.rb +12 -0
- metadata +3 -2
data/Manifest.txt
CHANGED
@@ -9,6 +9,7 @@ lib/stomp_message.rb
|
|
9
9
|
lib/stomp_message/version.rb
|
10
10
|
lib/stomp_message/stomp_server.rb
|
11
11
|
lib/stomp_message/stomp_statistics_server.rb
|
12
|
+
lib/stomp_message/stomp_z_active_record_server.rb
|
12
13
|
lib/stomp_message/stomp_send_topic.rb
|
13
14
|
lib/stomp_message/stomp_participant.rb
|
14
15
|
lib/stomp_message/message.rb
|
data/README.txt
CHANGED
@@ -1,5 +1,11 @@
|
|
1
1
|
A basic package for sending and receiving stomp messages
|
2
2
|
|
3
|
+
Active Record Server Class
|
4
|
+
|
5
|
+
This extends the server class to access active record. It is based on the rail app structure where the config file has database.yml and the app/models/*.rb contains all the files that are needed for active record. Note that there is some strange code to pick up class name of last file loaded for checking the AR connection. THis needs to be fixed.
|
6
|
+
|
7
|
+
But in general with the AR Server class you can recieve messages and write them to active record databases.
|
8
|
+
|
3
9
|
|
4
10
|
A server class will listen to topics (StompMessage::StompServer)
|
5
11
|
|
data/bin/stomp_message_send.rb
CHANGED
@@ -1,3 +1,22 @@
|
|
1
|
+
# == Synopsis
|
2
|
+
# Send a message to a stomp server and wait for result if requested
|
3
|
+
# == Usage
|
4
|
+
# stomp_message_send.rb -T topic -M command -b body -h host -p port "
|
5
|
+
# Debug: to turn on debug: stomp_message_send.rb -T '/topic/sms' -M stomp_DEBUG -b nil
|
6
|
+
# Statistics: stomp_message_send.rb -T '/topic/sms' -M stomp_REPORT -b nil -A true
|
7
|
+
# Reconnect: stomp_message_send.rb -T '/topic/sms' -M stomp_RECONNECT -b nil -A true ---- NOT USED OFTEN
|
8
|
+
# Ping: for ping test: stomp_message_send.rb -T '/topic/sms' -M stomp_PING -b nil -A true
|
9
|
+
# Ping: for email ping test: stomp_message_send.rb -T '/topic/sms' -M stomp_PING -b nil -A true -D comma_separated_email_addresses"
|
10
|
+
# Note -a flag for response --- eg true or false
|
11
|
+
# == Useful commands
|
12
|
+
# stomp_message_send.rb -T '/topic/sms' -M stomp_PING -b nil -A true
|
13
|
+
# stomp_message_send.rb -T '/topic/sms' -M stomp_DEBUG -b nil
|
14
|
+
# stomp_message_send.rb -T '/topic/sms' -M stomp_REPORT -b nil -A true -D email@com
|
15
|
+
# == Author
|
16
|
+
# Scott Sproule --- Ficonab.com (scott.sproule@ficonab.com)
|
17
|
+
# == Copyright
|
18
|
+
# Copyright (c) 2007 Ficonab Pte. Ltd.
|
19
|
+
# See license for license details
|
1
20
|
require 'yaml'
|
2
21
|
require 'rubygems'
|
3
22
|
gem 'stomp'
|
@@ -5,64 +24,12 @@ require 'stomp'
|
|
5
24
|
require 'optparse'
|
6
25
|
gem 'stomp_message'
|
7
26
|
require 'stomp_message'
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
puts "for statistics report: stomp_message_send.rb -t '/topic/sms' -m stomp_REPORT -b nil -a true"
|
12
|
-
puts "for statistics report: stomp_message_send.rb -t '/topic/sms' -m stomp_REPORT -b nil -a true"
|
13
|
-
puts "for ping test: stomp_message_send.rb -t '/topic/sms' -m stomp_PING -b nil -a true"
|
14
|
-
puts "for email ping test: stomp_message_send.rb -t '/topic/sms' -m stomp_PING -b nil -a true -d comma_separated_email_addresses"
|
15
|
-
puts "-a flag for response"
|
16
|
-
|
17
|
-
exit
|
18
|
-
end
|
19
|
-
def parse_options(params)
|
20
|
-
opts = OptionParser.new
|
21
|
-
puts "argv are #{params}"
|
22
|
-
#params_split = params.split(' ')
|
23
|
-
#puts "paramsp is #{paramsp}"
|
24
|
-
topic_flag=command_flag=body_flag=true
|
25
|
-
temp_hash = {}
|
26
|
-
temp_hash[:ack]= 'false' # no ack by default
|
27
|
-
temp_hash[:msisdn] = 'not_defined'
|
28
|
-
email_flag=false
|
29
|
-
opts.on("-h","--host VAL", String) {|val| temp_hash[:host ] = val
|
30
|
-
# puts "host is #{val}"
|
31
|
-
}
|
32
|
-
opts.on("-d","--email VAL", String) {|val| temp_hash[:email ] = val
|
33
|
-
puts "email address is #{val}"
|
34
|
-
# puts "host is #{val}"
|
35
|
-
}
|
36
|
-
|
37
|
-
opts.on("-p","--port VAL", String) {|val| temp_hash[:port ] = val
|
38
|
-
# puts "port is #{val}"
|
39
|
-
}
|
40
|
-
opts.on("-a","--ack VAL", String) {|val| temp_hash[:ack ] = val
|
41
|
-
puts "ack is #{val}"
|
42
|
-
}
|
27
|
+
require 'rdoc/usage'
|
28
|
+
|
29
|
+
|
43
30
|
|
44
|
-
|
45
|
-
|
46
|
-
user_flag=false }
|
47
|
-
opts.on("-t","--topic VAL", String) {|val| temp_hash[:topic ] = val
|
48
|
-
# puts "topic is #{val}"
|
49
|
-
topic_flag=false }
|
50
|
-
opts.on("-m","--msg_command VAL", String) {|val| temp_hash[:command ] = val
|
51
|
-
# puts "command is #{val}"
|
52
|
-
command_flag=false }
|
53
|
-
opts.on("-b","--body VAL", String) {|val| temp_hash[:body ] = val
|
54
|
-
# puts "body is #{val}"
|
55
|
-
body_flag=false }
|
56
|
-
#opts.on("-d","--database VAL", String) {|val| temp_hash[:db ] = val }
|
57
|
-
#opts.on("-p","--password VAL", String) {|val| temp_hash[:password ] = val }
|
58
|
-
#opts.on("-u","--user VAL", String) {|val| temp_hash[:user ] = val }
|
59
|
-
#puts " in test commander option parse #{port} #{url}"
|
60
|
-
opts.parse(params)
|
61
|
-
# puts " in HTTP #{hostname} port #{port} url: #{url}"
|
62
|
-
usage if topic_flag or body_flag or command_flag
|
63
|
-
return temp_hash
|
64
|
-
end
|
65
|
-
arg_hash=parse_options(ARGV)
|
31
|
+
arg_hash=StompMessage::Options.parse_options(ARGV)
|
32
|
+
RDoc::usage if arg_hash[:topic]==nil || arg_hash[:command]==nil || arg_hash[:help]==true
|
66
33
|
require 'pp'
|
67
34
|
options = arg_hash
|
68
35
|
# set up variables using hash
|
@@ -1,10 +1,64 @@
|
|
1
1
|
# basic format for stomp messages
|
2
2
|
require 'rexml/document'
|
3
3
|
#require 'xml_helper.rb'
|
4
|
-
require 'rubygems'
|
5
|
-
gem 'stomp_message'
|
6
|
-
require 'stomp_message'
|
4
|
+
#require 'rubygems'
|
5
|
+
#gem 'stomp_message'
|
6
|
+
#require 'stomp_message'
|
7
|
+
require 'optparse'
|
8
|
+
require 'rdoc/usage'
|
9
|
+
|
10
|
+
|
7
11
|
module StompMessage
|
12
|
+
class Options
|
13
|
+
def self.parse_options(params)
|
14
|
+
opts = OptionParser.new
|
15
|
+
puts "argv are #{params}"
|
16
|
+
temp_hash = {}
|
17
|
+
temp_hash[:ack]= 'false' # no ack by default
|
18
|
+
temp_hash[:msisdn] = 'not_defined'
|
19
|
+
email_flag=false
|
20
|
+
opts.on("-h","--host VAL", String) {|val| temp_hash[:host ] = val
|
21
|
+
# puts "host is #{val}"
|
22
|
+
}
|
23
|
+
opts.on("-D","--email VAL", String) {|val| temp_hash[:email ] = val }
|
24
|
+
|
25
|
+
opts.on("-p","--port VAL", String) {|val| temp_hash[:port ] = val }
|
26
|
+
opts.on("-S","--subject VAL", String) {|val| temp_hash[:subject ] = val }
|
27
|
+
opts.on("-s","--source VAL", String) {|val| temp_hash[:source ] = val }
|
28
|
+
opts.on("-A","--ack VAL", String) {|val| temp_hash[:ack ] = val
|
29
|
+
puts "ack is #{val}"
|
30
|
+
}
|
31
|
+
|
32
|
+
opts.on("-u","--user VAL", String) {|val| temp_hash[:user ] = val }
|
33
|
+
# puts "user is #{val}"
|
34
|
+
opts.on("-T","--topic VAL", String) {|val| temp_hash[:topic ] = val }
|
35
|
+
opts.on("-t","--text VAL", String) {|val| temp_hash[:text ] = val }
|
36
|
+
|
37
|
+
opts.on("-M","--msg_command VAL", String) {|val| temp_hash[:command ] = val }
|
38
|
+
|
39
|
+
opts.on("-b","--body VAL", String) {|val| temp_hash[:body ] = val }
|
40
|
+
opts.on("-k","--keyword VAL", String) {|val| temp_hash[:keyword ] = val
|
41
|
+
puts "keyword is #{val}"
|
42
|
+
keyword_flag=false }
|
43
|
+
opts.on("-m","--msisdn VAL", String) {|val| temp_hash[:msisdn ] = val }
|
44
|
+
opts.on("-U","--url VAL", String) {|val| temp_hash[:url ] = val }
|
45
|
+
|
46
|
+
opts.on("-a","--action VAL", String) {|val| temp_hash[:action ] = val }
|
47
|
+
|
48
|
+
opts.on("-B","--broadcast VAL", String) {|val| temp_hash[:broadcast ] = val }
|
49
|
+
opts.on("-x","--help", "get help") { |val| temp_hash[:help]=true }
|
50
|
+
|
51
|
+
opts.on("-d","--debug", "turn on debug") { |val| temp_hash[:debug ] = true }
|
52
|
+
|
53
|
+
|
54
|
+
opts.parse(params)
|
55
|
+
# puts " in HTTP #{hostname} port #{port} url: #{url}"
|
56
|
+
|
57
|
+
return temp_hash
|
58
|
+
|
59
|
+
end # parse options
|
60
|
+
end
|
61
|
+
# help build xml commands from messages
|
8
62
|
module XmlHelper
|
9
63
|
# create elements from instance variables... (instance variablesneed to be set)
|
10
64
|
#array variables need to end in s (eg mssidns) and are handled recurvisely
|
@@ -37,7 +91,18 @@ module StompMessage
|
|
37
91
|
element_xml= REXML::Element.new element_name
|
38
92
|
element_xml.text=element_value
|
39
93
|
element_xml
|
40
|
-
end
|
94
|
+
end
|
95
|
+
def load_iv(iv_input,doc)
|
96
|
+
iv=iv_input.delete('@')
|
97
|
+
val = REXML::XPath.first(doc, "//#{iv}").text
|
98
|
+
puts "in load iv #{iv} and val: #{val}"
|
99
|
+
eval "self.#{iv}=#{val}"
|
100
|
+
end
|
101
|
+
def load_instance_variables(xml_string)
|
102
|
+
xml_doc=REXML::Document.new(xml_string)
|
103
|
+
self.instance_variables.each {|iv| load_iv(iv,xml_doc) }
|
104
|
+
|
105
|
+
end
|
41
106
|
# add all elements to top variable
|
42
107
|
def add_elements(top)
|
43
108
|
elements = []
|
@@ -50,6 +115,8 @@ module StompMessage
|
|
50
115
|
end
|
51
116
|
|
52
117
|
end
|
118
|
+
# basic stomp message class
|
119
|
+
# sent between clients and servers... subclasss and add instance variables.
|
53
120
|
class Message
|
54
121
|
include StompMessage::XmlHelper
|
55
122
|
attr_accessor :__stomp_msg_command, :__stomp_msg_body
|
@@ -93,5 +160,45 @@ module StompMessage
|
|
93
160
|
end
|
94
161
|
sms
|
95
162
|
end
|
163
|
+
def self.load_xml_new(xml_string)
|
164
|
+
begin
|
165
|
+
# doc=REXML::Document.new(xml_string)
|
166
|
+
test= self.new('temp', 'temp')
|
167
|
+
test.load_instance_variables(xml_string)
|
168
|
+
#sms=StompMessage::Message.new(test.command, test.body)
|
169
|
+
rescue Exception => e
|
170
|
+
puts "Exception in load xml:#{xml_string}"
|
171
|
+
puts "message #{e.message}"
|
172
|
+
end
|
173
|
+
test
|
174
|
+
end
|
96
175
|
end
|
97
|
-
end
|
176
|
+
end
|
177
|
+
# if a ruby gem file then you need to grab second in caller array
|
178
|
+
def RDoc.usage_no_exit(*args)
|
179
|
+
main_program_file = caller[1].sub(/:\d+$/, '')
|
180
|
+
puts "main program is #{main_program_file} caller is #{caller.inspect}"
|
181
|
+
comment = File.open(main_program_file) do |file|
|
182
|
+
find_comment(file)
|
183
|
+
end
|
184
|
+
|
185
|
+
comment = comment.gsub(/^\s*#/, '')
|
186
|
+
|
187
|
+
markup = SM::SimpleMarkup.new
|
188
|
+
flow_convertor = SM::ToFlow.new
|
189
|
+
|
190
|
+
flow = markup.convert(comment, flow_convertor)
|
191
|
+
|
192
|
+
format = "plain"
|
193
|
+
|
194
|
+
unless args.empty?
|
195
|
+
flow = extract_sections(flow, args)
|
196
|
+
end
|
197
|
+
|
198
|
+
options = RI::Options.instance
|
199
|
+
if args = ENV["RI"]
|
200
|
+
options.parse(args.split)
|
201
|
+
end
|
202
|
+
formatter = options.formatter.new(options, "")
|
203
|
+
formatter.display_flow(flow)
|
204
|
+
end
|
@@ -49,19 +49,20 @@ class StompSendTopic
|
|
49
49
|
#manage timeout etc...
|
50
50
|
def self.open_connection(old_conn,login,pass,host,port)
|
51
51
|
conn=old_conn
|
52
|
-
|
52
|
+
count=0
|
53
|
+
flag= false
|
53
54
|
begin
|
54
55
|
conn.close if conn!=nil
|
55
|
-
|
56
|
-
Timeout::timeout(
|
56
|
+
count+=1
|
57
|
+
Timeout::timeout(15) {
|
57
58
|
conn=nil
|
58
59
|
conn = Stomp::Client.new(login, pass, host, port, false)
|
59
60
|
flag=true
|
60
61
|
}
|
61
62
|
|
62
63
|
rescue Timeout::Error
|
63
|
-
puts "Timeout error: exception retrying flag is: #{flag}"
|
64
|
-
retry if !flag
|
64
|
+
puts "Timeout error: exception retrying flag is: #{flag} retry number #{count}"
|
65
|
+
retry if !flag and count < 4
|
65
66
|
# raise "timeout"
|
66
67
|
|
67
68
|
end
|
@@ -87,7 +88,8 @@ class StompSendTopic
|
|
87
88
|
response_header = {"Content-type" => "text/xml"}
|
88
89
|
response_header.merge headers
|
89
90
|
ht =Net::HTTP.start(self.host,self.port)
|
90
|
-
|
91
|
+
url = self.url + "/" + self.topic
|
92
|
+
r=ht.post(url,msg.to_xml,response_header)
|
91
93
|
puts "url was: #{url}"
|
92
94
|
puts "result: #{r.to_s}"
|
93
95
|
r
|
@@ -96,6 +98,7 @@ class StompSendTopic
|
|
96
98
|
# m=StompMessage::Message.new('stomp_BILLING', msg)
|
97
99
|
open_connection
|
98
100
|
more_headers= {'persistent'=>'false' }
|
101
|
+
# i think bug in this merge..needs to return result
|
99
102
|
more_headers.merge headers
|
100
103
|
self.conn.send(self.topic, msg.to_xml, more_headers, &r_block)
|
101
104
|
# Thread.pass
|
@@ -21,6 +21,7 @@ module StompMessage
|
|
21
21
|
self.msg_count = self.exception_count=0
|
22
22
|
self.queue= Queue.new
|
23
23
|
self.conn=nil
|
24
|
+
self.mythreads = []
|
24
25
|
connect_connection
|
25
26
|
# self.conn = Stomp::Client.new self.login, self.password, self.host, self.port, false
|
26
27
|
# self.conn = Stomp::Connection.open self.login, self.password, self.host , self.port, false
|
@@ -34,6 +35,14 @@ module StompMessage
|
|
34
35
|
#setup_auto_close already done in INIT
|
35
36
|
exit(0)}
|
36
37
|
end
|
38
|
+
def reconnect
|
39
|
+
puts "about to reconnect"
|
40
|
+
self.conn.close if self.conn!=nil
|
41
|
+
connect_connection
|
42
|
+
# self.conn = Stomp::Client.new self.login, self.password, self.host, self.port, false
|
43
|
+
# self.conn = Stomp::Connection.open self.login, self.password, self.host , self.port, false
|
44
|
+
connect_topic
|
45
|
+
end
|
37
46
|
def connect_connection
|
38
47
|
self.conn = StompMessage::StompSendTopic.open_connection(self.conn, self.login, self.password, self.host, self.port)
|
39
48
|
end
|
@@ -91,6 +100,12 @@ module StompMessage
|
|
91
100
|
reply_msg = StompMessage::Message.new('stomp_REPLY', body)
|
92
101
|
send_reply(stomp_msg.headers,reply_msg) if stomp_msg.headers['reply-to']!=nil
|
93
102
|
end
|
103
|
+
def stomp_RECONNECT(msg,stomp_msg)
|
104
|
+
reconnect
|
105
|
+
body="ALIVE Class: #{self.class.to_s} listing to: #{self.topic} on host #{@my_hostname} msg_count #{self.msg_count} exception_count #{self.exception_count} connection status #{self.conn.open?}"
|
106
|
+
reply_msg = StompMessage::Message.new('stomp_REPLY', body)
|
107
|
+
send_reply(stomp_msg.headers,reply_msg) if stomp_msg.headers['reply-to']!=nil
|
108
|
+
end
|
94
109
|
def stomp_DEBUG(msg,stomp_msg)
|
95
110
|
@debug=!@debug
|
96
111
|
puts "debug flag is now #{@debug}"
|
@@ -103,18 +118,35 @@ module StompMessage
|
|
103
118
|
def monitor_queue_status
|
104
119
|
puts "starting up queue status"
|
105
120
|
self.mythreads << Thread.new { while true
|
106
|
-
|
121
|
+
begin
|
122
|
+
sleep(1000)
|
107
123
|
puts "QUEUE size is: #{self.queue.size}"
|
108
124
|
# puts "-------conn var is on #{self.conn.inspect}"
|
109
125
|
if !self.conn.open?
|
110
126
|
puts "restarting connection"
|
111
|
-
self.
|
112
|
-
connect_connection
|
113
|
-
connect_topic
|
127
|
+
self.reconnect
|
114
128
|
end
|
129
|
+
rescue Exception => e
|
130
|
+
handle_exception(e)
|
131
|
+
end
|
115
132
|
end # while
|
116
133
|
}
|
117
134
|
end
|
135
|
+
def handle_exception(e)
|
136
|
+
puts " Thread: #{Thread.current[:name]} :exception found #{e.backtrace}"
|
137
|
+
puts "Thread: #{Thread.current[:name]} :exception messag #{e.message}"
|
138
|
+
result = "-----------EXCEPTION FOUND----------\n"
|
139
|
+
result << "ALIVE Class: #{self.class.to_s} listing to: #{self.topic} on host #{@my_hostname} msg_count #{self.msg_count} exception_count #{self.exception_count}\n"
|
140
|
+
result << "-----exception data--------\n"
|
141
|
+
result << " Thread: #{Thread.current[:name]} :exception found #{e.backtrace}\n"
|
142
|
+
result << "Thread: #{Thread.current[:name]} :exception messag #{e.message}"
|
143
|
+
begin
|
144
|
+
StompMessage::StompSendTopic.send_email_stomp("scott.sproule@cure.com.ph",
|
145
|
+
"STOMP EXCEPTION", "scott.sproule@cure.com.ph","Thread: #{Thread.current[:name]} :exception messag #{e.message}", result)
|
146
|
+
rescue Exception => e
|
147
|
+
puts "Can not send email, please check smtp host setting"
|
148
|
+
end
|
149
|
+
end
|
118
150
|
def handle_message(msg)
|
119
151
|
puts "STOMP message frame is : #{msg} " if @debug
|
120
152
|
m=StompMessage::Message.load_xml(msg.body)
|
@@ -123,11 +155,18 @@ module StompMessage
|
|
123
155
|
send(m.command, m, msg) # effectively case statement (Should SCREEN HERE)
|
124
156
|
# puts "sms text is: #{sms.text} dest is: #{sms.destination} result is: #{res}"
|
125
157
|
end
|
158
|
+
#define thread specific variables here
|
159
|
+
# eg Thread.currect[:smsc]=blah blah
|
160
|
+
def setup_thread_specific_items(mythread_number)
|
161
|
+
|
162
|
+
Thread.current[:name]= "Thread: #{mythread_number}"
|
163
|
+
puts " #{Thread.current[:name]} in setup thread specific data"
|
164
|
+
end
|
126
165
|
def run
|
127
|
-
self.mythreads = []
|
128
166
|
1.upto(self.thread_count) { |c| # create the threads here
|
129
|
-
puts "creating thread: #{c}"
|
167
|
+
#puts "creating thread: #{c}"
|
130
168
|
self.mythreads << Thread.new(c) { |ctmp|
|
169
|
+
setup_thread_specific_items(ctmp)
|
131
170
|
while true
|
132
171
|
|
133
172
|
begin
|
@@ -136,19 +175,7 @@ module StompMessage
|
|
136
175
|
|
137
176
|
rescue Exception => e
|
138
177
|
self.exception_count+=1
|
139
|
-
|
140
|
-
puts "Thread: #{ctmp} :exception messag #{e.message}"
|
141
|
-
result = "-----------EXCEPTION FOUND----------\n"
|
142
|
-
result << "ALIVE Class: #{self.class.to_s} listing to: #{self.topic} on host #{@my_hostname} msg_count #{self.msg_count} exception_count #{self.exception_count}\n"
|
143
|
-
result << "-----exception data--------\n"
|
144
|
-
result << " Thread: #{ctmp} :exception found #{e.backtrace}\n"
|
145
|
-
result << "Thread: #{ctmp} :exception messag #{e.message}"
|
146
|
-
begin
|
147
|
-
StompMessage::StompSendTopic.send_email_stomp("scott.sproule@cure.com.ph",
|
148
|
-
"STOMP EXCEPTION", "scott.sproule@cure.com.ph","Thread: #{ctmp} :exception messag #{e.message}", result)
|
149
|
-
rescue Exception => e
|
150
|
-
puts "Can not send email, please check smtp host setting"
|
151
|
-
end
|
178
|
+
handle_exception(e)
|
152
179
|
end
|
153
180
|
# Thread.pass
|
154
181
|
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
require 'erb'
|
3
|
+
|
4
|
+
#require 'stomp_server'
|
5
|
+
module StompMessage
|
6
|
+
# statistics listening class. Updates the statistics at every message/event
|
7
|
+
# had to name it Z due to order problems. not sure why.
|
8
|
+
class StompZActiveRecordServer < StompMessage::StompServer
|
9
|
+
attr_accessor :model_list
|
10
|
+
def initialize(options={})
|
11
|
+
super(options)
|
12
|
+
puts "root_path: #{options[:root_path]} rails_environment #{options[:env]}"
|
13
|
+
setup_active_record(options[:root_path], options[:env])
|
14
|
+
puts "#{self.class}: finished Active Record initializing"
|
15
|
+
# self.model_list = []
|
16
|
+
end
|
17
|
+
#check the active record connection and reestablish... wierd stuff happening at cure.
|
18
|
+
#note this is specific tomysql... need to fix later
|
19
|
+
def check_active_record_connection(my_class)
|
20
|
+
begin
|
21
|
+
conn=my_class.new.connection
|
22
|
+
puts " checking AR connection status: #{conn.active?}"
|
23
|
+
puts " AR down... reconnecting" if !conn.active?
|
24
|
+
# puts "after first check"
|
25
|
+
conn.reconnect! if !conn.active?
|
26
|
+
# puts "after reconnect"
|
27
|
+
rescue Mysql::Error
|
28
|
+
puts "reconnecting due to Mysql:Error"
|
29
|
+
conn.reconnect
|
30
|
+
rescue Exception => e
|
31
|
+
puts "ActiveRecord found exception that should not be here #{e.message}"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
def monitor_ar_status(connection)
|
35
|
+
puts "starting up active record monitor status"
|
36
|
+
self.mythreads << Thread.new { while true
|
37
|
+
begin
|
38
|
+
sleep(1000)
|
39
|
+
check_active_record_connection(connection)
|
40
|
+
rescue Exception => e
|
41
|
+
handle_exception(e)
|
42
|
+
end
|
43
|
+
end # while
|
44
|
+
}
|
45
|
+
end
|
46
|
+
# the model path needs to include the models for active record... can be inferred from
|
47
|
+
# table names if needed but easiest and simplest is to give path to to the rails application
|
48
|
+
def setup_active_record(root_path,env)
|
49
|
+
require 'rubygems'
|
50
|
+
gem 'activerecord'
|
51
|
+
require 'active_record'
|
52
|
+
path= root_path + "config/database.yml"
|
53
|
+
puts "path is #{path}"
|
54
|
+
data=File.open(path).readlines.join
|
55
|
+
result=ERB.new(data).result
|
56
|
+
parsed=YAML.load(result)
|
57
|
+
puts "env is #{env} values are: #{parsed[env]}"
|
58
|
+
ActiveRecord::Base.establish_connection(parsed[env])
|
59
|
+
ActiveRecord::Base.logger = Logger.new(STDOUT)
|
60
|
+
# grab all the models
|
61
|
+
# model_path = root_path + "app/models/*.rb"
|
62
|
+
load_models(root_path)
|
63
|
+
#puts "after DIR"
|
64
|
+
end
|
65
|
+
#load the AR models... override if necessary
|
66
|
+
# model list is an array of models to load
|
67
|
+
def load_models(root_path)
|
68
|
+
model_path = root_path + "app/models/"
|
69
|
+
# puts "model path is #{model_path}"
|
70
|
+
last_model=""
|
71
|
+
self.model_list.each { |model_file|
|
72
|
+
lib = model_path + model_file
|
73
|
+
last_model = require lib
|
74
|
+
puts "loading required model: is #{lib}"
|
75
|
+
# last_model=lib #last one needs to be active_record (need to fix)
|
76
|
+
}
|
77
|
+
puts "last model is #{last_model}"
|
78
|
+
monitor_ar_status(eval("#{last_model[0]}"))
|
79
|
+
end
|
80
|
+
end # class
|
81
|
+
end #module
|
data/test/test_stomp_message.rb
CHANGED
@@ -7,6 +7,8 @@ require File.dirname(__FILE__) + '/test_helper.rb'
|
|
7
7
|
require 'rubygems'
|
8
8
|
gem 'openwferu'
|
9
9
|
require 'openwfe/workitem'
|
10
|
+
gem 'stomp_message'
|
11
|
+
require 'stomp_message'
|
10
12
|
class TestStompMessage < Test::Unit::TestCase
|
11
13
|
|
12
14
|
def setup
|
@@ -25,6 +27,16 @@ class TestStompMessage < Test::Unit::TestCase
|
|
25
27
|
m2=StompMessage::Message.new("command",false)
|
26
28
|
assert m1.to_xml==m2.to_xml, "xml generation wrong"
|
27
29
|
end
|
30
|
+
def test_xml_helper_load_iv
|
31
|
+
m1=StompMessage::Message.new("command",true)
|
32
|
+
m2=StompMessage::Message.new("command",true)
|
33
|
+
assert m1.to_xml==m2.to_xml, "xml generation wrong"
|
34
|
+
m3=StompMessage::Message.load_xml_new(m2.to_xml)
|
35
|
+
puts "M3 is #{m3.to_xml}"
|
36
|
+
assert m3.class=StompMessage::Message, 'm3 is wrong'
|
37
|
+
assert m3.to_xml==m2.to_xml, "xml generation wrong"
|
38
|
+
assert m3.to_xml==m1.to_xml, "xml generation wrong"
|
39
|
+
end
|
28
40
|
def test_object_nil
|
29
41
|
begin
|
30
42
|
m1=StompMessage::Message.new(nil)
|
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.4
|
|
3
3
|
specification_version: 1
|
4
4
|
name: stomp_message
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.1.
|
7
|
-
date: 2007-
|
6
|
+
version: 0.1.8
|
7
|
+
date: 2007-11-05 00:00:00 +08:00
|
8
8
|
summary: handling stomp messages and servers
|
9
9
|
require_paths:
|
10
10
|
- lib
|
@@ -40,6 +40,7 @@ files:
|
|
40
40
|
- lib/stomp_message/version.rb
|
41
41
|
- lib/stomp_message/stomp_server.rb
|
42
42
|
- lib/stomp_message/stomp_statistics_server.rb
|
43
|
+
- lib/stomp_message/stomp_z_active_record_server.rb
|
43
44
|
- lib/stomp_message/stomp_send_topic.rb
|
44
45
|
- lib/stomp_message/stomp_participant.rb
|
45
46
|
- lib/stomp_message/message.rb
|