Package akka.actor

Examples of akka.actor.ActorSystem


public class Example1 {
  /**
   * @param args
   */
  public static void main(String[] args) {
    ActorSystem _system = ActorSystem.create("balancing-dispatcher",
        ConfigFactory.load().getConfig("MyDispatcherExample"));
   
    ActorRef actor = _system.actorOf(new Props(MsgEchoActor.class)
        .withDispatcher("balancingDispatcher").withRouter(
            new RoundRobinRouter(5)));

    for (int i = 0; i < 25; i++) {
      actor.tell(i);
    }

    _system.shutdown();

  }
View Full Code Here


import akka.agent.Agent;

public class Game {
  public static void main(String args[]) {

    ActorSystem _system = ActorSystem.create("Agent-example");
    Agent<String> turn = new Agent<String>("", _system);
    PingPong table = new PingPong(turn);

    Thread alice = new Thread(new Player("bob", table));
    Thread bob = new Thread(new Player("alice", table));

    alice.setName("alice");
    bob.setName("bob");
    alice.start(); // alice starts playing
    bob.start(); // bob starts playing
    try {
      // Wait 5 seconds
      Thread.sleep(5000);
    } catch (InterruptedException e) {
    }

    table.hit("DONE"); // cause the players to quit their threads.
    try {
      Thread.sleep(500);
    } catch (InterruptedException e) {
    }
    _system.shutdown();
  }
View Full Code Here

  /**
   * @param args
   * @throws InterruptedException
   */
  public static void main(String[] args) throws InterruptedException {
    ActorSystem _system = ActorSystem.create("RandomRouterExample",
        ConfigFactory.load().getConfig("MyRouterExample"));
    ActorRef randomRouter = _system.actorOf(
        new Props(MsgEchoActor.class).withRouter(new FromConfig()),
        "myRandomRouterActor");

    for (int i = 1; i <= 10; i++) {
      // sends randomly to actors
      randomRouter.tell(i);
    }
    _system.shutdown();
  }
View Full Code Here

  /**
   * @param args
   * @throws Exception
   */
  public static void main(String[] args) throws Exception {
    ActorSystem system = ActorSystem.create("faultTolerance");

    LoggingAdapter log = Logging.getLogger(system, system);

    Integer originalValue = Integer.valueOf(0);

    ActorRef supervisor = system.actorOf(new Props(SupervisorActor.class),
        "supervisor");

    log.info("Sending value 8, no exceptions should be thrown! ");
    supervisor.tell(Integer.valueOf(8));

    Integer result = (Integer) Await.result(
        Patterns.ask(supervisor, new Result(), 5000),
        Duration.create(5000, TimeUnit.MILLISECONDS));

    log.info("Value Received-> {}", result);
    assert result.equals(Integer.valueOf(8));

    log.info("Sending value -8, ArithmeticException should be thrown! Our Supervisor strategy says resume !");
    supervisor.tell(Integer.valueOf(-8));

    result = (Integer) Await.result(
        Patterns.ask(supervisor, new Result(), 5000),
        Duration.create(5000, TimeUnit.MILLISECONDS));

    log.info("Value Received-> {}", result);
    assert result.equals(Integer.valueOf(8));

    log.info("Sending value null, NullPointerException should be thrown! Our Supervisor strategy says restart !");
    supervisor.tell(null);

    result = (Integer) Await.result(
        Patterns.ask(supervisor, new Result(), 5000),
        Duration.create(5000, TimeUnit.MILLISECONDS));

    log.info("Value Received-> {}", result);
    assert originalValue.equals(result);

    log.info("Sending value \"String\", IllegalArgumentException should be thrown! Our Supervisor strategy says Stop !");

    supervisor.tell(String.valueOf("Do Something"));

    log.info("Worker Actor shutdown !");
    system.shutdown();
  }
View Full Code Here

