Create akka Procedure - Java Message

Java examples for Message:AKKA

Description

Create akka Procedure

Demo Code



import akka.actor.UntypedActor;
import akka.actor.ActorSystem;
import akka.actor.ActorRef;
import akka.actor.Props;
import akka.japi.Procedure;

class SimpleDemoActor extends UntypedActor {
    Procedure<Object> level1 = new Procedure<Object>() {
        public void apply(Object message) throws Exception {
            if (message instanceof String) {
                if (message.equals("end")) {
                    getContext().unbecome();
                }//from w  w  w. j  a  v a2  s . c o  m
            } else {
                Emp emp = (Emp) message;
                double result = emp.getSalary() * 1.8;
                System.out.println(emp.getName() + ": " + result);
            }

            if(message.equals("become3")){
                getContext().become(level3, false);
            }

        }
    };

    Procedure<Object> level2 = new Procedure<Object>() {
        public void apply(Object message) throws Exception {
            if (message instanceof String) {
                if (message.equals("end")) {
                    getContext().unbecome();
                }
            } else {
                Emp emp = (Emp) message;
                double result = emp.getSalary() * 1.5;
                System.out.println("??" + emp.getName() + "????: " + result);
            }

        }
    };

    Procedure<Object> level3 = new Procedure<Object>() {
        public void apply(Object message) throws Exception {
            if (message instanceof String) {
                if (message.equals("end")) {
                    getContext().unbecome();
                }
            } else {
                Emp emp = (Emp) message;
                double result = emp.getSalary() * 1.2;
                System.out.println("??" + emp.getName() + "????: " + result);
            }

        }
    };

    @Override
    public void onReceive(Object message) throws Exception {
        String level = (String) message;
        if (level.equals("1")) {
            getContext().become(level1);
        } else if (level.equals("2")) {
            getContext().become(level2);
        }
    }
}

class Emp {
    private String name = "";
    private double salary = 0.0;

    public Emp(String name, double salary) {
        this.name = name;
        this.salary = salary;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }
}

public class BecomeUnbecomeActorDemo {
    public static void main(String[] args) {
        ActorSystem system = ActorSystem.create("sys");
        ActorRef ref = system.actorOf(Props.create(SimpleDemoActor.class), "simpleDemoActor");
        ref.tell("1", ActorRef.noSender());
        ref.tell(new Emp("empa", 10000), ActorRef.noSender());
        ref.tell(new Emp("empb", 20000), ActorRef.noSender());
        ref.tell("end", ActorRef.noSender());
        ref.tell("2", ActorRef.noSender());
        ref.tell(new Emp("empc", 10000), ActorRef.noSender());
        ref.tell(new Emp("empd", 20000), ActorRef.noSender());
        ref.tell("become3", ActorRef.noSender());
        ref.tell(new Emp("empe", 200000), ActorRef.noSender());
        ref.tell("end", ActorRef.noSender());
        system.terminate();

    }
}

Related Tutorials