Scala Actors: Is there a built-in way to stop/ interrupt an actor? - scala

In Java I have a hobby with thread, for example:
Thread thread = new Thread() {
#Override
public void run() {
//use this while loop, I can stop/ interrupt the thread when I want
while (!isInterrupted()) {
//...
}
}
};
thread.start();
and then, when I want to stop/ interrupt my thread:
thread.interrupt();
So my question is: is there a built-in way (a field/ method/ function...) in Scala that I can stop/ interrupt an actor?
This is my background: I'm new to Scala, I'm really learning to code. And I'm finding my best way between actor and thread. From my point of view, I like the new approach - actor.
P.S: Sorry for my English...

As indicated by the other answer, the actor model is specifically designed to restrict all interaction with an Actor to message passing and handling. Scala/Akka accomplish this by forcing you to create an Actor by calling the actorOf method. Since you don't have a reference to the underlying object, you can't call methods on it directly.
So, if you want to have an Actor that can be interrupted, just handle some message that does that.
def receive = {
case 'Interrupt => // handle interruption...
}
and in your client code
a ! 'Interrupt

You can send an Actor a PoisonPill to ask him to terminates (see http://akka.io/docs/akka/1.2/intro/getting-started-first-scala.html)
Please note that that works with akka Actors. Don't know for scala actors.

Related

scala akka how to catch ActorInitializationException when using ask pattern

I'm doing something like this:
...lots of surrounding context...
val actor = context.actorOf(Props(new Actor(...))
(actor ? message) onComplete {
case Done => {
println("done")
Do stuff with the surrounding context of this actor
}
case _ => {
println("got wildcard") // try to handle error message here
}
}
Actor is failing at initialization with
akka.actor.ActorInitializationException (in that actors context)
In the parent actor I am getting **dead letters encountered.**
since actor was never created properly and "message" couldn't be delivered.
In this scenario the "ask?" pattern never returns properly.
I could create a SupervisorStrategy in the parent global level, but I need to understand the context of the actor that generated the exception and it isn't possible (or convenient) handling it at the global level
Is there a way to "catch" the initialization exception in the "ask" context actor ? message?
According to akka philosophy ask should be used only when you interact with the actor from the outside of the actor system.
Since you have a reference to the context seems that you are inside the actor code. So you should try to get rid of ask.
If I'm wrong and you call ask from non-akka code then you shouldn't try to rely on anything that happens inside the actor system. All that you have is an ActorRef with no guaranties what is on the other side of this reference.

Is it possible to use the Akka scheduler inside an actor?

I want to have the possibility to put actors to sleep for a while. The actors should decide themselves how long they are going to sleep. As Thread.sleep() is not a recommended way of doing this I thought of using the scheduler in akka. I therefore defined an actor were another actor can register for being woken up.
class Scheduler extends Actor {
def receive = {
case Sleep(duration) => context.system.scheduler.scheduleOnce(duration) {
sender ! Ring
}
}
}
But the sending actor never receives the Ring message.
So my questions are
Is scheduling with the scheduler recommended inside an actor?
Why is the sending actor never receiving the Ring message?
If this is not possible what is the recommended way of solving the problem?
Let me first answer the title question: yes, it is possible to use the scheduler inside an actor.
case Sleep(duration) =>
context.system.scheduler.scheduleOnce(duration, self, Ring)
Now to the question behind the question
You did not say what you actually want to achieve, so I’m making an educated guess here that you want the actor—which normally does something called “X”—to do something called “Y” for a while, suspending the “X” activity. The full solutions to this would be
class Sleepy extends Actor {
def receive = {
... // cases doing “X”
case Sleep(duration) =>
case object WakeUp
context.system.scheduler.scheduleOnce(duration, self, WakeUp)
context.become({
case WakeUp => context.unbecome()
// drop the rest
}, discardOld = false)
}
}
The same could as well be implemented using the FSM trait and switching between the normal and the sleeping state. And of course you can do whatever you want while sleeping, e.g. mix in the Stash trait in Akka 2.1 and call stash() for all (or some) messages while sleeping, unstashAll() when getting the WakeUp message; or you could do something else altogether. Actors are very flexible.
What actors don’t do
Actors never really sleep, they always handle incoming messages. As shown above, you can define what that means, but the basic principle is that you cannot suspend an actor such that it will not process the messages in its mailbox.
You are closing over "sender" in your closure that's passed to the scheduler. This means that the Ring message is most likely being sent to the wrong actor. You should do this instead:
case Sleep(duration) =>
val s = sender
context.system.scheduler.scheduleOnce(duration) {
s ! Ring
}
}
Roland Kuhn's answer covers the subject, I just wanted to add that there's another common use-case for using the scheduler:
when sending a message to a different actor and waiting for this actor to respond, it's quite common to cap the wait with a timeout.
otherActor ! Request(...)
context.system.scheduler.scheduleOnce(duration, self, WakeUp)
...
case Response(...) => ...
case WakeUp => context stop self

scala actor blocking current thread

I tried to implement a timer based on Scala's actors API with, the current Thread actor (Actor.self) as the timer and an anonymous Actor which does the work that needs to be completed in time.
I have the following Scala program
import scala.actors.Actor.self
import scala.actors.TIMEOUT
object Main {
def main(args: Array[String]): Unit = {
val thiz = self
actor {
// do heavy work here
thiz ! "finish"
}
self.reactWithin(1000) {
case "finish" => println("complete")
case TIMEOUT => println("timeout")
}
}
}
When I run the program, I received
Exception in thread "main" scala.actors.SuspendActorControl
scala.actors.ActorProxy#1d99a4d: caught java.lang.InterruptedException
Please show me the way to overcome the problem.
You have two types of control passing with scala-actors, thread-blocking or thread-suspension. the latter means a control exception is thrown (the thread is made available again to the thread pool manager), and the actor body re-executed when a message comes into its mailbox. See the actors tutorial for more details, esp. section "Make it Thread-less!".
The receiveWithin method uses the thread-blocking, and the reactWithin the suspension. The problem you have here is that there is no outer managing actor that catches the exception. This only works within a "proper" actor, not with the proxy created for the main thread. So if you want to wait on the main thread outside an explicit actor, you need to use thread blocking / receiveWithin:
self.receiveWithin(1000) { ... }

Creating custom Akka Supervisor that shuts down all linked actors

This earlier question suggests that calling 'stop' on a supervisor actor will shut down all linked actors. However, if one creates a 'custom' actor (not through a factory), it seems that this auto-shutdown doesn't happen. For example, see this code from the answer to another question:
class Module1 extends Actor {
self.faultHandler = OneForOneStrategy(List(classOf[Throwable]), 5, 5000)
def receive = {
case Register(actor) =>
self.link(actor)
}
}
If 'stop' was called on the above Module1 actor, the actors linked to it wouldn't shut down. Is there a way to make a custom actor automatically shut down linked actors? Or is this just something that you have to write yourself if you want to create a custom actor?
Thanks!
You can call:
self.linkedActors.values.iterator
and then send a PoisonPill or stop() them.

garbage collecting scala actors

Scenario: I have this code:
class MyActor extends Actor {
def act() {
react {
case Message() => println("hi")
}
}
}
def meth() {
val a = new MyActor
a.start
a ! Message()
}
is the MyActor instance garbage collected? if not, how do i make sure it is? if I create an ad-hoc actor (with the 'actor' method), is that actor GCed?
This thread on the scala-user mailing list is relevant.
There Phillip Haller mentions using a particular scheduler (available in Scala 2.8) to enable termination of an Actor before garbage collection, either on a global or per-actor basis.
Memory leaks with the standard Actor library has lead to other Actor implementations. This was the reason for David Pollak and Jonas Boner's Actor library for Lift that you can read much more about here: http://blog.lostlake.org/index.php?/archives/96-Migrating-from-Scala-Actors-to-Lift-Actors.html
Have you tried adding a finalize method to see whether it is? I think the answer here is that the actors subsystem behaves no different to how you would expect it to: it does not cache any reference to your actor except in a thread-local for the duration of processing.
I would therefore expect that your actor is a candidate for collection (assuming the subsystem correctly clears out the ThreadLocal reference after the actor has processed the message which it does indeed appear to do in the Reaction.run method).