Package akka.actor

Examples of akka.actor.Inbox


        // Create the 'greeter' actor
        final ActorRef greeter = system.actorOf(Props.create(Greeter.class), "greeter");

        // Create the "actor-in-a-box"
        final Inbox inbox = Inbox.create(system);

        // Tell the 'greeter' to change its 'greeting' message
        greeter.tell(new WhoToGreet("akka"), ActorRef.noSender());

        // Ask the 'greeter for the latest 'greeting'
        // Reply should go to the "actor-in-a-box"
        inbox.send(greeter, new Greet());

        // Wait 5 seconds for the reply with the 'greeting' message
        Greeting greeting1 = (Greeting) inbox.receive(Duration.create(5, TimeUnit.SECONDS));
        System.out.println("Greeting: " + greeting1.message);

        // Change the greeting and ask for it again
        greeter.tell(new WhoToGreet("typesafe"), ActorRef.noSender());
        inbox.send(greeter, new Greet());
        Greeting greeting2 = (Greeting) inbox.receive(Duration.create(5, TimeUnit.SECONDS));
        System.out.println("Greeting: " + greeting2.message);

        // after zero seconds, send a Greet message every second to the greeter with a sender of the GreetPrinter
        ActorRef greetPrinter = system.actorOf(Props.create(GreetPrinter.class));
        system.scheduler().schedule(Duration.Zero(), Duration.create(1, TimeUnit.SECONDS), greeter, new Greet(), system.dispatcher(), greetPrinter);
View Full Code Here


  @Test
  public void demonstrateInbox() {
    final JavaTestKit probe = new JavaTestKit(system);
    final ActorRef target = probe.getRef();
    //#inbox
    final Inbox inbox = Inbox.create(system);
    inbox.send(target, "hello");
    //#inbox
    probe.expectMsgEquals("hello");
    probe.send(probe.getLastSender(), "world");
    //#inbox
    try {
      assert inbox.receive(Duration.create(1, TimeUnit.SECONDS)).equals("world");
    } catch (java.util.concurrent.TimeoutException e) {
      // timeout
    }
    //#inbox
  }
View Full Code Here

  @Test
  public void demonstrateWatch() {
    final JavaTestKit probe = new JavaTestKit(system);
    final ActorRef target = probe.getRef();
    //#watch
    final Inbox inbox = Inbox.create(system);
    inbox.watch(target);
    target.tell(PoisonPill.getInstance(), ActorRef.noSender());
    try {
      assert inbox.receive(Duration.create(1, TimeUnit.SECONDS)) instanceof Terminated;
    } catch (java.util.concurrent.TimeoutException e) {
      // timeout
    }
    //#watch
  }
View Full Code Here

TOP

Related Classes of akka.actor.Inbox

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.