你是对ActorPublisher
的:) 这是一个使用 PostgreSQL、异步 DB 驱动程序和LISTEN/NOTIFY 机制的简单示例:
演员:
class PostgresListener extends ActorPublisher[String] {
override def receive = {
case _ ⇒
val configuration = URLParser.parse(s"jdbc://postgresql://$host:$port/$db?user=$user&password=$password")
val connection = new PostgreSQLConnection(configuration)
Await.result(connection.connect, 5.seconds)
connection.sendQuery(s"LISTEN $channel")
connection.registerNotifyListener { message ⇒ onNext(message.payload) }
}
}
服务:
def stream: Source[ServerSentEvent, Unit] = {
val dataPublisherRef = Props[PostgresListener]
val dataPublisher = ActorPublisher[String](dataPublisherRef)
dataPublisherRef ! "go"
Source(dataPublisher)
.map(ServerSentEvent(_))
.via(WithHeartbeats(10.second))
}
build.sbt
在libraryDependencies
:
"com.github.mauricio" %% "postgresql-async" % "0.2.18"
Postgres 触发器应该调用select pg_notify('foo', 'payload')
据我所知,Slick 不支持LISTEN
.