Search in sources :

Example 6 with ActorRef

use of co.paralleluniverse.actors.ActorRef in project quasar by puniverse.

the class SupervisorTest method dontRestartTemporaryChildDeadOfNaturalCause.

@Test
public void dontRestartTemporaryChildDeadOfNaturalCause() throws Exception {
    final Supervisor sup = new SupervisorActor(RestartStrategy.ONE_FOR_ONE, new ChildSpec("actor1", ChildMode.TEMPORARY, 5, 1, TimeUnit.SECONDS, 3, ActorSpec.of(Actor1.class, "actor1"))).spawn();
    ActorRef<Object> a;
    a = getChild(sup, "actor1", 1000);
    for (int i = 0; i < 3; i++) a.send(1);
    a.send(new ShutdownMessage(null));
    assertThat(LocalActor.<Integer>get(a), is(3));
    a = getChild(sup, "actor1", 200);
    assertThat(a, nullValue());
    List<ActorRef<Object>> cs = getChildren(sup);
    assertEquals(cs.size(), 0);
    sup.shutdown();
    LocalActor.join(sup);
}
Also used : ShutdownMessage(co.paralleluniverse.actors.ShutdownMessage) ChildSpec(co.paralleluniverse.actors.behaviors.Supervisor.ChildSpec) ActorRef(co.paralleluniverse.actors.ActorRef) Test(org.junit.Test)

Example 7 with ActorRef

use of co.paralleluniverse.actors.ActorRef in project quasar by puniverse.

the class PeerTKB method run.

