Search in sources :

Example 6 with ActorMessage

use of actor4j.core.messages.ActorMessage in project actor4j-core by relvaner.

the class RestartProtocol method apply.

public void apply(final Exception reason) {
    final List<UUID> waitForChildren = new ArrayList<>(cell.getChildren().size());
    Iterator<UUID> iterator = cell.getChildren().iterator();
    while (iterator.hasNext()) {
        UUID dest = iterator.next();
        cell.watch(dest);
    }
    iterator = cell.getChildren().iterator();
    while (iterator.hasNext()) {
        UUID dest = iterator.next();
        waitForChildren.add(dest);
        cell.getSystem().sendAsDirective(new ActorMessage<>(null, INTERNAL_STOP, cell.getId(), dest));
    }
    if (waitForChildren.isEmpty()) {
        postRestart(reason);
        cell.setActiveDirectiveBehaviour(false);
    } else
        cell.become(new Consumer<ActorMessage<?>>() {

            protected boolean flag_stop;

            @Override
            public void accept(ActorMessage<?> message) {
                if (message.tag == INTERNAL_STOP)
                    flag_stop = true;
                else if (message.tag == INTERNAL_STOP_SUCCESS) {
                    waitForChildren.remove(message.source);
                    if (waitForChildren.isEmpty()) {
                        if (flag_stop)
                            postStop();
                        else {
                            postRestart(reason);
                            cell.unbecome();
                            cell.setActiveDirectiveBehaviour(false);
                        }
                    }
                }
            }
        }, false);
}
Also used : Consumer(java.util.function.Consumer) ArrayList(java.util.ArrayList) ActorMessage(actor4j.core.messages.ActorMessage) UUID(java.util.UUID)

Example 7 with ActorMessage

use of actor4j.core.messages.ActorMessage in project actor4j-core by relvaner.

the class StopProtocol method apply.

public void apply() {
    final List<UUID> waitForChildren = new ArrayList<>(cell.getChildren().size());
    Iterator<UUID> iterator = cell.getChildren().iterator();
    while (iterator.hasNext()) {
        UUID dest = iterator.next();
        cell.watch(dest);
    }
    iterator = cell.getChildren().iterator();
    while (iterator.hasNext()) {
        UUID dest = iterator.next();
        waitForChildren.add(dest);
        cell.getSystem().sendAsDirective(new ActorMessage<>(null, INTERNAL_STOP, cell.getId(), dest));
    }
    if (waitForChildren.isEmpty())
        postStop();
    else
        cell.become(new Consumer<ActorMessage<?>>() {

            @Override
            public void accept(ActorMessage<?> message) {
                if (message.tag == INTERNAL_STOP_SUCCESS) {
                    waitForChildren.remove(message.source);
                    if (waitForChildren.isEmpty())
                        postStop();
                }
            }
        });
}
Also used : Consumer(java.util.function.Consumer) ArrayList(java.util.ArrayList) ActorMessage(actor4j.core.messages.ActorMessage) UUID(java.util.UUID)

Example 8 with ActorMessage

use of actor4j.core.messages.ActorMessage in project actor4j-core by relvaner.

the class PseudoActorFeature method test.

@Test(timeout = 2000)
public void test() {
    CountDownLatch testDone = new CountDownLatch(10);
    ActorSystem system = new ActorSystem();
    PseudoActor main = new PseudoActor(system, false) {

        @Override
        public void receive(ActorMessage<?> message) {
        }
    };
    final int[] postconditions_numbers = new int[] { 341, 351, 451, 318, 292, 481, 240, 478, 382, 502, 158, 401, 438, 353, 165, 344, 6, 9, 18, 31, 77, 90, 45, 63, 190, 1 };
    UUID numberGenerator = system.addActor(new ActorFactory() {

        @Override
        public Actor create() {
            return new Actor("numberGenerator") {

                protected ActorTimer timer;

                protected int counter = 0;

                @Override
                public void preStart() {
                    timer = system.timer().schedule(() -> new ActorMessage<Integer>(postconditions_numbers[counter++], 0, self(), null), main.getId(), 0, 25);
                }

                @Override
                public void receive(ActorMessage<?> message) {
                    logger().debug(String.format("numberGenerator received a message.tag (%d) from main%n", message.tag));
                    testDone.countDown();
                }

                @Override
                public void postStop() {
                    timer.cancel();
                }
            };
        }
    });
    Timer timer = new Timer();
    timer.schedule(new TimerTask() {

        protected int i;

        protected int counter = 0;

        @Override
        public void run() {
            main.runWithRx().take(2).forEach(msg -> {
                assertEquals(postconditions_numbers[counter++], msg.valueAsInt());
                logger().debug("-> main received a message.value (" + msg.valueAsInt() + ") from numberGenerator");
            });
            main.send(new ActorMessage<>(null, i++, main.getId(), numberGenerator));
        }
    }, 0, 50);
    system.start();
    try {
        testDone.await();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    timer.cancel();
    system.shutdownWithActors(true);
}
Also used : ActorSystem(actor4j.core.ActorSystem) ActorSystem(actor4j.core.ActorSystem) Actor(actor4j.core.actors.Actor) Timer(java.util.Timer) Test(org.junit.Test) ActorFactory(actor4j.core.utils.ActorFactory) UUID(java.util.UUID) ActorMessage(actor4j.core.messages.ActorMessage) CountDownLatch(java.util.concurrent.CountDownLatch) ActorLogger.logger(actor4j.core.utils.ActorLogger.logger) ActorTimer(actor4j.core.ActorTimer) TimerTask(java.util.TimerTask) Assert(org.junit.Assert) PseudoActor(actor4j.core.actors.PseudoActor) ActorTimer(actor4j.core.ActorTimer) CountDownLatch(java.util.concurrent.CountDownLatch) PseudoActor(actor4j.core.actors.PseudoActor) Timer(java.util.Timer) ActorTimer(actor4j.core.ActorTimer) TimerTask(java.util.TimerTask) Actor(actor4j.core.actors.Actor) PseudoActor(actor4j.core.actors.PseudoActor) ActorMessage(actor4j.core.messages.ActorMessage) UUID(java.util.UUID) ActorFactory(actor4j.core.utils.ActorFactory) Test(org.junit.Test)

Example 9 with ActorMessage

use of actor4j.core.messages.ActorMessage in project actor4j-core by relvaner.

the class AwaitFeature method test_await.

@Test(timeout = 2000)
public void test_await() {
    CountDownLatch testDone = new CountDownLatch(1);
    AtomicBoolean[] postconditions = new AtomicBoolean[2];
    for (int i = 0; i < postconditions.length; i++) postconditions[i] = new AtomicBoolean(false);
    UUID dest = system.addActor(new ActorFactory() {

        @Override
        public Actor create() {
            return new Actor() {

                protected Consumer<ActorMessage<?>> action = new Consumer<ActorMessage<?>>() {

                    @Override
                    public void accept(ActorMessage<?> t) {
                        postconditions[0].set(true);
                    }
                };

                protected boolean first = true;

                @Override
                public void receive(ActorMessage<?> message) {
                    if (first) {
                        await(1, action);
                        first = false;
                    } else {
                        postconditions[1].set(true);
                        testDone.countDown();
                    }
                }
            };
        }
    });
    system.send(new ActorMessage<Object>(null, 0, system.SYSTEM_ID, dest));
    system.send(new ActorMessage<Object>(null, 1, system.SYSTEM_ID, dest));
    system.send(new ActorMessage<Object>(null, 1, system.SYSTEM_ID, dest));
    system.start();
    try {
        testDone.await();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    system.shutdown(true);
    assertEquals(true, postconditions[0].get());
    assertEquals(true, postconditions[1].get());
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Consumer(java.util.function.Consumer) Actor(actor4j.core.actors.Actor) ActorMessage(actor4j.core.messages.ActorMessage) UUID(java.util.UUID) ActorFactory(actor4j.core.utils.ActorFactory) Test(org.junit.Test)

Example 10 with ActorMessage

use of actor4j.core.messages.ActorMessage in project actor4j-core by relvaner.

the class EmbeddedActorFeature method test_become_unbecome.

@Test(timeout = 2000)
public void test_become_unbecome() {
    CountDownLatch testDone = new CountDownLatch(2);
    AtomicInteger counter = new AtomicInteger(0);
    ActorSystem system = new ActorSystem();
    UUID host = system.addActor(() -> new Actor("host") {

        protected EmbeddedActor client;

        @Override
        public void preStart() {
            client = new EmbeddedActor("host:client", this) {

                @Override
                public boolean receive(ActorMessage<?> message) {
                    boolean result = false;
                    if (message.tag == SWAP) {
                        become(msg -> {
                            logger().debug(String.format("Received String message: %s", msg.valueAsString()));
                            unbecome();
                            if (counter.incrementAndGet() == 1)
                                assertEquals("Hello World!", msg.valueAsString());
                            else
                                assertEquals("Hello World Again!", msg.valueAsString());
                            testDone.countDown();
                            return true;
                        }, false);
                        result = true;
                    }
                    return result;
                }
            };
        }

        @Override
        public void receive(ActorMessage<?> message) {
            if (!client.embedded(message))
                unhandled(message);
        }
    });
    system.send(new ActorMessage<Object>(null, SWAP, system.SYSTEM_ID, host));
    system.send(new ActorMessage<Object>("Hello World!", 0, system.SYSTEM_ID, host));
    system.send(new ActorMessage<Object>(null, SWAP, system.SYSTEM_ID, host));
    system.send(new ActorMessage<Object>("Hello World Again!", 0, system.SYSTEM_ID, host));
    system.start();
    try {
        testDone.await();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    system.shutdownWithActors(true);
}
Also used : ActorSystem(actor4j.core.ActorSystem) CountDownLatch(java.util.concurrent.CountDownLatch) EmbeddedActor(actor4j.core.actors.EmbeddedActor) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) EmbeddedActor(actor4j.core.actors.EmbeddedActor) Actor(actor4j.core.actors.Actor) ActorMessage(actor4j.core.messages.ActorMessage) UUID(java.util.UUID) Test(org.junit.Test)

Aggregations

ActorMessage (actor4j.core.messages.ActorMessage)17 UUID (java.util.UUID)14 CountDownLatch (java.util.concurrent.CountDownLatch)10 Test (org.junit.Test)10 Actor (actor4j.core.actors.Actor)8 Consumer (java.util.function.Consumer)8 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7 ActorFactory (actor4j.core.utils.ActorFactory)6 ActorSystem (actor4j.core.ActorSystem)5 ArrayList (java.util.ArrayList)3 Assert (org.junit.Assert)3 Timer (java.util.Timer)2 TimerTask (java.util.TimerTask)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 ActorTimer (actor4j.core.ActorTimer)1 EmbeddedActor (actor4j.core.actors.EmbeddedActor)1 PersistentActor (actor4j.core.actors.PersistentActor)1 PrimaryActor (actor4j.core.actors.PrimaryActor)1 PseudoActor (actor4j.core.actors.PseudoActor)1 SecondaryActor (actor4j.core.actors.SecondaryActor)1