Package akka.tutorial.first.java

Source Code of akka.tutorial.first.java.ExampleApp$Counter

package akka.tutorial.first.java;

import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import akka.actor.UntypedActor;
import akka.dispatch.Future;
import akka.dispatch.OnSuccess;
import static akka.pattern.Patterns.ask;

public class ExampleApp {

    public static class Counter extends UntypedActor {
        int count = 0;

        public void onReceive(Object message) throws Exception {
            if ("tick".equals(message))
                count++;
            else if ("get".equals(message))
                getSender().tell(count);
            else
                unhandled(message);
        }
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
  public static void main(String... args) {
        ActorSystem system = ActorSystem.create("example");

        ActorRef counter = system.actorOf(new Props(Counter.class));

        counter.tell("tick");
        counter.tell("tick");
        counter.tell("tick");

        Future future = ask(counter, "get", 5000);

        future.onSuccess(new OnSuccess<Integer>() {
            public void onSuccess(Integer count) {
                System.out.println("Count is " + count);
            }
        });

        system.shutdown();
    }
}
TOP

Related Classes of akka.tutorial.first.java.ExampleApp$Counter

TOP
Copyright © 2018 www.massapi.com. 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.