public void run() throws ExecutionException, InterruptedException {
    switch(SCENARIO.testGenEvent) {
        case test:
            final Store store = Grid.getInstance().store();
            if (i == 1) {
                StoreTransaction tx = store.beginTransaction();
                try {
                    long root = store.getRoot("root", tx);
                    //"hello".getBytes();
                    byte[] buf = null;
                    store.set(root, buf, tx);
                    store.commit(tx);
                } catch (TimeoutException ex) {
                    throw new RuntimeException("set failed");
                }
                Thread.sleep(20000);
            } else {
                StoreTransaction tx = store.beginTransaction();
                byte[] get;
                try {
                    long root = store.getRoot("root", tx);
                    get = store.get(root);
                    store.commit(tx);
                } catch (TimeoutException ex) {
                    throw new RuntimeException("get failed");
                }
                System.out.println(get);
            }
            break;
        case testGenServer:
            if (i == 1) {
                spawnGenServer(new AbstractServerHandler<Message, Integer, Message>() {

                    @Override
                    public void init() throws SuspendExecution {
                        super.init();
                        ServerActor.currentServerActor().register("myServer");
                    }

                    @Override
                    public Integer handleCall(ActorRef<?> from, Object id, Message m) {
                        return m.a + m.b;
                    }
                }).join();
            } else {
                Integer get = spawnActor(new BasicActor<Message, Integer>(new MailboxConfig(10, Channels.OverflowPolicy.THROW)) {

                    protected Integer doRun() throws SuspendExecution, InterruptedException {
                        final Server<Message, Integer, Message> gs = (Server) ActorRegistry.getActor("myServer");
                        return gs.call(new Message(3, 4));
                    }
                }).get();
                System.out.println("value is " + get);
                assert get == 7;
            }
            break;
        case testGenEvent:
            if (i == 1) {
                final Val<String> dv = new Val<>();
                spawnGenEvent(new Initializer() {

                    @Override
                    public void init() throws SuspendExecution {
                        EventSourceActor.currentEventSourceActor().register("myEventServer");
                        try {
                            final EventSource<String> ge = LocalActor.self();
                            ge.addHandler(new EventHandler<String>() {

                                @Override
                                public void handleEvent(String event) {
                                    dv.set(event);
                                    System.out.println("sout " + event);
                                    ge.shutdown();
                                }
                            });
                        } catch (InterruptedException ex) {
                            System.out.println(ex);
                        }
                    }

                    @Override
                    public void terminate(Throwable cause) throws SuspendExecution {
                        System.out.println("terminated");
                    }
                });
                String get = dv.get();
                System.out.println("got msg " + get);
                assert get.equals("hello world");
            } else {
                spawnActor(new BasicActor<Message, Void>() {

                    protected Void doRun() throws SuspendExecution, InterruptedException {
                        final EventSource<String> ge = (EventSource) ActorRegistry.getActor("myEventServer");
                        ge.notify("hello world");
                        return null;
                    }
                }).join();
            }
            break;
        case testMultiGetActor:
            if (i == 1) {
                spawnGenEvent(new Initializer() {

                    AtomicInteger ai = new AtomicInteger();

                    @Override
                    public void init() throws SuspendExecution {
                        Actor.currentActor().register("myEventServer");
                        try {
                            final EventSource<String> ge = LocalActor.self();
                            ge.addHandler(new EventHandler<String>() {

                                @Override
                                public void handleEvent(String event) {
                                    System.out.println("msg no " + ai.incrementAndGet() + ": " + event);
                                }
                            });
                        } catch (InterruptedException ex) {
                            System.out.println(ex);
                        }
                    }

                    @Override
                    public void terminate(Throwable cause) throws SuspendExecution {
                        System.out.println("terminated");
                    }
                }).join();
            } else {
                Queue<Actor> queue = new LinkedList<>();
                for (int j = 0; j < 1000; j++) {
                    final BasicActor<Message, Void> actor = spawnActor(new BasicActor<Message, Void>("actor-" + j) {

                        protected Void doRun() throws SuspendExecution, InterruptedException {
                            try {
                                final EventSource<String> ge = (EventSource) ActorRegistry.getActor("myEventServer");
                                ge.notify("hwf " + getName());
                            } catch (Exception e) {
                                System.out.println("error in " + getName());
                                throw e;
                            }
                            return null;
                        }
                    });
                    queue.add(actor);
                //                        actor.join();
                }
                for (Actor localActor : queue) localActor.join();
                Thread.sleep(500);
            }
            break;
        case testOrdering:
            if (i == 1) {
                spawnGenEvent(new Initializer() {

                    AtomicInteger ai = new AtomicInteger();

                    @Override
                    public void init() throws SuspendExecution {
                        EventSourceActor.currentEventSourceActor().register("myEventServer");
                        try {
                            EventSourceActor<String> ge = EventSourceActor.currentEventSourceActor();
                            ge.ref().addHandler(new EventHandler<String>() {

                                @Override
                                public void handleEvent(String event) {
                                    System.out.println("msg no " + ai.incrementAndGet() + ": " + event);
                                }
                            });
                        } catch (InterruptedException ex) {
                            System.out.println(ex);
                        }
                    }

                    @Override
                    public void terminate(Throwable cause) throws SuspendExecution {
                        System.out.println("terminated");
                    }
                }).join();
            } else {
                Queue<Actor> queue = new LinkedList<>();
                for (int j = 0; j < 1; j++) {
                    final BasicActor<Message, Void> actor = spawnActor(new BasicActor<Message, Void>("actor-" + j) {

                        protected Void doRun() throws SuspendExecution, InterruptedException {
                            try {
                                final EventSource<String> ge = (EventSource) ActorRegistry.getActor("myEventServer");
                                for (int k = 0; k < 3000; k++) ge.notify("hw " + k + " f" + getName());
                            } catch (Exception e) {
                                System.out.println("error in " + getName());
                                throw e;
                            }
                            return null;
                        }
                    });
                    queue.add(actor);
                //                        actor.join();
                }
                for (Actor localActor : queue) localActor.join();
                Thread.sleep(5000);
            }
            break;
        default:
    }
    System.out.println("finished");
    System.exit(0);
    while (true) {
        System.out.println("==================");
        ThreadUtil.dumpThreads();
        Thread.sleep(5000);
    }
}
Also used : Val(co.paralleluniverse.strands.dataflow.Val) Server(co.paralleluniverse.actors.behaviors.Server) StoreTransaction(co.paralleluniverse.galaxy.StoreTransaction) ActorRef(co.paralleluniverse.actors.ActorRef) Store(co.paralleluniverse.galaxy.Store) EventHandler(co.paralleluniverse.actors.behaviors.EventHandler) MailboxConfig(co.paralleluniverse.actors.MailboxConfig) LocalActor(co.paralleluniverse.actors.LocalActor) BasicActor(co.paralleluniverse.actors.BasicActor) EventSourceActor(co.paralleluniverse.actors.behaviors.EventSourceActor) Actor(co.paralleluniverse.actors.Actor) ServerActor(co.paralleluniverse.actors.behaviors.ServerActor) AbstractServerHandler(co.paralleluniverse.actors.behaviors.AbstractServerHandler) TimeoutException(co.paralleluniverse.galaxy.TimeoutException) SuspendExecution(co.paralleluniverse.fibers.SuspendExecution) BasicActor(co.paralleluniverse.actors.BasicActor) LinkedList(java.util.LinkedList) TimeoutException(co.paralleluniverse.galaxy.TimeoutException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) EventSource(co.paralleluniverse.actors.behaviors.EventSource) Initializer(co.paralleluniverse.actors.behaviors.Initializer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 8 with ActorRef

use of co.paralleluniverse.actors.ActorRef in project quasar by puniverse.

the class SupervisorActor method addChild1.

private ChildEntry addChild1(ChildSpec spec) {
    log().debug("Adding child {}", spec);
    ActorRef actor = null;
    if (spec.builder instanceof ActorRef) {
        actor = (ActorRef) spec.builder;
        if (findEntry(actor) != null)
            throw new SupervisorException("Supervisor " + this + " already supervises actor " + actor);
    }
    Object id = spec.getId();
    if (id == null && actor != null)
        id = actor.getName();
    if (id != null && findEntryById(id) != null)
        throw new SupervisorException("Supervisor " + this + " already supervises an actor by the name " + id);
    final ChildEntry child = new ChildEntry(spec, actor);
    children.add(child);
    if (id != null)
        childrenById.put(id, child);
    return child;
}
Also used : ActorRef(co.paralleluniverse.actors.ActorRef)

Example 9 with ActorRef

use of co.paralleluniverse.actors.ActorRef in project quasar by puniverse.

the class Server method main.

public static void main(String[] args) throws ExecutionException, InterruptedException {
    System.setProperty("galaxy.nodeId", Integer.toString(nodeId));
    System.setProperty("galaxy.port", Integer.toString(7050 + nodeId));
    System.setProperty("galaxy.slave_port", Integer.toString(8050 + nodeId));
    new Fiber(new ServerActor(new AbstractServerHandler<SumRequest, Integer, SumRequest>() {

        @Override
        public void init() throws SuspendExecution {
            super.init();
            Actor.currentActor().register("myServer");
            System.out.println(this.toString() + " is ready");
        }

        @Override
        public Integer handleCall(ActorRef<?> from, Object id, SumRequest m) {
            System.out.println(this.toString() + " is handling " + m);
            if (m.a == 0 && m.b == 0)
                ServerActor.currentServerActor().shutdown();
            return m.a + m.b;
        }
    })).start().join();
    System.exit(0);
}
Also used : ServerActor(co.paralleluniverse.actors.behaviors.ServerActor) ActorRef(co.paralleluniverse.actors.ActorRef) Fiber(co.paralleluniverse.fibers.Fiber) AbstractServerHandler(co.paralleluniverse.actors.behaviors.AbstractServerHandler)

Aggregations

ActorRef (co.paralleluniverse.actors.ActorRef)9 Actor (co.paralleluniverse.actors.Actor)3 LocalActor (co.paralleluniverse.actors.LocalActor)3 ExecutionException (java.util.concurrent.ExecutionException)3 ShutdownMessage (co.paralleluniverse.actors.ShutdownMessage)2 AbstractServerHandler (co.paralleluniverse.actors.behaviors.AbstractServerHandler)2 ServerActor (co.paralleluniverse.actors.behaviors.ServerActor)2 ChildSpec (co.paralleluniverse.actors.behaviors.Supervisor.ChildSpec)2 Test (org.junit.Test)2 BasicActor (co.paralleluniverse.actors.BasicActor)1 MailboxConfig (co.paralleluniverse.actors.MailboxConfig)1 EventHandler (co.paralleluniverse.actors.behaviors.EventHandler)1 EventSource (co.paralleluniverse.actors.behaviors.EventSource)1 EventSourceActor (co.paralleluniverse.actors.behaviors.EventSourceActor)1 Initializer (co.paralleluniverse.actors.behaviors.Initializer)1 Server (co.paralleluniverse.actors.behaviors.Server)1 Fiber (co.paralleluniverse.fibers.Fiber)1 SuspendExecution (co.paralleluniverse.fibers.SuspendExecution)1 Store (co.paralleluniverse.galaxy.Store)1 StoreTransaction (co.paralleluniverse.galaxy.StoreTransaction)1