welcome to section three working with
akka basic tools in the previous section
you learned the basic actor concepts in
akka in this section we will take a look
at the basic tools in occur such as
actor f versus actor path versus actor
selection routing replace actor behavior
via become unbecoming
via FSM let's start our first video
actor ref versus actor path versus actor
selection in this video we will discuss
what's the difference between actor ref
actor path and actor selection in the
previous section when we discussed actor
components we said that the actor ref is
the interface of the actor instance
actor F is a reference for a single
actor it is created when an actor
instance is created and is pointed to
this instance when the actor is starts
the instance still points to the same
actor ref but when the actor shuts down
the actor reference will point to a
dead-letter and when you create a new
instance
acha will create a new actor reference
this means actor references don't change
during the actor life cycle we know
actors are created in a strictly
hierarchical fashion if we are thinking
about an actor's tree like a file system
and want to get a path for actor B it's
a forward slash user /a /b this is a
path for an actor actor path is a
location of the actor on the actor
system tree it's a sequence of actor
names given by recursively following the
supervision links between a child and
parent down towards the roots of the
actor system the difference between an
actor path and actor ref is that the
actor path represents a name of the
actor which means you can create the
actor terminate it and then create a new
actor with the same path the newly
created actor is a new instance of the
actor which means it has a different
actor reference actor selection is
another representation of the actor like
actor reference
you can send messages to an actor via
actor selection like actor F the main
difference between them is that the
actor selection is created from the
actor path or actor name this means that
the actor selection stays valid even
when an actor dies and another instance
is created so you can't watch the actor
selection like actor reference because
actor selection doesn't represent a
single instance it represents a path now
let's play with an actor reference and
actor selection we have a counter actor
that handles two messages increment and
decrement and has a count variable now
let's create an instance from a counter
then let's print an act of reference for
our instance after that let's create an
actor selection we will use the actor
selection method that takes a path or
name we will give it our actor name and
print the actor selection for our actor
then let's kill our counter instance and
create a new one and a new actor
selection we'll print a new actor
reference and actor selection let's run
yeah it's not the same actor reference
but it's the same actor selection we
said we can't watch the actor selection
but what if we don't have an actor
reference and we know just a name for
the actor how can we watch it it's
simple by default every actor can handle
a message called identify and reply to
this message with the actor reference so
you can easily convert active selection
to act a reference and avoid using actor
selection so let's create a watch actor
that will have a selection value for the
counter actor we will send an identify
message for this selection an identify
message contains the message ID that
will return a response on the receive
method we will handle the response of
identify
acta identify contains a correlated ID
which is the message ID and an optional
value for actor ref when the actor is
live it will have some value for actor F
and when the actor does not exist the
value will be none
you
now let's create two instance forms
counter and watcher let's run
great that's the act of reference for
the current running counter instance to
summarize we discussed the difference
between actor reference actor path and
actor selection