示例#1
0
  public static void main(String[] args) {
    ActorSystem system = ActorSystem.create("typed-system", ConfigFactory.load());
    Squarer mySquarer =
        TypedActor.get(system)
            .typedActorOf(new TypedProps<SquarerImpl>(Squarer.class, SquarerImpl.class));
    Squarer otherSquarer =
        TypedActor.get(system)
            .typedActorOf(
                new TypedProps<SquarerImpl>(
                    Squarer.class,
                    new Creator<SquarerImpl>() {
                      @Override
                      public SquarerImpl create() throws Exception {
                        return new SquarerImpl("foo");
                      }
                    }));

    System.out.println("squareDontCare: " + new Date());
    mySquarer.squareDontCare(2);
    System.out.println("squareDontCare: " + new Date());

    System.out.println("square: " + new Date());
    Future<Integer> future = mySquarer.square(2);
    System.out.println("square: " + new Date());

    System.out.println("squareNowPlease: " + new Date());
    mySquarer.squareNowPlease(2);
    System.out.println("squareNowPlease: " + new Date());

    System.out.println("squareNow: " + new Date());
    mySquarer.squareNow(2);
    System.out.println("squareNow: " + new Date());
  }
  @Test
  public void createATypedActor() {
    try {
      // #typed-actor-create1
      Squarer mySquarer =
          TypedActor.get(system)
              .typedActorOf(new TypedProps<SquarerImpl>(Squarer.class, SquarerImpl.class));
      // #typed-actor-create1
      // #typed-actor-create2
      Squarer otherSquarer =
          TypedActor.get(system)
              .typedActorOf(
                  new TypedProps<SquarerImpl>(
                      Squarer.class,
                      new Creator<SquarerImpl>() {
                        public SquarerImpl create() {
                          return new SquarerImpl("foo");
                        }
                      }),
                  "name");
      // #typed-actor-create2

      // #typed-actor-calls
      // #typed-actor-call-oneway
      mySquarer.squareDontCare(10);
      // #typed-actor-call-oneway

      // #typed-actor-call-future
      Future<Integer> fSquare = mySquarer.square(10); // A Future[Int]
      // #typed-actor-call-future

      // #typed-actor-call-option
      Option<Integer> oSquare = mySquarer.squareNowPlease(10); // Option[Int]
      // #typed-actor-call-option

      // #typed-actor-call-strict
      int iSquare = mySquarer.squareNow(10); // Int
      // #typed-actor-call-strict
      // #typed-actor-calls

      assertEquals(100, Await.result(fSquare, Duration.create(3, TimeUnit.SECONDS)).intValue());

      assertEquals(100, oSquare.get().intValue());

      assertEquals(100, iSquare);

      // #typed-actor-stop
      TypedActor.get(system).stop(mySquarer);
      // #typed-actor-stop

      // #typed-actor-poisonpill
      TypedActor.get(system).poisonPill(otherSquarer);
      // #typed-actor-poisonpill
    } catch (Exception e) {
      // Ignore
    }
  }