Here's a Akka Java demo to illustrate how Akka works. 1, first download Akka's official bag
The download address is: http://akka.io/downloads/. I downloaded the Akka 2.3.15.
Unzip this compression pack, prepare to copy the associated jar package to your own project 2, create a Java project
Import Packages First
3, the following is the source code and source code description
Here, first paste into the source code, and then the source analysis. The source code has 4 files, the main function is, the main program to create Actorsystem, and generate Actor1 (creationactor), this Actor1, according to the random message, loop to create the thread, Each thread creates Actor2 (Calculatoractor), and a different message is sent to Actor2 to tell the second-tier actor what to do. These Actor2 do their own things, send a message to Actor1, tell themselves finished, Actor1 received the message, print the result, and end the thread.
The entire business can be expressed in the following diagram: (the left is a schematic, the right is a class diagram)
Op.java
<span style= "Font-family:microsoft yahei;font-size:14px;"
>package Com.cwqsolo.study.akka.demo;
Import java.io.Serializable; The Op class is an action class that contains the subtraction class and the subtraction result class public class Op {public interface Mathop extends Serializable {} public interface Mathresult extends Serializable {}//addition operation Static class Add implements Mathop {private static final long Seri
Alversionuid = 1L;
private final int N1;
private final int n2;
Public Add (int n1, int n2) {this.n1 = n1;
This.n2 = n2;
public int getN1 () {return N1;
public int getN2 () {return n2;
} Static class Addresult implements Mathresult {private static final long serialversionuid = 1L;
private final int N1;
private final int n2;
private final int result;
Public Addresult (int n1, int n2, int result) {this.n1 = n1;
This.n2 = n2;
This.result = result;
public int getN1 () {return N1; } public int getN2 () {return n2;
public int GetResult () {return result;
}///subtraction operation Static class subtract implements Mathop {private static final long serialversionuid = 1L;
private final int N1;
private final int n2;
public subtract (int n1, int n2) {this.n1 = n1;
This.n2 = n2;
public int getN1 () {return N1;
public int getN2 () {return n2;
} Static class Subtractresult implements Mathresult {private static final long serialversionuid = 1L;
private final int N1;
private final int n2;
private final int result;
Public Subtractresult (int n1, int n2, int result) {this.n1 = n1;
This.n2 = n2;
This.result = result;
public int getN1 () {return N1;
public int getN2 () {return n2;
public int GetResult () {return result; }///multiplication operation Static class Multiply implements Mathop {private static Final long serialversionuid = 1L;
private final int N1;
private final int n2;
Public Multiply (int n1, int n2) {this.n1 = n1;
This.n2 = n2;
public int getN1 () {return N1;
public int getN2 () {return n2;
} Static class Multiplicationresult implements Mathresult {private static final long serialversionuid = 1L;
private final int N1;
private final int n2;
private final int result;
Public Multiplicationresult (int n1, int n2, int result) {this.n1 = n1;
This.n2 = n2;
This.result = result;
public int getN1 () {return N1;
public int getN2 () {return n2;
public int GetResult () {return result;
}//Division operation static class Divide implements Mathop {private static final long serialversionuid = 1L;
Private final double N1;
private final int n2;
Public Divide (double n1, int n2) {this.n1 = n1; THIS.N2 =N2;
Public double getN1 () {return N1;
public int getN2 () {return n2;
} Static class Divisionresult implements Mathresult {private static final long serialversionuid = 1L;
Private final double N1;
private final int n2;
private final double result;
Public Divisionresult (double n1, int n2, double result) {this.n1 = n1;
This.n2 = n2;
This.result = result;
Public double getN1 () {return N1;
public int getN2 () {return n2;
Public double GetResult () {return result; }}}</span>
Calculatoractor.java
<span style= "Font-family:microsoft yahei;font-size:14px;"
>package Com.cwqsolo.study.akka.demo;
Import Akka.actor.UntypedActor;
public class Calculatoractor extends Untypedactor {@Override public void OnReceive (Object message) {
if (message instanceof Op.add) {Op.add Add = (op.add) message;
System.out.println ("Calculating" + add.getn1 () + "+" + add.getn2 ());
Op.addresult result = new Op.addresult (ADD.GETN1 (), ADD.GETN2 (), add.getn1 () + ADD.GETN2 ());
Getsender (). Tell (result, getself ());
else if (message instanceof op.subtract) {op.subtract subtract = (op.subtract) message;
System.out.println ("Calculating" + subtract.getn1 () + "-" + subtract.getn2 ()); Op.subtractresult result = new Op.subtractresult (SUBTRACT.GETN1 (), SUBTRACT.GETN2 (), SUBTRACT.GETN1 ()-Subtra
CT.GETN2 ());
Getsender (). Tell (result, getself ()); ' Else if ' (Message instanceof op.multiply) {op.multiply Multiply = (op.multiply) message;
System.out.println ("Calculating" + multiply.getn1 () + "*" + multiply.getn2 ()); Op.multiplicationresult result = new Op.multiplicationresult (MULTIPLY.GETN1 (), MULTIPLY.GETN2 (), Multiply.get
N1 () * MULTIPLY.GETN2 ());
Getsender (). Tell (result, getself ());
else if (message instanceof op.divide) {op.divide Divide = (op.divide) message;
System.out.println ("Calculating" + divide.getn1 () + "/" + DIVIDE.GETN2 ()); Op.divisionresult result = new Op.divisionresult (DIVIDE.GETN1 (), DIVIDE.GETN2 (), DIVIDE.GETN1 ()/DIVIDE.GETN2
());
Getsender (). Tell (result, getself ());
else {unhandled (message); }}}</span>
Creationactor.java
<span style= "Font-family:microsoft yahei;font-size:14px;"
>package Com.cwqsolo.study.akka.demo;
Import Akka.actor.ActorRef;
Import Akka.actor.Props;
Import Akka.actor.UntypedActor; Create actor public class Creationactor extends Untypedactor {@Override public void OnReceive (Object message) throws Ex ception {if (message instanceof op.mathop) {actorref calculator = GetContext (). Actorof (props.creat
E (Calculatoractor.class));
Calculator.tell (Message, getself ()); else if (message instanceof Op.multiplicationresult) {op.multiplicationresult result = (Op.multiplicationresult)
Message
System.out.printf ("Mul Result:%d *%d =%d\n", result.getn1 (), RESULT.GETN2 (), Result.getresult ());
GetContext (). Stop (Getsender ());
else if (message instanceof Op.divisionresult) {op.divisionresult result = (op.divisionresult) message; System.out.printf ("Div Result:%.0f/%d =%.2f\n", result.getn1 (), ResulT.GETN2 (), Result.getresult ());
GetContext (). Stop (Getsender ());
else {unhandled (message); }}}</span>
Creationapplication.java
<span style= "Font-family:microsoft yahei;font-size:14px;"
>package Com.cwqsolo.study.akka.demo;
Import static Java.util.concurrent.TimeUnit.SECONDS;
Import Java.util.Random;
Import scala.concurrent.duration.Duration;
Import Akka.actor.ActorRef;
Import Akka.actor.ActorSystem;
Import Akka.actor.Props;
Import Com.typesafe.config.ConfigFactory; public class Creationapplication {public static void main (string[] args) {if (Args.length = = 0 | | args[0].equals (
"Calculatorworker") Startremoteworkersystem ();
if (Args.length = = 0 | | args[0].equals ("creation")) Startremotecreationsystem (); public static void Startremoteworkersystem () {actorsystem.create ("Calculatorworkersystem"), Configfactory
. Load (("calculator"));
System.out.println ("Started Calculatorworkersystem");
The public static void Startremotecreationsystem () {final Actorsystem system = actorsystem.create ("Creationsystem")
Configfactory.load ("remotecreation")); Final actorref actor = system.actorof (Props.create (Creationactor.class), "Creationactor");
System.out.println ("Started Creationsystem");
Final Random r = new Random (); System.scheduler (). Schedule (Duration.create (1, SECONDS), Duration.create (1, SECONDS), new Runnable () {@ Override public void Run () {if (r.nextint)% 2 = 0) {Actor.tell (new op.multiply
(R.nextint (m), r.nextint), NULL);
else {Actor.tell (new Op.divide (R.nextint (10000), R.nextint () + 1), NULL);
}}, System.dispatcher ()); }}</span>
The results of the source run are:
Started Calculatorworkersystem
Started Creationsystem
Calculating 4915.0/63
Div RESULT:4915/63 = 78.02
Calculating 1409.0/15
Div RESULT:1409/15 = 93.93
Calculating 99 * 64
Mul result:99 * 64 = 6336
4. SOURCE Analysis 4.1, let's take a look at the sequence diagram and see how it is invoked.
4.2 Description of some key elements
1. Only one workersystem can be created
Actorsystem.create ("Calculatorworkersystem", configfactory.load ("calculator"));
2. Only one actorsystem can be created
Final Actorsystem system = actorsystem.create ("Creationsystem", configfactory.load ("remotecreation"));
3. Create actor
Final Actorref actor = system.actorof (Props.create (creationactor.class), "Creationactor");
4, send the message to this actor
Actor.tell (New Op.multiply (R.nextint), r.nextint (), NULL);
5, Actor return message to the upper-level call Actor
Getsender (). Tell (result, getself ());