Beispiel #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
    }
  }
 @Test
 public void createHierarchies() {
   try {
     // #typed-actor-hierarchy
     Squarer childSquarer =
         TypedActor.get(TypedActor.context())
             .typedActorOf(new TypedProps<SquarerImpl>(Squarer.class, SquarerImpl.class));
     // Use "childSquarer" as a Squarer
     // #typed-actor-hierarchy
   } catch (Exception e) {
     // dun care
   }
 }
  @Test
  public void mustGetTheTypedActorExtension() {

    try {
      // #typed-actor-extension-tools

      // Returns the Typed Actor Extension
      TypedActorExtension extension =
          TypedActor.get(system); // system is an instance of ActorSystem

      // Returns whether the reference is a Typed Actor Proxy or not
      TypedActor.get(system).isTypedActor(someReference);

      // Returns the backing Akka Actor behind an external Typed Actor Proxy
      TypedActor.get(system).getActorRefFor(someReference);

      // Returns the current ActorContext,
      // method only valid within methods of a TypedActor implementation
      ActorContext context = TypedActor.context();

      // Returns the external proxy of the current Typed Actor,
      // method only valid within methods of a TypedActor implementation
      Squarer sq = TypedActor.<Squarer>self();

      // Returns a contextual instance of the Typed Actor Extension
      // this means that if you create other Typed Actors with this,
      // they will become children to the current Typed Actor.
      TypedActor.get(TypedActor.context());

      // #typed-actor-extension-tools
    } catch (Exception e) {
      // dun care
    }
  }
 @Test
 public void proxyAnyActorRef() {
   try {
     final ActorRef actorRefToRemoteActor = system.deadLetters();
     // #typed-actor-remote
     Squarer typedActor =
         TypedActor.get(system)
             .typedActorOf(new TypedProps<Squarer>(Squarer.class), actorRefToRemoteActor);
     // Use "typedActor" as a FooBar
     // #typed-actor-remote
   } catch (Exception e) {
     // dun care
   }
 }
  private static CustomerService[] startRing(int n) {

    logger.warn("Spawning actors...");
    long startConstructing = System.currentTimeMillis();
    CustomerService[] nodes = new CustomerService[n];
    for (int i = 0; i < nodes.length; i++) {
      nodes[i] =
          (CustomerService)
              TypedActor.newRemoteInstance(
                  CustomerService.class, CustomerServiceImpl.class, 7000, "localhost", 9919);
      nodes[i].getCustomer(UUID.randomUUID().toString());
    }

    long endConstructing = System.currentTimeMillis();

    logger.warn("constructing :" + (endConstructing - startConstructing));

    return nodes;
  }
  @Test
  public void typedRouterPattern() {
    try {
      // #typed-router
      // prepare routees
      TypedActorExtension typed = TypedActor.get(system);

      Named named1 = typed.typedActorOf(new TypedProps<Named>(Named.class));

      Named named2 = typed.typedActorOf(new TypedProps<Named>(Named.class));

      List<Named> routees = new ArrayList<Named>();
      routees.add(named1);
      routees.add(named2);

      List<String> routeePaths = new ArrayList<String>();
      routeePaths.add(typed.getActorRefFor(named1).path().toStringWithoutAddress());
      routeePaths.add(typed.getActorRefFor(named2).path().toStringWithoutAddress());

      // prepare untyped router
      ActorRef router = system.actorOf(new RoundRobinGroup(routeePaths).props(), "router");

      // prepare typed proxy, forwarding MethodCall messages to `router`
      Named typedRouter = typed.typedActorOf(new TypedProps<Named>(Named.class), router);

      System.out.println("actor was: " + typedRouter.name()); // name-243
      System.out.println("actor was: " + typedRouter.name()); // name-614
      System.out.println("actor was: " + typedRouter.name()); // name-243
      System.out.println("actor was: " + typedRouter.name()); // name-614

      // #typed-router
      typed.poisonPill(named1);
      typed.poisonPill(named2);
      typed.poisonPill(typedRouter);

    } catch (Exception e) {
      // dun care
    }
  }