Skip to content

Instantly share code, notes, and snippets.

@lsevero
Forked from mikeball/core.clj
Created May 5, 2020 16:04
Show Gist options
  • Select an option

  • Save lsevero/f4d5570e2d61a61b441b9513a421a9f7 to your computer and use it in GitHub Desktop.

Select an option

Save lsevero/f4d5570e2d61a61b441b9513a421a9f7 to your computer and use it in GitHub Desktop.

Revisions

  1. @mikeball mikeball revised this gist Oct 21, 2015. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions core.clj
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,9 @@
    ; Postgres listen/notify in Clojure using http://impossibl.github.io/pgjdbc-ng/

    ; in project.clj dependencies
    ; [com.impossibl.pgjdbc-ng/pgjdbc-ng "0.5"]


    (ns pglisten.core
    (:import [com.impossibl.postgres.jdbc PGDataSource]
    [com.impossibl.postgres.api.jdbc PGNotificationListener]))
    @@ -34,5 +38,10 @@



    ; trigger message using psql, should print to console in clojure app.
    ; select pg_notify('mymessages', 'hello...');





  2. @mikeball mikeball created this gist Oct 21, 2015.
    38 changes: 38 additions & 0 deletions core.clj
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    ; Postgres listen/notify in Clojure using http://impossibl.github.io/pgjdbc-ng/

    (ns pglisten.core
    (:import [com.impossibl.postgres.jdbc PGDataSource]
    [com.impossibl.postgres.api.jdbc PGNotificationListener]))


    (def datasource (doto (PGDataSource.)
    (.setHost "localhost") ; todo move into
    (.setPort 5432)
    (.setDatabase "listenpg_db")
    (.setUser "listenpg_user")
    (.setPassword "password")))


    ; create a listener that triggers when a message is received
    (def listener
    (reify PGNotificationListener
    (^void notification [this ^int processId ^String channelName ^String payload]
    (println "msg: " payload) )))


    ; setup a connection with the listener
    (def connection
    (doto (.getConnection datasource)
    (.addNotificationListener listener)))


    ; begin listening to a channel
    (doto (.createStatement connection)
    (.execute "LISTEN mymessages;")
    (.close))