Created
October 3, 2010 15:39
-
-
Save taotetek/608675 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def listen_thread(url) | |
listen_conn = DriverManager.get_connection(url) | |
stmt = listen_conn.create_statement | |
stmt.execute("LISTEN watchers") | |
stmt.close | |
while true | |
sleep 1 | |
puts 'polling...' | |
notifications = listen_conn.get_notifications || [] | |
notifications.each do |notification| | |
unless notification.nil? | |
puts "NOTIFICATION ------------" | |
puts "pid: #{notification.pid}" | |
puts "name: #{notification.name}" | |
puts "param: #{notification.parameter}" | |
puts "-------------------------" | |
id = notification.parameter.split(',').last | |
puts "RETRIEVING RECORD FOR NOTIFIED ID: #{id}" | |
stmt = listen_conn.create_statement | |
rs = stmt.execute_query("SELECT * FROM watched_table WHERE id = #{id}") | |
while rs.next | |
puts "id:\t#{rs.get_int(1)}" | |
puts "value:\t#{rs.get_int(2)}" | |
puts "record_time:\t#{rs.get_timestamp(3)}" | |
end | |
puts "-------------------------" | |
stmt.close | |
rs.close | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment