- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
"cronjobs" and Actors
Wed, 2010-06-16, 16:52
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
Wed, 2010-06-16, 20:37
#2
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
>>
>>
>
Wed, 2010-06-16, 21:17
#3
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:
--
Jonas Bonér
work: http://jayway.com
code: http://akkasource.com
blog: http://jonasboner.com
twitter: @jboner
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
Wed, 2010-06-16, 22:27
#4
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
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: