Reputation: 73
Hello I am trying to implement sql sentence in Scala AKKA MySql which connects to a MySQL database using Slick.source and I would like to execute the query in a timer with a specific period of time. I would like to obtain information from the database in 10 seconds I have not found any information on how to do it . My question is : is there any option in Slick for implementing a timer with the SQL sentence. Any idea ??
class AKKAMYSQL {
def run(connectionMysql: Config,mqttUrlSub:String,mqttUserSub:String,mqttPasswordSub:String,mqttTopicSub:String): Unit = {
print(" AKKAMYSQL ")
implicit val system = ActorSystem()
implicit val mat = ActorMaterializer()
val configFile = new File("application.conf")
val fileConfig = ConfigFactory.parseFile(configFile)
val config = ConfigFactory.load(fileConfig)
val sinkSettingsInflux = MqttConnectionSettings(
mqttUrlSub,
"",
new MemoryPersistence
).withAuth(mqttUserSub, mqttPasswordSub)
implicit val session = SlickSession.forConfig(connectionMysql)
system.registerOnTermination(session.close())
case class User(plant: String,source: String,value:String,timeStamp:String,tipo: String)
implicit val getUserResult = GetResult(r => User(r.nextString(), r.nextString(),r.nextString(),r.nextString(),r.nextString()))
import session.profile.api._
val done: Future[Done] =
Slick.source(sql"SELECT plantnodes.xid as planta,profilesources.xid as profileSource,profilevalues.value as value,profilevalues.ts as timeStamp,profiletypes.xid as tipo FROM profileheaders INNER JOIN profilevalues ON profilevalues.profileHeaderId = profileheaders.id INNER JOIN plantnodes ON plantnodes.id = profileheaders.plantNodeId INNER JOIN profilesources ON profilesources.id = profileheaders.profileSourceId INNER JOIN profiletypes ON profiletypes.id = profileheaders.profileTypeId WHERE profiletypes.xid = 'RTV#FR#00' OR profiletypes.xid = 'RTV#CAVG#00' OR profiletypes.xid = 'RTV#AP#00' OR profiletypes.xid = 'RTV#RP#00' OR 'RTV#SP#00' OR 'RTV#PF#00' OR 'RTV#VAVG#00' OR 'RTV#SOC#00' OR 'RTV#TH#00' LIMIT 3".as[User])
.via(Flow[User].map(f = u => {
val jsonString =
"""
{
}
val json: JsValue = Json.parse(jsonString)
new MqttMessage(topic =mqttTopicSub,ByteString.apply(Json.stringify(res)))
}))
.log("user")
.runWith(Sink.ignore)
done.onComplete {end => {
if (end.isFailure) {
println("Error " + end.get.toString())
System.exit(1)
}
else
println("Finished")
}
}
}
}
Upvotes: 1
Views: 114
Reputation: 17933
I don't think that slick
has the functionality that you are looking for. However, you can use other akka
constructs to achieve the same goal.
First, construct an Actor
that executes your SQL querying:
object Tick
class TickActor(akkamysql : AKKAMYSQL) extends Actor {
override def receive : Receive = {
case Tick => akkamysql.run()
}
}
Then, use this Actor in conjunction with the ActorSystem's Scheduler
:
val akkamysql : AKKAMYSQL = ???
val actorSystem : ActorSystem = ???
val tickActorRef = actorSystem.actorOf(Props(classOf[TickActor], akkamysql))
import system.dispatcher
val cancellable =
system.scheduler.schedule(0 milliseconds, 10000 milliseconds, tickActor, Tick)
Upvotes: 2