Package co.paralleluniverse.actors

Examples of co.paralleluniverse.actors.Actor


    private ActorRef<?> start(ChildEntry child) {
        final ActorRef old = child.actor;
        if (old != null && !LocalActor.isDone(old))
            throw new IllegalStateException("Actor " + child.actor + " cannot be restarted because it is not dead");

        final Actor actor = child.spec.builder.build();
        if (actor.getName() == null && child.spec.id != null)
            actor.setName(child.spec.id);

        log().info("{} starting child {}", this, actor);

        if (old != null && actor.getMonitor() == null && isLocal(old) && LocalActor.getMonitor(old) != null)
            actor.setMonitor(LocalActor.getMonitor(old));
        if (actor.getMonitor() != null)
            actor.getMonitor().addRestart();

        final Strand strand;
        if (actor.getStrand() != null)
            strand = actor.getStrand();
        else
            strand = createStrandForActor(child.actor != null && isLocal(child.actor) ? LocalActor.getStrand(child.actor) : null, actor);

        try {
            strand.start();
        } catch (IllegalThreadStateException e) {
            log().info("Child {} has already been started.", actor);
        }

        return start(child, actor.ref());
    }
View Full Code Here


        assert !actor.equals(LocalActor.self()) : "Can't \"call\" self - deadlock guaranteed";

        if (m.getFrom() == null)
            m.setFrom(from());

        final Actor currentActor;
        if (m.getFrom() instanceof TempActor)
            currentActor = ((TempActor<?>) m.getFrom()).actor.get();
        else
            currentActor = Actor.currentActor();

        assert currentActor != null;

        final Object watch = currentActor.watch(actor);

        if (m.getId() == null)
            m.setId(watch);

        final Object id = m.getId();

        final SelectiveReceiveHelper<Object> helper = new SelectiveReceiveHelper<Object>(currentActor) {
            @Override
            protected void handleLifecycleMessage(LifecycleMessage m) {
                if (m instanceof ExitMessage) {
                    final ExitMessage exit = (ExitMessage) m;
                    if (Objects.equals(exit.getActor(), actor) && exit.getWatch() == watch)
                        throw Exceptions.rethrow(exit.getCause());
                }
                super.handleLifecycleMessage(m);
            }
        };
        try {
            actor.sendSync(m);
            final ResponseMessage response = (ResponseMessage) helper.receive(timeout, unit, new MessageProcessor<Object, Object>() {
                @Override
                public Object process(Object m) throws SuspendExecution, InterruptedException {
                    return (m instanceof ResponseMessage && id.equals(((ResponseMessage) m).getId())) ? m : null;
                }
            });
            currentActor.unwatch(actor, watch); // no need to unwatch in case of receiver death, so not doen in finally block

            if (response instanceof ErrorResponseMessage)
                throw Exceptions.rethrow(((ErrorResponseMessage) response).getError());
            return ((ValueResponseMessage<V>) response).getValue();
        } finally {
View Full Code Here

    private static ActorRef getCurrentActor() {
        ActorRef actorRef = LocalActor.self();
        if (actorRef == null) {
            // create a "dummy actor" on the current strand
            Actor actor = new Actor(Strand.currentStrand(), null, new MailboxConfig(5, OverflowPolicy.THROW)) {
                @Override
                protected Object doRun() throws InterruptedException, SuspendExecution {
                    throw new AssertionError();
                }
            };
View Full Code Here

    private ActorRef<?> start(ChildEntry child) {
        final ActorRef old = child.actor;
        if (old != null && !LocalActorUtil.isDone(old))
            throw new IllegalStateException("Actor " + child.actor + " cannot be restarted because it is not dead");

        final Actor actor = child.spec.builder.build();
        if (actor.getName() == null && child.spec.id != null)
            actor.setName(child.spec.id);

        LOG.info("{} starting child {}", this, actor);

        if (old != null && actor.getMonitor() == null && isLocal(old) && LocalActorUtil.getMonitor(old) != null)
            actor.setMonitor(LocalActorUtil.getMonitor(old));
        if (actor.getMonitor() != null)
            actor.getMonitor().addRestart();

        final Strand strand;
        if (actor.getStrand() != null)
            strand = actor.getStrand();
        else
            strand = createStrandForActor(child.actor != null && isLocal(child.actor) ? LocalActorUtil.getStrand(child.actor) : null, actor);

        try {
            strand.start();
        } catch (IllegalThreadStateException e) {
            LOG.info("Child {} has already been started.", actor);
        }

        return start(child, actor.ref());
    }
View Full Code Here

    }

    public static GenResponseMessage call(final ActorRef actor, GenRequestMessage m, long timeout, TimeUnit unit) throws TimeoutException, InterruptedException, SuspendExecution {
        assert !actor.equals(ActorRef.self()) : "Can't \"call\" self - deadlock guaranteed";
       
        final Actor currentActor;
        if (m.getFrom() instanceof TempActor)
            currentActor = ((TempActor<?>) m.getFrom()).actor.get();
        else
            currentActor = Actor.currentActor();

        assert currentActor != null;

        final Object watch = currentActor.watch(actor);

        if (m.getId() == null)
            m.setId(watch);

        final Object id = m.getId();

        final SelectiveReceiveHelper<Object> helper = new SelectiveReceiveHelper<Object>(currentActor) {
            @Override
            protected void handleLifecycleMessage(LifecycleMessage m) {
                if (m instanceof ExitMessage) {
                    final ExitMessage exit = (ExitMessage) m;
                    if (Objects.equals(exit.getActor(), actor) && exit.getWatch() == watch)
                        throw Exceptions.rethrow(exit.getCause());
                }
                super.handleLifecycleMessage(m);
            }
        };
        try {
            actor.sendSync(m);
            final GenResponseMessage response = (GenResponseMessage) helper.receive(timeout, unit, new MessageProcessor<Object, Object>() {
                @Override
                public Object process(Object m) throws SuspendExecution, InterruptedException {
                    return (m instanceof GenResponseMessage && id.equals(((GenResponseMessage) m).getId())) ? m : null;
                }
            });
            currentActor.unwatch(actor, watch); // no need to unwatch in case of receiver death, so not doen in finally block

            if (response instanceof GenErrorResponseMessage)
                throw Exceptions.rethrow(((GenErrorResponseMessage) response).getError());
            return response;
        } finally {
View Full Code Here

    private static ActorRef getCurrentActor() {
        ActorRef actorRef = ActorRef.self();
        if (actorRef == null) {
            // create a "dummy actor" on the current strand
            Actor actor = new Actor(Strand.currentStrand(), null, new MailboxConfig(5, OverflowPolicy.THROW)) {
                @Override
                protected Object doRun() throws InterruptedException, SuspendExecution {
                    throw new AssertionError();
                }
            };
View Full Code Here

                } else {
                    spawnActor(new BasicActor<RepliableMessage<String>, Void>() {
                        int i=3;
                        @Override
                        protected Void doRun() throws InterruptedException, SuspendExecution {
                            Actor pong = getActor("pong");
                            System.out.println("pong is "+pong);
                            while (i-- > 0) {
                                pong.send(new RepliableMessage("ping",this));
                                RepliableMessage<String> msg = receive();
                                System.out.println("ping received "+msg.data);
                            }
                            pong.send(new RepliableMessage("finished",null));
                            return null;
                        }
                    }).join();
                }
                break;
View Full Code Here

        verifyInActor();
        final ChildEntry child = addChild1(spec);

        ActorRef<?> actor = null;
        if(spec.builder instanceof Actor) {
            final Actor a = ((Actor) spec.builder);
            actor = a.isStarted() ? a.ref() : a.spawn();
        }
        if (actor == null)
            actor = start(child);
        else
            start(child, actor);
View Full Code Here

    private ActorRef<?> start(ChildEntry child) throws SuspendExecution {
        final ActorRef old = child.actor;
        if (old != null && !LocalActor.isDone(old))
            throw new IllegalStateException("Actor " + child.actor + " cannot be restarted because it is not dead");

        final Actor actor = child.spec.builder.build();
        if (actor.getName() == null && child.spec.id != null)
            actor.setName(child.spec.id);

        log().info("{} starting child {}", this, actor);

        if (old != null && actor.getMonitor() == null && isLocal(old) && LocalActor.getMonitor(old) != null)
            actor.setMonitor(LocalActor.getMonitor(old));
        if (actor.getMonitor() != null)
            actor.getMonitor().addRestart();

        final Strand strand;
        if (actor.getStrand() != null)
            strand = actor.getStrand();
        else
            strand = createStrandForActor(child.actor != null && isLocal(child.actor) ? LocalActor.getStrand(child.actor) : null, actor);

        try {
            strand.start();
        } catch (IllegalThreadStateException e) {
            log().info("Child {} has already been started.", actor);
        }

        return start(child, actor.ref());
    }
View Full Code Here

            store.setListener(id, null);
            final byte[] buf = store.getx(id, null);
            assert buf != null : actorRef + " " + impl;
           
            final Object obj = ser.read(buf);
            final Actor actor;
            if (obj instanceof Actor)
                actor = (Actor) obj;
            else
                throw new IllegalArgumentException("Serialized object " + obj + " is not an actor.");
           
            GlobalRemoteChannelReceiver.getReceiver(actor.getMailbox(), id);
            return actor;
        } catch (co.paralleluniverse.galaxy.TimeoutException e) {
            throw new RuntimeException(e);
        }
    }
View Full Code Here

TOP

Related Classes of co.paralleluniverse.actors.Actor

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.