A concurrent program in the Reactors framework is composed of multiple reactors,
which execute concurrently, and in isolation. The only way they can communicate is
by exchanging events using entities called *channels*.
A Reactor[T] object accepts events of type T on its input channel.
One reactor can propagate events concurrently to other reactors.
Event streams cannot be shared between reactors --
it is an error to use an event stream originating in one reactor
in some different reactor.
Reactors are defined by extending the Reactor trait.
The events passed to reactors can be subscribed to using
their main.events stream.
Here is an example:
class MyPrinter extends Reactor[String] {
main.events onEvent {
e => println(e)
}
}
Separate reactor instances that exist at runtime are created using reactor systems.
Each reactor belongs to a specific reactor system. Usually there is a single reactor
system within a single program instance.
Here is an example:
val reactorSystem = ReactorSystem.default("MyReactorSystem")
val channel = reactorSystem.spawn(Proto[MyPrinter])
Creating a reactor returns its channel.
Events can be sent to a channel using the ! method:
channel ! "Hi!"// eventually, this is printed by `MyPrinter`
To stop, a reactor must seal its main channel.
The following reactor seals its main channel after receiving the first event:
class MyPrinter extends Reactor[String] {
main.events onEvent {
e =>
println(e)
main.seal()
}
}
Reactors also receive special SysEvent events on the internal.events stream.
A reactor is a basic unit of concurrency.
A concurrent program in the Reactors framework is composed of multiple reactors, which execute concurrently, and in isolation. The only way they can communicate is by exchanging events using entities called *channels*.
A
Reactor[T]
object accepts events of typeT
on its input channel. One reactor can propagate events concurrently to other reactors. Event streams cannot be shared between reactors -- it is an error to use an event stream originating in one reactor in some different reactor.Reactors are defined by extending the
Reactor
trait. The events passed to reactors can be subscribed to using theirmain.events
stream. Here is an example:Separate reactor instances that exist at runtime are created using reactor systems. Each reactor belongs to a specific reactor system. Usually there is a single reactor system within a single program instance. Here is an example:
Creating a reactor returns its channel. Events can be sent to a channel using the
!
method:To stop, a reactor must seal its main channel. The following reactor seals its main channel after receiving the first event:
Reactors also receive special
SysEvent
events on theinternal.events
stream.the type of events, which
this
reactor produces