Echoserver
In the previous article, we used akka to write a simpleHelloworldFor exampleActorMode) has a preliminary understanding. This article uses akka to writeEchoserverTo see how to use actor in the worldTCP protocol.
GitHub Project
As usual, the echoserver code is put on GitHub. Echoserver is a little more complex than helloworld. There are three classes, as shown in:
Main
Start with the main class this time:
The first line of the main () method createsActor SystemThe name is mysystem. For the next four lines of code, see the detailed explanation below.
TCP Manager
Akka abstracts the entire TCP layer into an actor, which is the TCP manager. In the second line of the main () method, we added a TCP manager to the actor system and got in touch with its broker:
ActorRef tcpManager = Tcp.get(mySystem).getManager();
Now our actor system looks like this:
Props
PropsIt should bePropertiesIt is used to tell the actor system how to create an actor. Props provides fourFactory methodTo create a props instance, as shown below:
public static Props create(Class<?> type, Object[] os)public static <T extends Actor> Props create(Class<T> type, Creator<T> crtr)public static <T extends Actor> Props create(Creator<T> crtr)public static Props create(Class<?> type, Seq<Object> seq)
The third line of the main () method uses the first factory method above. This factory method has two parameters: a class and an array (actually
Vararg ). The first parameter indicates the class of the actor, and the second parameter is the parameter passed to the actor constructor. In this way, the actor system will know how to create according to props (Use
Reflection An actor instance. The fourth line of the main () method creates an accepter named accepter. The Accepter must use tcpmanager, which is why we pass tcpmanager to the props. Create () method as the second parameter. Now the actor system is changed to the following (I use the dotted arrow to indicate the dependencies between actor ):
The fifth line of the main () method sends a message to the Accepter: integer 12345. Tell it to bind port 12345, start listening to TCP connections, and prepare the echo service.
Accepter
The Accepter monitors the port and then sends the received TCP connection to handler for processing. The Accepter is a little complicated. The following is all the code:
The constructor is used to establish dependencies on tcpmanager. The onreceive () method is described in detail below:
- If the received message is of the integer type (Port), We send it to tcpmanager.Bind messageTo notify it to bind a port.
- If tcpmanager successfully binds the port, it will respondBoundMessage (Bound).
- Otherwise, respondBinding failedMessage (Commandfailed), Accepter ends itself by calling the getcontext (). Stop () method.
- After the port is successfully bound, if a connection arrives, you will receiveConnectedMessage. The Accepter creates a handler by calling getcontext (). actorof () and registers it with tcpmanager. Then tcpmanager sends the messages related to the connection to handler. In other words, the connection is taken over by handler.
Parent-child relationship between actor
Within an actor system, actor can be formed instead of simply dependent (or referenced) relationships.Parent and ChildLink: an actor can be created.Sub-actor, And thenSubtaskAssign them for processing. The children of an actor uses itsContextIn the above Code, the Accepter obtains its own context through getcontext (), and then creates a sub-actor by calling the actorof () method of context. Assume that two clients are connected to our echo server, then the entire actor system will be like this (the real line arrow in the actor system indicates the parent-child relationship ):
Handler
The last class is handler, which is relatively simple. The Code is as follows:
Handler only processes two types of messages:
- ReceivedThe message tells handler that a message arrives. Because it is an echo server, it does not care about what is in the received message. The getsender () method can obtain the message sender, that is, tcpmanager. Then, send a message to tcpmanager.WriteMessage, telling it to write the data intact to the client.
- ConnectionclosedTells the handler that the connection has been disconnected, and the handler ends his short life by calling the stop () method of context.
Test echoserver
Start echoserver, and then you canTelnetCommand to test, which is not described in detail here.
Conclusion
It is still difficult to use akka to write an echo server. You need to understand many aspects of akka. However, compared with socket or NiO, the echo server of akka is obviously simpler.
Akka Study Notes (2) -- echo server