Skip to content

Instantly share code, notes, and snippets.

@taotetek
Created October 4, 2010 02:30
Show Gist options
  • Save taotetek/609179 to your computer and use it in GitHub Desktop.
Save taotetek/609179 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'bundler/setup'
require 'java'
require 'yaml'
$LOAD_PATH << 'vendor/jars/'
require 'postgresql-9.0-801.jdbc3.jar'
# set up our database connection to the example database...
java_import java.sql.DriverManager
DriverManager.register_driver(org.postgresql.Driver.new)
url = "jdbc:postgresql://localhost/listen_notify_poller"
def insert_thread(url)
8 lines: insert_conn = DriverManager.get_connection(url) ------------------------------------------------------
end
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?
test = YAML::load(notification.parameter)
puts test.inspect
end
end
end
end
insert_thread = Thread.new{insert_thread(url)}
listen_thread = Thread.new{listen_thread(url)}
listen_thread.join
insert_thread.join
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment