Search in sources :

Example 1 with StatelessActor

use of actor4j.core.actors.StatelessActor in project actor4j-core by relvaner.

the class StatelessActorFeature method test.

@Test(timeout = 10000)
public void test() {
    ActorSystem system = new ActorSystem();
    system.setParallelismFactor(2);
    CountDownLatch testDone = new CountDownLatch(system.getParallelismMin() * system.getParallelismFactor());
    ActorGroup group = new ActorGroupSet();
    system.setAlias(system.addActor(() -> new StatelessActor(group) {

        protected boolean first = true;

        @Override
        public void receive(ActorMessage<?> message) {
            logger().debug(String.format("from thread %s of actor %s%n", Thread.currentThread().getName(), self()));
            if (first) {
                testDone.countDown();
                first = false;
            }
        }
    }, system.getParallelismMin() * system.getParallelismFactor()), "instances");
    Timer timer = new Timer();
    timer.schedule(new TimerTask() {

        @Override
        public void run() {
            system.sendViaAlias(new ActorMessage<Object>(null, 0, system.SYSTEM_ID, null), "instances");
        }
    }, 0, 50);
    system.start();
    try {
        testDone.await();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    timer.cancel();
    system.shutdownWithActors(true);
}
Also used : ActorSystem(actor4j.core.ActorSystem) Timer(java.util.Timer) TimerTask(java.util.TimerTask) ActorGroup(actor4j.core.utils.ActorGroup) StatelessActor(actor4j.core.actors.StatelessActor) ActorGroupSet(actor4j.core.utils.ActorGroupSet) ActorMessage(actor4j.core.messages.ActorMessage) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

ActorSystem (actor4j.core.ActorSystem)1 StatelessActor (actor4j.core.actors.StatelessActor)1 ActorMessage (actor4j.core.messages.ActorMessage)1 ActorGroup (actor4j.core.utils.ActorGroup)1 ActorGroupSet (actor4j.core.utils.ActorGroupSet)1 Timer (java.util.Timer)1 TimerTask (java.util.TimerTask)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Test (org.junit.Test)1