  /**
   * @param args
   * @throws InterruptedException
   */
  public static void main(String[] args) throws InterruptedException {
    ActorSystem _system = ActorSystem.create("CustomRouteeRouterExample");
   
    ActorRef echoActor1 = _system.actorOf(new Props(MsgEchoActor.class));
    ActorRef echoActor2 = _system.actorOf(new Props(MsgEchoActor.class));
    ActorRef echoActor3 = _system.actorOf(new Props(MsgEchoActor.class));
   
    Iterable<ActorRef> routees = Arrays.asList(new ActorRef[] { echoActor1,
        echoActor2, echoActor3 });

    ActorRef randomRouter = _system.actorOf(new Props(MsgEchoActor.class)
        .withRouter(RandomRouter.create(routees)));

    for (int i = 1; i <= 10; i++) {
      // sends randomly to actors
      randomRouter.tell(i);
    }
    _system.shutdown();
  }
View Full Code Here

import akka.util.Timeout;

public class CalculatorActorSytem {

  public static void main(String[] args) throws Exception {
    ActorSystem _system = ActorSystem.create("TypedActorsExample");

    Timeout timeout = new Timeout(Duration.parse("5 seconds"));

    CalculatorInt calculator = TypedActor.get(_system).typedActorOf(
        new TypedProps<Calculator>(CalculatorInt.class,
            Calculator.class));

    // calling a fire and forget method
    calculator.incrementCount();

    // Invoke the method and wait for result
    Future<Integer> future = calculator.add(Integer.valueOf(14),
        Integer.valueOf(6));
    Integer result = Await.result(future, timeout.duration());

    System.out.println("Result is " + result);

    Option<Integer> counterResult = calculator.incrementAndReturn();
    System.out.println("Result is " + counterResult.get());

    counterResult = calculator.incrementAndReturn();
    System.out.println("Result is " + counterResult.get());

    // Get access to the ActorRef
    ActorRef calActor = TypedActor.get(_system).getActorRefFor(calculator);
    // call actor with a message
    calActor.tell("Hi there");
   
    _system.shutdown();

  }
View Full Code Here

  /**
   * @param args
   */
  public static void main(String[] args) {
    ActorSystem system = ActorSystem.create("zeromqClientTest");
    system.actorOf(new Props(ClientActor.class)
        .withRouter(new RoundRobinRouter(3)), "client");
  }
View Full Code Here

  /**
   * @param args
   */
  public static void main(String[] args) {
    ActorSystem system = ActorSystem.create("zeromqServerTest");
    system.actorOf(new Props(ServerActor.class), "server");
  }
View Full Code Here

  /**
   * @param args
   * @throws InterruptedException
   */
  public static void main(String[] args) throws InterruptedException {
    ActorSystem _system = ActorSystem.create("CustomRouteeRouterExample");

    int lowerBound = 2;
    int upperBound = 15;
    DefaultResizer resizer = new DefaultResizer(lowerBound, upperBound);

    ActorRef randomRouter = _system.actorOf(new Props(MsgEchoActor.class)
        .withRouter(new RandomRouter(resizer)));

    for (int i = 1; i <= 10; i++) {
      // sends randomly to actors
      randomRouter.tell(i);
    }
    _system.shutdown();
  }
View Full Code Here

import akka.routing.BroadcastRouter;

public class CalculatorActorSytem {

  public static void main(String[] args) throws Exception {
    ActorSystem _system = ActorSystem.create("TypedActorsExample");

    CalculatorInt calculator1 = TypedActor.get(_system).typedActorOf(
        new TypedProps<Calculator>(CalculatorInt.class,
            Calculator.class));

    CalculatorInt calculator2 = TypedActor.get(_system).typedActorOf(
        new TypedProps<Calculator>(CalculatorInt.class,
            Calculator.class));

    // Create a router with Typed Actors
    ActorRef actor1 = TypedActor.get(_system).getActorRefFor(calculator1);
    ActorRef actor2 = TypedActor.get(_system).getActorRefFor(calculator2);

    Iterable<ActorRef> routees = Arrays.asList(new ActorRef[] { actor1,
        actor2 });
    ActorRef router = _system.actorOf(new Props()
        .withRouter(BroadcastRouter.create(routees)));

    router.tell("Hello there");

    _system.shutdown();

  }
View Full Code Here

TOP

Related Classes of akka.actor.ActorSystem

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.