Class: StompMessage::StompSendTopic

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

Overview

this class manages sending and receiving messages. It uses passed code block to execute received code

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ StompSendTopic

need to define topic, host properly



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/stomp_message/stomp_send_topic.rb', line 16

def initialize(options={})
 # set up variables using hash
  @close_ok=false
   init_vars(options)
#  puts "host is: #{host} port is #{port}"
# using url as flag wrong
#  self.conn = Stomp::Client.new(self.login, self.password, self.host, self.port, false) if self.url ==nil

  puts "#{self.class}: Initialized host is: #{self.host} port is #{self.port} topic is #{self.topic} login #{self.} pass: #{self.password}"
  # scott old   self.conn.subscribe( self.topic, { :ack =>"auto" })  { |m| 
 #    self.conn.subscribe( self.topic, { :ack =>"client" })  { |m| 
#                                 # puts "#{self.class} msg: #{m.to_s}"
#                                   }
 # setup_auto_close
end

Instance Attribute Details

#connObject

Returns the value of attribute conn.



13
14
15
# File 'lib/stomp_message/stomp_send_topic.rb', line 13

def conn
  @conn
end

#hostObject

Returns the value of attribute host.



13
14
15
# File 'lib/stomp_message/stomp_send_topic.rb', line 13

def host
  @host
end

#loginObject

Returns the value of attribute login.



13
14
15
# File 'lib/stomp_message/stomp_send_topic.rb', line 13

def 
  @login
end

#passwordObject

Returns the value of attribute password.



13
14
15
# File 'lib/stomp_message/stomp_send_topic.rb', line 13

def password
  @password
end

#portObject

Returns the value of attribute port.



13
14
15
# File 'lib/stomp_message/stomp_send_topic.rb', line 13

def port
  @port
end

#topicObject

Returns the value of attribute topic.



13
14
15
# File 'lib/stomp_message/stomp_send_topic.rb', line 13

def topic
  @topic
end

#urlObject

Returns the value of attribute url.



13
14
15
# File 'lib/stomp_message/stomp_send_topic.rb', line 13

def url
  @url
end

Class Method Details

.open_connection(old_conn, login, pass, host, port) ⇒ Object

manage timeout etc…



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/stomp_message/stomp_send_topic.rb', line 50

def self.open_connection(old_conn,,pass,host,port)
    conn=old_conn
    flag=false
   begin
    conn.close if conn!=nil
    
    Timeout::timeout(5) {
         conn=nil
         conn = Stomp::Client.new(, pass, host, port, false)
         flag=true
     }
 
     rescue Timeout::Error
      puts "Timeout error: exception retrying flag is: #{flag}"
      retry  if !flag
   #   raise "timeout" 
  
     end         
      conn
end

.send_email(from, to, msg) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/stomp_message/stomp_send_topic.rb', line 156

def  self.send_email(from,to,msg)
      case   Socket.gethostname
      when "svbalance.cure.com.ph"
            smtp_host='mail2.cure.com.ph'
      when "Scotts-Computer.local"
        smtp_host='mail2.cure.com.ph'
      else
        smtp_host='localhost'
      end
      
    	Net::SMTP.start(smtp_host) { |smtp|
    		smtp.send_message(msg, from, to)
  	   }
end

.send_email_stomp(from, from_alias, to, subject, message) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/stomp_message/stomp_send_topic.rb', line 141

def self.send_email_stomp(from, from_alias, to, subject, message)
      #to_csv_array=to.each.join(",")
    	msg = <<EOF__RUBY_END_OF_MESSAGE
From: #{from_alias} <#{from}>
To: #{to}
Subject: #{subject} #{Time.now}
Comand ----------
#{subject}
RESPONSE -----------
#{message}
EOF__RUBY_END_OF_MESSAGE
     recipients=to.split(',')
     recipients.each {|r|  StompMessage::StompSendTopic.send_email(from,r,msg)}
    
end

Instance Method Details

#close_topicObject

close the topic



74
75
76
77
# File 'lib/stomp_message/stomp_send_topic.rb', line 74

def close_topic
    self.conn.unsubscribe(self.topic) if self.conn !=nil  
  #  self.conn=nil 
end

#disconnect_stompObject

disconnect the connection



79
80
81
82
83
# File 'lib/stomp_message/stomp_send_topic.rb', line 79

