Advertisement
Guest User

Untitled

a guest
Jan 13th, 2016
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. // SET UP CONNECTION AND POLLER
  2. val host = config.getString("db.host").get
  3. val port = config.getInt("db.port").get
  4. val dbName = config.getString("db.name").get
  5. val user = config.getString("slick.dbs.default.db.user").get
  6. val password = config.getString("slick.dbs.default.db.password").get
  7.  
  8. // initialize JDBC driver & connection pool
  9. Class.forName("org.postgresql.Driver")
  10. ConnectionPool.singleton(s"jdbc:postgresql://$host:$port/$dbName", user, password)
  11. ConnectionPool.dataSource().asInstanceOf[PoolingDataSource].setAccessToUnderlyingConnectionAllowed(true)
  12.  
  13. val connection = ConnectionPool.borrow()
  14. val db: DB = DB(connection)
  15.  
  16. private val notificationsObservable: Observable[Array[PGNotification]] =
  17. Observable.interval(1 second, 500 millis)
  18. .map{ _ =>
  19. db.readOnly { implicit session =>
  20. val pgConnection = connection.asInstanceOf[DelegatingConnection].getInnermostDelegate.asInstanceOf[PGConnection]
  21. Option(pgConnection.getNotifications).getOrElse(Array[PGNotification]())
  22. }
  23. }
  24. .filter( _.nonEmpty )
  25.  
  26. notificationsObservable.subscribe {
  27. _.foreach { notification =>
  28. log.info(s"Notification received: Pid(${notification.getPID}) Channel(${notification.getName}) Payload(${notification.getParameter})")
  29. }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement