Props is a configuration class to specify options for the creation of actors, think of it as an immutable and thus
Freely shareable recipe for creating an actor including associated deployment information (e.g. which dispatcher
To use, see more below).
Create actor Mode one
Define an actor, as below,
Package com.usoft2;
Import Akka.actor.UntypedActor;
/**
* Created by Liyanxin on 2015/1/12.
*/Public
class Myactor extends Untypedactor {
private int x;
private int y;
public myactor (int x, int y) {
this.x = x;
This.y = y;
}
@Override public
void OnReceive (Object message) throws Exception {
System.out.println ("message=" + message);
int result = x + y;
This.getsender (). Tell (result, this.getself ());
This.getcontext (). Stop (This.getself ());
}
}
You can see that there are some property variables in the class, how to create the actor using its constructor through the Props.create method. As follows
Package com.usoft2;
Import Akka.actor.ActorRef;
Import Akka.actor.ActorSystem;
Import Akka.actor.Props;
Import Akka.actor.UntypedActor;
/** * Created by liyanxin on 2015/1/12. */public class HelloWorld {public static class Startactor extends Untypedactor {@Override public
void Prestart () throws Exception {/** * Props.create method, the first parameter is Myactor class, the argument behind is the parameterized list, * Represents the parameters passed into its constructor */final Actorref child = GetContext (). Actorof (Props.cre
Ate (Myactor.class, 4, 5), "MyChild");
Child.tell ("Good Morning", this.getself ()); } @Override public void OnReceive (Object message) throws Exception {System.out.println ("ResU
lt= "+ message);
This.getcontext (). Stop (This.getself ());
}} public static void Main (String args[]) {Actorsystem system = actorsystem.create ("Myactorsystem"); System.actorof (Props.creaTe (Startactor.class), "HelloWorld"); }
}
This code represents the arguments passed into the Acor constructor,
Final Actorref child =
getcontext (). Actorof (Props.create (Myactor.class, 4, 5), "MyChild");
Child.tell ("Good Morning", this.getself ());
Create actor Mode two
Define actor, ibid.,
Package com.usoft2;
Import Akka.actor.UntypedActor;
/**
* Created by Liyanxin on 2015/1/12.
*/Public
class Myactor extends Untypedactor {
private int x;
private int y;
public myactor (int x, int y) {
this.x = x;
This.y = y;
}
@Override public
void OnReceive (Object message) throws Exception {
System.out.println ("message=" + message);
int result = x + y;
This.getsender (). Tell (result, this.getself ());
This.getcontext (). Stop (This.getself ());
}
}
Define Actorcreator, as below,
Package com.usoft2;
Import Akka.japi.Creator;
/**
* Created by Liyanxin on 2015/1/12.
*/Public
class Myactorcreator implements creator<myactor> {
private int x;
private int y;
public myactorcreator (int x, int y) {
this.x = x;
This.y = y;
}
@Override Public
Myactor Create () throws Exception {
return new Myactor (x, y);
}
}
Finally see how to create an actor via Acorcreator, as below,
Package com.usoft2;
Import Akka.actor.ActorRef;
Import Akka.actor.ActorSystem;
Import Akka.actor.Props;
Import Akka.actor.UntypedActor;
/** * Created by liyanxin on 2015/1/12.
*/public class Helloworldcreator {public static class Startactor extends Untypedactor {@Override
public void Prestart () throws Exception {/** * Props.create method, the first parameter is Myactor class, the argument behind is the parameterized list, * Represents the parameter passed in to its constructor */final Actorref child = GetContext (). Actorof (Pro
Ps.create (New Myactorcreator (4, 5)), "MyChild");
Child.tell ("Good Morning", this.getself ()); } @Override public void OnReceive (Object message) throws Exception {System.out.println ("ResU
lt= "+ message);
This.getcontext (). Stop (This.getself ());
}} public static void Main (String args[]) {Actorsystem system = actorsystem.create ("Myactorsystem"); System.actorof(Props.create (Startactor.class), "HelloWorld"); }
}
This code shows how to create an actor with the Creator object using the Props.create method, as below,
Final Actorref child =
getcontext (). Actorof (Props.create (New Myactorcreator (4, 5)), "MyChild");
Child.tell ("Good Morning", this.getself ());
Best Practices
Define an actor as follows,
Package com.usoft2;
Import Akka.actor.Props;
Import Akka.actor.UntypedActor;
Import Akka.japi.Creator;
/** * Created by liyanxin on 2015/1/12.
*/public class MyActor2 extends Untypedactor {private int x;
private int y;
public MyActor2 (int x, int y) {this.x = x;
This.y = y;
}/** * Create Props for a actor of this type. * This is equivalent to a static workshop method that creates Props instance */public static Props Props (final int x, final int y) directly from this static method {return
Props.create (New creator<myactor2> () {private static final long serialversionuid = 1L;
@Override public MyActor2 Create () throws Exception {return new MyActor2 (x, y);
}
}); } @Override public void OnReceive (Object message) throws Exception {System.out.println ("message=" + mes
SAGE);
int result = x + y;
This.getsender (). Tell (result, this.getself ()); This.getcontext (). Stop(This.getself ()); }
}
The static factory method that directly returns the Props instance is defined above, and the actor is created by Props instance, as follows,
Package com.usoft2;
Import Akka.actor.ActorRef;
Import Akka.actor.ActorSystem;
Import Akka.actor.Props;
Import Akka.actor.UntypedActor;
/** * Created by liyanxin on 2015/1/12. */public class HelloWorld2 {public static class Startactor extends Untypedactor {@Override public
void Prestart () throws Exception {/** * Props.create method, the first parameter is Myactor class, the argument behind is the parameterized list, * Represents the parameters passed into its constructor */final Actorref child = GetContext (). Actorof (MyActor2.
Props (4, 7), "MyChild");
Child.tell ("Good Morning", this.getself ()); } @Override public void OnReceive (Object message) throws Exception {System.out.println ("ResU
lt= "+ message);
This.getcontext (). Stop (This.getself ());
}} public static void Main (String args[]) {Actorsystem system = actorsystem.create ("Myactorsystem"); System.actorof (Props.create (startactor. Class), "HelloWorld"); }
}