score:1
In Akka Typed's functional API (which this example uses), the typical way to do this would be along these lines:
object TestActor {
final case class Command()
def apply(): Behavior[Command] =
fromMap(mutable.Map.empty)
private def fromMap(mmap: mutable.Map[Int, Map[Int, Int]]): Behavior[Command] =
Behaviors.receive {
case (context, Command()) =>
mmap += (1 -> Map(2 -> 3))
context.log.info(mmap)
Behaviors.same
}
}
Here, the object
is basically serving as just a module and the fields of that object are better thought of as globals.
Note that there is also an object-oriented API for defining typed actors, which in Scala would be:
object TestActor {
final case class Command()
def apply(): Behavior[TestActor.Command] =
Behaviors.setup { context =>
new TestActor(context)
}
}
class TestActor(context: ActorContext[TestActor.Command]) extends AbstractBehavior[TestActor.Command](context) {
val mmap: mutable.Map[Int, Map[Int, Int]] = mutable.Map.empty
def onMessage(msg: TestActor.Command): Behavior[TestActor.Command] = {
mmap += (1 -> Map(2, 3))
context.log.info(mmap)
this
}
}
It's worth noting that in Akka, it's generally better to have var
s of immutable data (i.e. mutable containers holding immutable values) than val
s of mutable data (i.e. immutable containers holding mutable values), as that can expose a channel for something outside the actor (including another actor) to "pull the rug out from under" an actor.
In the functional API, this would typically take a form like:
object TestActor {
final case class Command()
def apply(): Behavior[Command] =
fromMap(Map.empty)
private def fromMap(map: Map[Int, Map[Int, Int]]): Behavior[Command] =
Behaviors.receive {
case (context, Command()) =>
val nextMap = map + (1 -> Map(2 -> 3))
context.log.info(nextMap)
withMap(nextMap)
}
}
And in the OO Scala API:
// TestActor companion object is as before
class TestActor(context: ActorContext[TestActor.Command]) extends AbstractBehavior[TestActor.Command](context) {
var map: Map[Int, Map[Int, Int]] = Map.empty
def onMessage(msg: TestActor.Command): Behavior[TestActor.Command] = {
map = map + (1 -> Map(2 -> 3))
context.log.info(map)
this
}
}
Source: stackoverflow.com
Related Query
- How to store data in Akka Actor Object fields?
- How can I get the name of an Akka actor from within the actor itself?
- Akka - How many instances of an actor should you create?
- How to get Akka actor by name as an ActorRef?
- How to wait for Akka actor system to terminate?
- How to select akka actor with actorSelection?
- How to test Akka Actor functionality by mocking one or more methods in it
- How to test that Akka actor was created in Scala
- How do I test an Akka actor that sends a message to another actor?
- How to create RDD object on cassandra data using pyspark
- How to preserve request contextual data in Akka
- Akka TCP client: How can I send a message over TCP using akka actor
- How do I setup akka Actor fault tolerance?
- How to mock an Akka Actor to Unit Test a class?
- Akka HTTP Websocket, how to identify connections inside of actor
- how can I store a scala actor reference?
- Akka Actor how to only process the latest message
- How to start a Scala akka actor
- How to unit test an Akka actor that sends a message to itself, without using Thread.sleep
- How do I unit test an akka Actor synchronously?
- How to know requested fields in resolver of Object in Sangria GraphQL
- How to traverse JSON object fields using JsPath?
- How to create an Akka Actor given the class name
- How do I get the CPU utilization of an akka actor
- How to get a Promise (or Future) by asking an Akka Actor considering that the result is NOT available as a response from the same message
- Scala/Play Framework: How to modify fields in Form object before validation
- How to visualize Actor message flow with Akka 2.1.2?
- How to implement receive () in Akka Actor
- How to create akka actor with different path?
- How to save streaming data using Akka Persistence
More Query from same tag
- sbt new RuntimeException
- open class or implicit class in java
- sbt update is not resolving the latest artifacts from repository
- cache and persist datasets
- Scala | Property File +HashMap or XML | What is better to use in refrence to performane of Scala program?
- How to change sbt version for project
- Invalidating weak hash maps across multiple servers
- How can I export scala seq to js.Array in case classes
- Functional Queue From Programming In Scala
- Scala "is not a member of" when chaining method calls without periods and parameters
- Spark: Override library method
- Unable to resolve dependencies from test
- Scala type error
- how to compile single file in sbt
- How to write a function that handle exceptions from futures in Scala?
- reversing the boolean return type from a function
- ScalaCheck generator for web URLs
- Scala: Declare val in if condition
- Anonymous methods passing in arguments
- Scala equivalent of Java java.lang.Class<T> Object
- Scala: generic method using implicit evidence doesn't compile
- Scala parametrized generic parameter
- Separating Text File Into Map In Scala
- Creating dataset based on different case classes
- Sending data from NodeJS to Apache Spark via TCP port
- Play Framework compiling causes "method render in class views cannot be applied to given types" error
- Calling a curried function that uses an implicit
- How can I change Scala script's directory?
- Higher Kinded Types in Scala and Haskell
- Puzzled by type mismatch when applying a Scala Function1