Exemplo n.º 1
0
  /**
   * @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();
  }
Exemplo n.º 2
0
  // #crTest
  @Test
  public void countVotesAsIntendedNotAsInFlorida() {
    ActorRef routedActor = system.actorOf(new Props().withRouter(new VoteCountRouter()));
    routedActor.tell(DemocratVote);
    routedActor.tell(DemocratVote);
    routedActor.tell(RepublicanVote);
    routedActor.tell(DemocratVote);
    routedActor.tell(RepublicanVote);
    Timeout timeout = new Timeout(Duration.parse("1 seconds"));
    Future<Object> democratsResult = routedActor.ask(DemocratCountResult, timeout);
    Future<Object> republicansResult = routedActor.ask(RepublicanCountResult, timeout);

    assertEquals(3, Await.result(democratsResult, timeout.duration()));
    assertEquals(2, Await.result(republicansResult, timeout.duration()));
  }
    @Override
    public void actionPerformed(ActionEvent arg0) {

      if (master == null) {
        JOptionPane.showMessageDialog(
            null,
            "Error, There is no master to perform the builds. Please configure"
                + " a remote or local master in the menu bar.");
        return;
      }

      BATCHECKOUT checkout =
          (BATCHECKOUT) view.selectedDatesTablePanel.m_combo_checkout.getSelectedItem();
      BATBUILD build = (BATBUILD) view.selectedDatesTablePanel.m_combo_build.getSelectedItem();
      BATDELETE delete = (BATDELETE) view.selectedDatesTablePanel.m_combo_delete.getSelectedItem();

      Timeout timeout = new Timeout(Duration.create(2, "seconds"));
      Future<Object> selectedDatesFuture =
          Patterns.ask(selectedActor, new RequestSelectedDatesMessage(), timeout);
      try {
        CurrentSelectedDatesResponse sdm =
            (CurrentSelectedDatesResponse) Await.result(selectedDatesFuture, timeout.duration());
        for (SelectedDate d : sdm.dates) {
          master.tell(
              new CheckoutBuildDeleteTask(master, d.getDate(), checkout, build, delete), self());
        }

        selectedActor.tell(new RemoveAllDates());
        updateAllGui(new ArrayList<SelectedDate>());
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
Exemplo n.º 4
0
 @Test
 public void useWatch() {
   ActorSystem system = ActorSystem.create("MySystem");
   ActorRef myActor = system.actorOf(new Props(WatchActor.class));
   Future<Object> future = Patterns.ask(myActor, "kill", 1000);
   assert Await.result(future, Duration.parse("1 second")).equals("finished");
   system.shutdown();
 }
 private List<SelectedDate> getSelectedDates() {
   Timeout timeout = new Timeout(Duration.create(2, "seconds"));
   Future<Object> selectedDatesFuture =
       Patterns.ask(selectedActor, new RequestSelectedDatesMessage(), timeout);
   CurrentSelectedDatesResponse sdm;
   try {
     sdm = (CurrentSelectedDatesResponse) Await.result(selectedDatesFuture, timeout.duration());
     return sdm.dates;
   } catch (Exception e) {
     e.printStackTrace();
     return null;
   }
 }
Exemplo n.º 6
0
  public static void main(String[] args) throws InterruptedException {

    ActorSystem app = ActorSystem.create("UntypedCoordinatedExample", AkkaSpec.testConf());

    ActorRef counter1 = app.actorOf(new Props().withCreator(UntypedCoordinatedCounter.class));
    ActorRef counter2 = app.actorOf(new Props().withCreator(UntypedCoordinatedCounter.class));

    counter1.tell(new Coordinated(new Increment(counter2)));

    Thread.sleep(3000);

    long timeout = 5000;
    Duration d = Duration.create(timeout, TimeUnit.MILLISECONDS);

    Future<Object> future1 = counter1.ask("GetCount", timeout);
    Future<Object> future2 = counter2.ask("GetCount", timeout);

    int count1 = (Integer) Await.result(future1, d);
    System.out.println("counter 1: " + count1);
    int count2 = (Integer) Await.result(future2, d);
    System.out.println("counter 1: " + count2);

    app.shutdown();
  }
Exemplo n.º 7
0
  @Test
  public void usePatternsGracefulStop() {
    ActorSystem system = ActorSystem.create("MySystem");
    ActorRef actorRef = system.actorOf(new Props(MyUntypedActor.class));
    // #gracefulStop

    try {
      Future<Boolean> stopped =
          gracefulStop(actorRef, Duration.create(5, TimeUnit.SECONDS), system);
      Await.result(stopped, Duration.create(6, TimeUnit.SECONDS));
      // the actor has been stopped
    } catch (ActorTimeoutException e) {
      // the actor wasn't stopped within 5 seconds
    }
    // #gracefulStop
    system.shutdown();
  }
Exemplo n.º 8
0
  @Test
  public void usingAsk() {
    ActorSystem system = ActorSystem.create("MySystem");
    ActorRef myActor =
        system.actorOf(
            new Props(
                new UntypedActorFactory() {
                  public UntypedActor create() {
                    return new MyAskActor();
                  }
                }),
            "myactor");

    // #using-ask
    Future<Object> future = Patterns.ask(myActor, "Hello", 1000);
    Object result = Await.result(future, Duration.create(1, TimeUnit.SECONDS));
    // #using-ask
    system.shutdown();
  }