This page is no longer maintained — Please continue to the home page at www.scala-lang.org

"cronjobs" and Actors

4 replies
tcn
Joined: 2010-04-02,
User offline. Last seen 2 years 18 weeks ago.

Hi!

I don't understand the following behaviour:

object Test {
val actor = new MyActor // extends Actor, using loop{ react {

def main(args:Array[String]){
actor.start
new Thread(new Runnable {
def run {
while(true){
println("1")
actor ! "hello world"
println("2")
Thread.sleep(5000)
}
}
}).start
}

The output is

1
2
1
2

Two threads are running. The same applies when using a TimerTask.

Why is that? And how do I properly implement a cronjob that's signalling an
actor?

thx,tcn

James Iry
Joined: 2008-08-19,
User offline. Last seen 1 year 23 weeks ago.
Re: "cronjobs" and Actors
Works for me under 2.8rc5
If I define MyActor import scala.actors._
class MyActor extends Actor {   def act = loop{ react {case s : String => println(s)}} }
Then I get
12hello world<sleep>12hello world<sleep>
etc


On Wed, Jun 16, 2010 at 8:53 AM, Timo Nentwig <scala@nentwig.biz> wrote:
Hi!

I don't understand the following behaviour:

object Test {
 val actor = new MyActor // extends Actor, using loop{ react {

 def main(args:Array[String]){
   actor.start
   new Thread(new Runnable {
         def run {
           while(true){
             println("1")
             actor ! "hello world"
             println("2")
             Thread.sleep(5000)
           }
         }
   }).start
}

The output is

1
2
1
2
<sleep>

Two threads are running. The same applies when using a TimerTask.

Why is that? And how do I properly implement a cronjob that's signalling an actor?

thx,tcn


tcn
Joined: 2010-04-02,
User offline. Last seen 2 years 18 weeks ago.
Re: "cronjobs" and Actors

On Wed, 16 Jun 2010, James Iry wrote:

> Date: Wed, 16 Jun 2010 09:34:38 -0700
> From: James Iry
> To: Timo Nentwig
> Cc: scala-user@listes.epfl.ch
> Subject: Re: [scala-user] "cronjobs" and Actors
>
> Works for me under 2.8rc5

Err, yeah, I should have tried my example, sorry.

So, I obviously didn't extract the issue correctly from the actual code :S I'll
try tomorrow again more thoroughly. Sorry and thanks for your effort.

> If I define MyActor
> import scala.actors._
>
> class MyActor extends Actor {
> def act = loop{ react {case s : String => println(s)}}
> }
>
> Then I get
>
> 1
> 2
> hello world
>
> 1
> 2
> hello world
>
>
> etc
>
>
>
> On Wed, Jun 16, 2010 at 8:53 AM, Timo Nentwig wrote:
>
>> Hi!
>>
>> I don't understand the following behaviour:
>>
>> object Test {
>> val actor = new MyActor // extends Actor, using loop{ react {
>>
>> def main(args:Array[String]){
>> actor.start
>> new Thread(new Runnable {
>> def run {
>> while(true){
>> println("1")
>> actor ! "hello world"
>> println("2")
>> Thread.sleep(5000)
>> }
>> }
>> }).start
>> }
>>
>> The output is
>>
>> 1
>> 2
>> 1
>> 2
>>
>>
>> Two threads are running. The same applies when using a TimerTask.
>>
>> Why is that? And how do I properly implement a cronjob that's signalling an
>> actor?
>>
>> thx,tcn
>>
>>
>

Jonas Bonér
Joined: 2008-12-19,
User offline. Last seen 42 years 45 weeks ago.
Re: "cronjobs" and Actors
Here is a Scheduler for Actors (Akka). Cron-style: 
http://github.com/jboner/akka/blob/master/akka-core/src/main/scala/actor/Scheduler.scala
Use like this: 
Scheduler.schedule(receiverActor, messageToBeSent, initialDelayBeforeSending, delayBetweenMessages, timeUnit)

On 16 June 2010 17:53, Timo Nentwig <scala@nentwig.biz> wrote:
Hi!

I don't understand the following behaviour:

object Test {
 val actor = new MyActor // extends Actor, using loop{ react {

 def main(args:Array[String]){
   actor.start
   new Thread(new Runnable {
         def run {
           while(true){
             println("1")
             actor ! "hello world"
             println("2")
             Thread.sleep(5000)
           }
         }
   }).start
}

The output is

1
2
1
2
<sleep>

Two threads are running. The same applies when using a TimerTask.

Why is that? And how do I properly implement a cronjob that's signalling an actor?

thx,tcn




--
Jonas Bonér

work:   http://jayway.com
code:   http://akkasource.com
blog:    http://jonasboner.com
twitter: @jboner




Philipp Haller
Joined: 2009-01-13,
User offline. Last seen 42 years 45 weeks ago.
Re: "cronjobs" and Actors

Hi,

On 06/16/2010 05:53 PM, Timo Nentwig wrote:
> Why is that? And how do I properly implement a cronjob that's signalling an
> actor?

I would try to implement the waiting logic using

reactWithin(timeout) {
case TIMEOUT => ...
}

This uses `TimerTask`s internally, and can be used inside a lightweight
`ReplyReactor` (on Scala 2.8).

Cheers,
Philipp

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland