Skip to content

Instantly share code, notes, and snippets.

@taotetek
Created October 3, 2010 15:39
Show Gist options
  • Save taotetek/608675 to your computer and use it in GitHub Desktop.
Save taotetek/608675 to your computer and use it in GitHub Desktop.
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