I'm trying to implement Map entry listener with Scala.
Idea:
I need to subscribe to the Map from Service.
I need to notify all subscribers of the Map when entry with specific key is added/updated.
I need to have access to the Map from other Services to check entry value.
I couldn't find ready solution for this, so I've tried to implement it with Akka:
class TrackingService(system: ActorSystem) extends LazyLogging {
private val trackingActor = system.actorOf(TrackingActor.props)
private val duration = Duration(15, TimeUnit.SECONDS)
private implicit val timeout = Timeout(duration)
def fireEvent(key: String): Unit = {
TrackingActor ! EventFired(key)
}
def eventHappened(key: String): Future[Boolean] = {
TrackingActor ? DoesEventHappened(key)).mapTo[Boolean]
}
def registerHiddenCardListener(key: String, data: MyData): Unit = {
TrackingActor ! Subscribe(key, data)
}
}
case class EventFired(key: String)
case class EventHappened(key: String)
case class EventHappenedResponse(happened: Boolean)
case class Subscribe(key: String, data: Data)
class TrackingActor extends Actor with LazyLogging {
var eventMap: Map[String, Boolean] = Map.empty[String, Boolean]
var dataMap: Map[String, List[Data]] = Map.empty[String, List[Data]]
def receive: Receive = {
case Subscribe(key, data) =>
val currentData: List[Data] = dataMap.getOrElse(key, Nil)
val newData = data :: currentData
dataMap = dataMap + (key -> newData)
case EventHappened(key) => sender() ! EventHappenedResponse(eventMap.getOrElse(key, false))
case e#EventFired(key) =>
eventMap = eventMap + (key -> true)
for {
dataOpt <- dataMap.get(key)
data <- dataOpt
} {
// do callback with data (e.g. send email)
}
case x => logger.warn(s"Received unknown message: $x")
}
}
object TrackingActor {
def props: Props = Props(classOf[TrackingActor])
}
What I don't like in this solution: I don't like ask pattern, but I need to have access to the entries from non-actor classes. Also, I don't like having 2 maps, but I need to store somewhere data, which should be used for the callback.
Any ideas on how can I improve this?
Here's an idea:
case class Subscribe[A, B](f: (A, B, NotifyingMap[A,B]) => Any)
case class Event[A, B](key: A, value: B, originator: NotifyingMap[A,B])
case class RegisterObserver(actorRef: ActorRef)
/**
* Subscribes to events
*/
class Subscriber[A,B]{
def register(actorSystem: ActorSystem) = {
val actor = actorSystem.actorOf(Props(classOf[Observer[A,B]]))
actor ! Subscribe(handleEvent)
}
def handleEvent(key: A, value: B, notifyingMap: NotifyingMap[A, B]) = {
println(s"Saw key $key with value $value")
}
}
/**
* Observer of events that will call a partial function when
* an event comes in.
*/
class Observer[A, B] extends Actor{
var f: (A,B,NotifyingMap[A,B]) => Any = _
def receive = {
case x: Subscribe[A, B] =>
f = x.f
Notifier() ! RegisterObserver(self)
case e: Event[A,B] =>
f(e.key, e.value, e.originator)
}
}
/**
* Notifier that sends out the event to all registered observers.
*/
class Notifier extends Actor {
var observers = List[ActorRef]()
def receive = {
case x: RegisterObserver =>
observers = x.actorRef :: observers
case x: Event[_,_] =>
observers.foreach(_ ! Event)
}
}
/**
* Singleton notifier.
*/
object Notifier{
var notifier: ActorRef = _
def create(actorSystem: ActorSystem) =
actorSystem.actorOf(Props(classOf[Notifier]))
def apply(): ActorRef = notifier
}
/**
* Class that sends out an event when an item is put. Also allows for
* getting an item based on a key.
*/
class NotifyingMap[A, B](){
val map: TrieMap[A,B] = TrieMap[A,B]()
// Specific business logic here on when you publish the event.
def put(key: A, value: B) = {
map.put(key, value).foreach(v => Notifier() ! Event(key, v, this))
}
def get(key: A) = map.get(key)
}
By doing this you can keep your Subscriber a non-Actor class while still allowing it to react to an event. You can also call plain old methods on your NotifyingMap since it's just a class and not an Actor.
I personally like to store callback information in the messages. Typically you see this by having another ActorRef in a case class. In this example we have the NotifyingMap in the case class so we know where the event originated from and can appropriately call the get method there.
Full disclosure: I didn't run any of this code. It does compile.
Related
I'm trying to implement an application that controls a camera. Camera commands are represented as a stream of CameraAction objects:
sealed trait CameraMessage
case object Record(recordId: String) extends CameraMessage
case object Stop extends CameraMessage
...
val s = Stream[F, CameraMessage]
Let's say I have a test stream that emits "Record" and emits "Stop" 20 seconds later, after another 20 seconds another "Record" message is emitted and so on, the input stream is infinite.
Then the app consumes "Record" it should create an instance of GStreamer pipeline (i.e. it is an effect) and "run" it, on "Stop" it 'stops' the pipeline and closes it. Then on subsequent "Record" the pattern is repeated with new GStreamer pipeline.
The problem is that I need to pass an instance of impure, mutable object between handles of stream events.
FS2 documentation suggest to use chunks to make a stream stateful, so I tried
def record(gStreamerPipeline: String, fileName: String)
(implicit sync: Sync[F]): F[Pipeline] =
{
... create and open pipeline ...
}
def stopRecording(pipe: Pipeline)(implicit sync: Sync[F]): F[Unit] = {
... stop pipeline, release resources ...
}
def effectPipe(pipelineDef: String)(implicit L: Logger[F]):
Pipe[F, CameraMessage, F[Unit]] = {
type CameraSessionHandle = Pipeline
type CameraStream = Stream[F, CameraSessionHandle]
s: Stream[F, CameraMessage] =>
s.scanChunks(Stream[F, CameraSessionHandle]()) {
case (s: CameraStream, c: Chunk[CameraMessage]) =>
c.last match {
case Some(Record(fileName)) =>
(Stream.bracket(record(pipelineDef, fileName))(p => stopRecording(p)), Chunk.empty)
case Some(StopRecording) =>
(Stream.empty, Chunk(s.compile.drain))
case _ =>
(s, Chunk.empty)
}
}
}
The problem with this code that actual recording does not happen on 'Record' event but rather then the effect of the whole chunk is evaluated, i.e. when 'StopRecording' message arrives the camera is turned on and then immediately turned off again.
How can I pass a "state" without chunking? Or is there any other way to achieve the result I need?
This may be similar to
FS2 Stream with StateT[IO, _, _], periodically dumping state
but the difference is that the state in my case is not a pure data structure but a resource.
I eventually was able so solve it using Mutable Reference pattern as described in https://typelevel.org/blog/2018/06/07/shared-state-in-fp.html
Here is the code:
import cats.effect._
import cats.syntax.all._
import fs2.Stream
import scala.concurrent.{ExecutionContext, ExecutionContextExecutor}
import scala.language.higherKinds
class FRef[F[_], T](implicit sync: Sync[F]) {
private var state: T = _
def set(n: T): F[Unit] = sync.delay(this.state = n)
def get: F[T] = sync.pure(state)
}
object FRef {
def apply[F[_], T](implicit sync: Sync[F]): F[FRef[F, T]] = sync.delay { new FRef() }
}
class CameraImpl(id: String) extends Camera {
override def record(): Unit = {
println(s"Recording $id")
}
override def stop(): Unit = {
println(s"Stopping $id")
}
override def free(): Unit = {
Thread.sleep(500)
println(s"Freeing $id")
}
}
object Camera {
def apply(id: String) = new CameraImpl(id)
}
trait Camera {
def record(): Unit
def stop(): Unit
def free(): Unit
}
sealed trait CameraMessage
case class Record(recordId: String) extends CameraMessage
case object StopRecording extends CameraMessage
class Streamer[F[_]](implicit sync: Sync[F]) {
def record(id: String): F[Camera] = {
sync.delay {
val r = Camera(id)
r.record()
r
}
}
def stopRecording(pipe: Camera): F[Unit] = {
sync.delay {
pipe.stop()
pipe.free()
}
}
def effectPipe(state: FRef[F, Option[Camera]])(
implicit sync: Sync[F]): Stream[F, CameraMessage] => Stream[F, Unit] = {
type CameraStream = Stream[F, Camera]
s: Stream[F, CameraMessage] =>
s.evalMap {
case Record(fileName) =>
for {
r <- record(fileName)
_ <- state.set(Some(r))
} yield ()
case StopRecording =>
for {
s <- state.get
_ <- stopRecording(s.get)
_ <- state.set(None)
} yield ()
}
}
}
object FS2Problem extends IOApp {
import scala.concurrent.duration._
override def run(args: List[String]): IO[ExitCode] = {
implicit val ec: ExecutionContextExecutor = ExecutionContext.global
val streamer = new Streamer[IO]
val s = Stream.awakeEvery[IO](5.seconds).take(10).zipWithIndex.map {
case (_, idx) =>
idx % 2 match {
case 0 =>
Record(s"Record $idx")
case _ =>
StopRecording
}
}
val ss = for {
streamerState <- Stream.eval(FRef[IO, Option[Camera]])
s <- s.through(streamer.effectPipe(streamerState))
} yield ()
ss.compile.drain.map(_ => ExitCode.Success)
}
}
I'm trying to setup a basic websocket based notification system using Scala, Play and Akka. The websocket works fine, I'm able to send stuff, echo it back, respond with newly created case classes etc. I've defined a sealed trait with case classes to represent the various inputs and outputs, with custom writes and reads in order to manage input/output JSON objects and add/remove a "type" field.
However, when I try to send data through the websocket via an external action, I get this weird run time error: java.lang.ClassCastException: v1.socket.SocketEvent$ cannot be cast to v1.socket.SocketEvent
Here is the invocation from an action:
WebSocketActor.send(ConnectionRequest("test", "test", "test"), "test")
Here is my WebSocketActor:
object WebSocketActor {
def props(out: ActorRef) = Props(new WebSocketActor(out))
var sessions: Map[String, Map[String, ActorRef]] = Map()
/** Send data to a specified user.
* Response with false if the user does not have a session
*/
def send(socketEvent: SocketEvent, userId: String): Boolean = {
if (sessions contains userId) {
val userSessions = sessions(userId)
userSessions.foreach(user => user._2 ! SocketEvent)
return true
}
false
}
}
class WebSocketActor(out: ActorRef) extends Actor {
/** Authenticate the user and add device to session
* (note that authentication is stripped out for brevity)
*/
def auth: Receive = {
case init: Init => {
var session: Map[String, ActorRef] = Map(init.deviceId -> out)
if (sessions contains init.userId) {
session = session ++ sessions(init.userId)
}
sessions = sessions + (init.userId -> session)
context become await
}
}
def await: Receive = {
case conReq: ConnectionRequest => {
out ! conReq
}
}
override def receive: Receive = auth
}
Here is the SocketEvent seale trait, companion object and case classes:
sealed trait SocketEvent
object SocketEvent {
implicit val socketEventFmt: Format[SocketEvent] = new Format[SocketEvent] {
override def writes(event: SocketEvent): JsValue = event match {
case e: ConnectionRequest => Json.toJson(e)(Json.format[ConnectionRequest])
case e: Init => Json.toJson(e)(Json.format[Init])
}
override def reads(json: JsValue): JsResult[SocketEvent] = (json \ "type").as[String] match {
case "connectionRequest" =>
Json.fromJson[ConnectionRequest](json.as[JsObject] - "type")(Json.format[ConnectionRequest])
case "init" =>
Json.fromJson[Init](json.as[JsObject] - "type")(Json.format[Init])
}
}
}
case class ConnectionRequest(userId: String, publicKey: String, signature: String) extends SocketEvent
object ConnectionRequest {
implicit val format: OWrites[ConnectionRequest] = Json.format[ConnectionRequest]
.withConstant("type", "connectionRequest")
}
case class Init(userId: String, deviceId: String) extends SocketEvent
object Init{
implicit val initFmt: OWrites[Init] = Json.format[Init]
.withConstant("type", "Init")
}
Finally, an OWritesExtra class that is used to add fields to the outputed Json:
object OWritesExtra {
implicit class OWritesExt[A](owrites: OWrites[A]) {
/** Add a (key, value) pair to the JSON object,
* where the value is constant.
*/
def withConstant[B: Writes](key: String, value: B): OWrites[A] =
withValue(key, _ => value)
/** Add a key, value pair to the JSON object that is written, where
* the value depends on the original object.
*/
def withValue[B: Writes](key: String, value: A => B): OWrites[A] =
new OWrites[A] {
def writes(a: A): JsObject = owrites.writes(a) ++ Json.obj(key -> value(a))
}
}
}
The issue was that I was sending the type SocketEvent instead of the instance socketEvent in the send method.
Can I set Logging in Akka actor by implicit? I would like to log inside the actor. My code is as given below:
object EmployeeRouterActor {
final case class Employee(id: Long, name: String)
final case object StopChild
final case class ChildResponse(id: Long, name: String)
}
final class EmployeeRouterActor extends Actor with akka.actor.ActorLogging {
import EmployeeRouterActor._
private var children = Map.empty[Long, ActorRef]
override def receive: Receive = {
case e # Employee(id, _) => {
getChild(id) ! e
}
case r # ChildResponse(id, _) => {
stopChild(id)
val idItem = r.id
val nameItem = r.name
sender ! s"Employee {$idItem} is $nameItem."
}
}
private def getChild(id: Long): ActorRef =
context.child(id.toString).getOrElse {
val child = context.actorOf(EmployeeEchoActor.apply(), id.toString)
children += (id -> child)
child
}
private def stopChild(id: Long) = {
children(id) ! StopChild
children -= id
}
}
But, in this case, I can not do something like this:
class EmployeeRouterActor (implicit logger: LoggingAdapter) { ... }
I have got following errors:
Error:(10, 30) could not find implicit value for parameter logger: akka.event.LoggingAdapter
def apply(): Props = Props(new EmployeeEchoActor)
Error:(10, 30) not enough arguments for constructor EmployeeEchoActor: (implicit logger: akka.event.LoggingAdapter)actors.EmployeeEchoActor.
Unspecified value parameter logger.
def apply(): Props = Props(new EmployeeEchoActor)
Is it possible to use Logging from Akka inside the actors?
EmployeeEchoActor (child actor) is defined as given below:
object EmployeeEchoActor {
def apply(): Props = Props(new EmployeeEchoActor)
}
class EmployeeEchoActor(implicit logger: LoggingAdapter) extends Actor {
override def receive = {
case employee: EmployeeRouterActor.Employee => {
logger.info("Message received!")
val idItem = employee.id
val nameItem = employee.name
context.parent ! EmployeeRouterActor.ChildResponse( idItem, nameItem )
}
case EmployeeRouterActor.StopChild => {
logger.info("Stopping :(!")
context.stop(self)
}
case _ => sender ! "Internal Error!"
}
}
The solution is:
final class EmployeeRouterActor extends Actor with akka.actor.ActorLogging {
Implement ActorLogging and use log at code, for example:
override def receive: Receive = {
case e # Employee(id, _) => {
log.info("Message received!")
getChild(id) ! e
}
case r # ChildResponse(id, _) => {
log.info("Response received from child!")
stopChild(id)
log.info("Child has been stopped!")
val idItem = r.id
val nameItem = r.name
sender ! s"Employee {$idItem} is $nameItem."
}
}
I have Play websockets action:
def socket = WebSocket.acceptWithActor[String, Array[Byte]] { request => out =>
Props(new WebSocketInActor(out))
}
Generally I need to send to browser large raw arrays of data. But sometimes I need to send some small string data. In browser I can detect is data in text format or raw ArrayBuffer.
If I create actor that sends String, I can send string messages, If I create actor that sends with Array[Byte], I can send raw arrays. Both situations I don't need to change client code. So, how can I force Play to use both sending methods with one out actor?
Ah, those answers that comes just after you post question on SO. Looking through reference and sourcecode, I found that there is mixedFrame FrameFromatter: https://github.com/playframework/playframework/blob/2.4.x/framework/src/play/src/main/scala/play/api/mvc/WebSocket.scala#L75
So you just need to say that you will respond with Either[String, Array[Byte]] and if you want to send string use Left(somestring) or else use Right[somearray].
class WebSocketInActor(out: ActorRef) extends Actor {
override def preStart() = {
println("User connected")
val s = "Hello"
out ! Left(s)
out ! Right(s.getBytes("utf8"))
}
override def postStop() = {
println("User discconnected")
}
def receive = {
case msg: String => {
}
case _ =>
}
}
def socket = WebSocket.acceptWithActor[String, Either[String, Array[Byte]]] { request => out =>
Props(new WebSocketInActor(out))
}
UPDATE:
Or you can go one step further and create your own frame formatter
sealed trait WSMessage
case class StringMessage(s: String) extends WSMessage
case class BinaryMessage(a: Array[Byte]) extends WSMessage
case class JsonMessage(js: JsValue) extends WSMessage
implicit object myFrameFormatter extends BasicFrameFormatter[WSMessage] {
private val textFrameClass = classOf[TextFrame]
private val binaryFrameClass = classOf[BinaryFrame]
def toFrame(message: WSMessage): BasicFrame = message match {
case StringMessage(s) => TextFrame(s)
case BinaryMessage(a) => BinaryFrame(a)
case JsonMessage(js) => TextFrame(Json.stringify(js))
}
def fromFrame(frame: BasicFrame): WSMessage = frame match {
case TextFrame(s) => StringMessage(s)
case BinaryFrame(a) => BinaryMessage(a)
}
def fromFrameDefined(clazz: Class[_]): Boolean = clazz match {
case `textFrameClass` => true
case `binaryFrameClass` => true
case _ => false // shouldn't be reachable
}
}
class WebSocketInActor(out: ActorRef) extends Actor {
override def preStart() = {
println("User connected")
val s = "Hello"
val a:Array[Byte] = Array(100, 50, 30).map(_.toByte)
out ! StringMessage(s)
out ! JsonMessage(Json.obj("txt" -> s, "array" -> a))
out ! BinaryMessage(a)
}
override def postStop() = {
println("User discconnected")
}
def receive = {
case msg: String => {
}
case _ =>
}
}
def socket = WebSocket.acceptWithActor[String, WSMessage] { request => out =>
Props(new WebSocketInActor(out))
}
I want to be able to make concurrent requests to multiple data repositories and consolidate the results. I am trying to understand if my approach is at all valid or if there is a better way to approach this problem. I am definitely new to Akka / Spray / Scala and really want to get a better understanding of how to properly build these components. Any suggestions / Tips would be greatly appreciated. Trying to wrap my head around the use of actors and futures for this type of implementation.
Spray Service:
trait DemoService extends HttpService with Actor with ActorLogging {
implicit val timeout = Timeout(5 seconds) // needed for `?` below
val mongoMasterActor = context.actorOf(Props[MongoMasterActor], "redisactor")
val dbMaster = context.actorOf(Props[DbMasterActor], "dbactor")
val messageApiRouting =
path("summary" / Segment / Segment) { (dataset, timeslice) =>
onComplete(getDbResponses(dbMaster, dataset, timeslice)) {
case Success(dbMessageResponse) => complete(s"The result was $dbMessageResponse")
case Failure(ex) => complete(s"An error occurred: ${ex.getMessage}")
}
}
/** Passes the desired actor reference for a specific dataset and timeslice for summary data retrieval
*
* #param mongoActor an actor reference to the RedisActor that will handle the appropriate request routing
* #param dataset The dataset for which the summary has been requested
* #param timeslice The timeslice (Month, Week, Day, etc.) for which the summary has been requested
*/
def getSummary(mongoActor: ActorRef, dataset: String, timeslice: String): Future[DbMessageResponse] = {
log.debug(s"dataset: $dataset timeslice: $timeslice")
val dbMessage = DbMessage("summary", dataset + timeslice)
(mongoActor ? dbMessage).mapTo[DbMessageResponse]
}
def getDbResponses(dbActor: ActorRef, dataset: String, timeslice: String): Future[SummaryResponse] = {
log.debug(s"dataset: $dataset timeslice: $timeslice")
val dbMessage = DbMessage("summary", dataset + timeslice)
(dbActor ? dbMessage).mapTo[SummaryResponse]
}
def getSummaryPayload(mongoSummary: DbMessageResponse, redisSummary: DbMessageResponse): String = {
mongoSummary.response + redisSummary.response
}
}
Akka Actor / Future mock db requests:
class DbMasterActor extends Actor with ActorLogging {
private var originalSender: ActorRef = _
//TODO: Need to add routing to the config to limit instances
val summaryActor = context.actorOf(Props(new SummaryActor), "summaryactor")
def receive = {
case msg: DbMessage => {
this.originalSender = sender
msg.query match {
case "summary" => {
getDbResults().onComplete{
case Success(result) => originalSender ! result
case Failure(ex) => log.error(ex.getMessage)
}
}
}
}
//If not match log an error
case _ => log.error("Received unknown message: {} ")
}
def getDbResults(): Future[SummaryResponse] = {
log.debug("hitting db results")
val mongoResult = Future{ Thread.sleep(500); "Mongo"}
val redisResult = Future{ Thread.sleep(800); "redis"}
for{
mResult <- mongoResult
rResult <- redisResult
} yield SummaryResponse(mResult, rResult)
}
}
Following the reading of Effective Akka by Jamie Allen, I am going to attempt to apply his "Cameo" pattern suggestion.
Slideshare:
http://www.slideshare.net/shinolajla/effective-akka-scalaio
Github:
https://github.com/jamie-allen/effective_akka
I think what I created will work, but doesn't sound like the best approach based on Jamie's comments in his talks. I will update / edit back to this post what I have implemented (or try to).
Summary Actor (Cameo Actor):
object SummaryResponseHandler {
case object DbRetrievalTimeout
def props(mongoDb: ActorRef, redisDb: ActorRef, originalSender: ActorRef): Props = {
Props(new SummaryResponseHandler(mongoDb, redisDb, originalSender))
}
}
class SummaryResponseHandler(mongoDb: ActorRef, redisDb: ActorRef,
originalSender: ActorRef) extends Actor with ActorLogging {
import SummaryResponseHandler._
var mongoSummary, redisSummary: Option[String] = None
def receive = LoggingReceive {
case MongoSummary(summary) =>
log.debug(s"Received mongo summary: $summary")
mongoSummary = summary
collectSummaries
case RedisSummary(summary) =>
log.debug(s"Received redis summary: $summary")
redisSummary = summary
collectSummaries
case DbRetrievalTimeout =>
log.debug("Timeout occurred")
sendResponseAndShutdown(DbRetrievalTimeout)
}
def collectSummaries = (mongoSummary, redisSummary) match {
case (Some(m), Some(r)) =>
log.debug(s"Values received for both databases")
timeoutMessager.cancel
sendResponseAndShutdown(DataSetSummary(mongoSummary, redisSummary))
case _ =>
}
def sendResponseAndShutdown(response: Any) = {
originalSender ! response
log.debug("Stopping context capturing actor")
context.stop(self)
}
import context.dispatcher
val timeoutMessager = context.system.scheduler.scheduleOnce(
250 milliseconds, self, DbRetrievalTimeout)
}
class SummaryRetriever(mongoDb: ActorRef, redisDb: ActorRef) extends Actor with ActorLogging {
def receive = {
case GetSummary(dataSet) =>
log.debug("received dataSet")
val originalSender = sender
val handler = context.actorOf(SummaryResponseHandler.props(mongoDb,redisDb, originalSender), "cameo-message-handler")
mongoDb.tell(GetSummary(dataSet), handler)
redisDb.tell(GetSummary(dataSet), handler)
case _ => log.debug(s"Unknown result $GetSummary(datset)")
}
}
Common:
case class GetSummary(dataSet: String)
case class DataSetSummary(
mongo: Option[String],
redis: Option[String]
)
case class MongoSummary(
summary: Option[String]
)
case class RedisSummary(
summary: Option[String]
)
trait MongoProxy extends Actor with ActorLogging
trait RedisProxy extends Actor with ActorLogging
Mock Stubs:
class MongoProxyStub extends RedisProxy {
val summaryData = Map[String, String](
"dataset1" -> "MongoData1",
"dataset2" -> "MongoData2")
def receive = LoggingReceive {
case GetSummary(dataSet: String) =>
log.debug(s"Received GetSummary for ID: $dataSet")
summaryData.get(dataSet) match {
case Some(data) => sender ! MongoSummary(Some(data))
case None => sender ! MongoSummary(Some(""))
}
}
}
class RedisProxyStub extends MongoProxy{
val summaryData = Map[String, String](
"dataset1" -> "RedisData1",
"dataset2" -> "RedisData2")
def receive = LoggingReceive {
case GetSummary(dataSet: String) =>
log.debug(s"Received GetSummary for ID: $dataSet")
summaryData.get(dataSet) match {
case Some(data) => sender ! RedisSummary(Some(data))
case None => sender ! RedisSummary(Some(""))
}
}
}
Boot (You should use test, but was just wanting to run from boot):
object Boot extends App{
val system = ActorSystem("DbSystem")
val redisProxy = system.actorOf(Props[RedisProxyStub], "cameo-success-mongo")
val mongoProxy = system.actorOf(Props[MongoProxyStub], "cameo-success-redis")
val summaryRetrieverActor = system.actorOf(Props(new SummaryRetriever(redisProxy, mongoProxy)), "cameo-retriever1")
implicit val timeout = Timeout(5 seconds)
val future = summaryRetrieverActor ? GetSummary("dataset1")
val result = Await.result(future, timeout.duration).asInstanceOf[DataSetSummary]
println(Some(result.mongo).x)
println(result.redis)
system.shutdown()
}
Application Config:
akka.loglevel = "DEBUG"
akka.event-handlers = ["akka.event.slf4j.Slf4jEventHandler"]
akka.actor.debug.autoreceive = on
akka.actor.debug.lifecycle = on
akka.actor.debug.receive = on
akka.actor.debug.event-stream = on