मैं स्लिक/चालू करें फ्रेमवर्क (स्काला) को PostgreSQL बयान सूचित सुनने का उपयोग कर सकते घटनाओं सूचित?PostgreSQL को सुनें साथ स्लिक
मैं इस के समान कुछ करना चाहता हूँ:
http://bjorngylling.com/2011-04-13/postgres-listen-notify-with-node-js.html
मैं स्लिक/चालू करें फ्रेमवर्क (स्काला) को PostgreSQL बयान सूचित सुनने का उपयोग कर सकते घटनाओं सूचित?PostgreSQL को सुनें साथ स्लिक
मैं इस के समान कुछ करना चाहता हूँ:
http://bjorngylling.com/2011-04-13/postgres-listen-notify-with-node-js.html
मैं स्लिक का समर्थन करता है PostgreSQL's NOTIFY
नहीं लगता है, लेकिन postgresql-async पुस्तकालय है। एक एक अक्का स्ट्रीम Source
बना सकते हैं और एक प्ले endpoint है कि एक ग्राहक के लिए डेटाबेस सूचनाएं धाराओं में यह शामिल करने के बाद का उपयोग कर सकते हैं:
package controllers
import javax.inject.{Inject, Singleton}
import akka.actor._
import akka.stream._
import akka.stream.scaladsl._
import com.github.mauricio.async.db.postgresql.PostgreSQLConnection
import com.github.mauricio.async.db.postgresql.util.URLParser
import com.github.mauricio.async.db.util.ExecutorServiceUtils.CachedExecutionContext
import play.api.Logger
import play.api.http.ContentTypes
import play.api.libs.EventSource
import play.api.mvc._
import scala.concurrent.duration._
import scala.concurrent.Await
@Singleton
class DbNotificationController @Inject()(cc: ControllerComponents,
materializer: Materializer)
extends AbstractController(cc) {
implicit val mat = materializer
val configuration = URLParser.parse("jdbc:postgresql://localhost:5233/my_db?user=dbuser&password=pwd")
val connection = new PostgreSQLConnection(configuration)
Await.result(connection.connect, 5 seconds)
val (actor, dbSource) =
Source.actorRef[String](Int.MaxValue, OverflowStrategy.dropNew)
.toMat(BroadcastHub.sink[String])(Keep.both)
.run()
connection.sendQuery("LISTEN my_channel")
connection.registerNotifyListener { message =>
val msg = message.payload
Logger.debug(s"Sending the payload: $msg")
actor ! msg
}
def index() = Action {
Ok(views.html.scaladbnotification())
}
def streamDb() = Action {
Ok.chunked(dbSource via EventSource.flow).as(ContentTypes.EVENT_STREAM)
}
}
ऊपर नियंत्रक में, जब श्रोता डेटाबेस से एक सूचना प्राप्त होता है, अधिसूचना में पेलोड (String
) लॉग इन किया गया है और एक अभिनेता को भेजा गया है। इस अभिनेता को भेजे गए संदेश Source
फ़ीड करते हैं जिनका उपयोग streamDb
एंडपॉइंट में किया जाता है। ग्राहक को पेलोड संदेश भेजे जाने से पहले, उन्हें Play's EventSource
कक्षा में परिवर्तित कर दिया जाता है।
मैं Play streaming example application, जो आप प्रयोग करने के लिए उपयोग कर सकते हैं से DbNotificationController
अनुकूलित। आप ऐसा करना चाहते हैं, तो जाहिर है आपको लगता है कि परियोजना में DbNotificationController
एकीकृत करने की जरूरत:
build.sbt
को "com.github.mauricio" %% "postgresql-async" % "0.2.21"
जोड़ें।NOTIFY
सहित जरूरत PostgreSQL निर्धारित करें, और अपने विन्यास के अनुसार नियंत्रक में डेटाबेस यूआरएल समायोजित करें।/app/controllers/
में कॉपी और पेस्ट करें।scaladbnotification.scala.html
) app/views/
में:@main {
<h1>Server Sent Event from DB</h1>
<h1 id="db"></h1>
<p>
DB events are pushed from the Server using a Server Sent Event connection.
</p>
<script type="text/javascript" charset="utf-8">
if (!!window.EventSource) {
var stringSource = new EventSource("@routes.DbNotificationController.streamDb()");
stringSource.addEventListener('message', function(e) {
$('#db').html(e.data.replace(/(\d)/g, '<span>$1</span>'))
});
} else {
$("#db").html("Sorry. This browser doesn't seem to support Server sent event. Check <a href='http://html5test.com/compare/feature/communication-eventSource.html'>html5test</a> for browser compatibility.");
}
</script>
}
/conf/routes
फ़ाइल में जोड़ने के निम्नलिखित लाइनों:GET /scala/dbNotification controllers.DbNotificationController.index() GET /scala/dbNotification/liveDb controllers.DbNotificationController.streamDb()
sbt run
साथ अनुप्रयोग प्रारंभ करें और अपने ब्राउज़र में निम्न URL पर जाएँ:
इस मदद करता है? http://www.smartjava.org/content/listen-notifications-postgresql-scala –