def disconnect_stomp
      close_topic
       puts "#{self.class}  closing connection"
      self.conn.close() if self.conn !=nil   
end

#init_vars(options) ⇒ Object

setup_auto_close



31
32
33
34
35
36
37
38
39
40
# File 'lib/stomp_message/stomp_send_topic.rb', line 31

def init_vars(options)
   self. =   options[:login]==nil ? '' : options[:login]
    self.url =   options[:url]== nil ? nil : options[:url]
      self.password =   options[:password]==nil ? '' : options[:password]
        self.host =   options[:host]==nil ? 'localhost' : options[:host]
    self.port =   options[:port]==nil ? '61613' : options[:port]
    self.topic = options[:topic]==nil ? '/topic/undefined' : options[:topic]  
    self.conn=nil
    puts "self url is #{self.url}"
end

#open_connectionObject



70
71
72
# File 'lib/stomp_message/stomp_send_topic.rb', line 70

def open_connection
  self.conn = StompMessage::StompSendTopic.open_connection(@conn, self., self.password, self.host, self.port) if self.conn==nil
end

#post_stomp(msg, headers) ⇒ Object

post stomp message to url



85
86
87
88
89
90
91
92
93
94
# File 'lib/stomp_message/stomp_send_topic.rb', line 85

def post_stomp(msg,headers)
       
       response_header = {"Content-type" => "text/xml"}
       response_header.merge headers
       ht =Net::HTTP.start(self.host,self.port)
       r=ht.post(self.url,msg.to_xml,response_header)
       puts "url was: #{url}"
       puts "result: #{r.to_s}"
       r
end

#send_email_stomp(from, from_alias, to, subject, message) ⇒ Object

send_sms



138
139
140
# File 'lib/stomp_message/stomp_send_topic.rb', line 138

def send_email_stomp(from, from_alias, to, subject, message)
  StompMessage::StompSendTopic.send_email_stomp(from,from_alias,to,subject,message)
end

#send_topic(msg, headers, &r_block) ⇒ Object



95
96
97
98
99
100
101
102
# File 'lib/stomp_message/stomp_send_topic.rb', line 95

def send_topic(msg, headers, &r_block)
     #  m=StompMessage::Message.new('stomp_BILLING', msg)
        open_connection
        more_headers= {'persistent'=>'false' }
        more_headers.merge headers
       self.conn.send(self.topic, msg.to_xml, more_headers, &r_block)
     #  Thread.pass
end

#send_topic_acknowledge(msg, headers, timeout = 4) ⇒ Object

be careful with the receipt topic calculations… strange errors onactive mq



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/stomp_message/stomp_send_topic.rb', line 104

def send_topic_acknowledge(msg, headers, timeout=4)
    #m=StompMessage::Message.new('stomp_BILLING', msg)
    open_connection
    s=rand*20   # scott - used to be 1000 but seem to create connections on activemq
    # open new topic to listen to reply...
     # was this but jms seems to blow up  receipt_topic="/topic/receipt/client#{s.to_i}"
    receipt_topic="/topic/rcpt_client#{s.to_i}"
    receipt_flag = false
   # internal_conn =  Stomp::Connection.open '', '', self.host, self.port, false 
    self.conn.subscribe( receipt_topic, { :ack =>"client" })   {|msg|
                  begin
                  Timeout::timeout(timeout) {
                      self.conn.acknowledge(msg,msg.headers)
              	      msg2= msg.body
              	       yield msg2
                   }
                   rescue Exception => e
                    puts "exception #{e.message}"
                 #   raise "timeout" 
                   ensure
                     receipt_flag=true
                     self.conn.unsubscribe receipt_topic 
                   end   
                     }
                    
    
    more_headers= {'persistent'=>'false',  'reply-to' => "#{receipt_topic}" }
    more_headers.merge headers
    self.conn.send(self.topic, msg.to_xml, more_headers )  
    Thread.new {   sleep(timeout)
                    puts "calling unsubscribe on #{receipt_topic}" if !receipt_flag
                    self.conn.unsubscribe receipt_topic if !receipt_flag
                     }       
end

#setup_auto_closeObject

only call this once



42
43
44
45
46
47
48
# File 'lib/stomp_message/stomp_send_topic.rb', line 42

def setup_auto_close
  
   at_exit { puts "#{self.class}: auto close exit block"
         close_topic
         disconnect_stomp } if !@close_ok
    @close_ok=true
end