Akka
What is akka? Directly reference the above description on the akka Website:
Akka is a toolkit and runtime for building highly concurrent, distributed, and fault tolerant event-driven applications on the JVM.
Anyway, I think akka is difficult to get started with, and the documentation is not very friendly for beginners. This article will use akka to write a hello World Program. Starting with this program, we will introduce some basic concepts of akka.
Hello-world-java8 Project
Akka also providesJavaAndScalaVersion. The helloworld project in this article uses Java version. This project is for my useNetbeansCreatedGradleProject and put it on GitHub. Because it is a helloworld project, there are only two classes, as shown in:
Build. gradle
First introduce the build script:
If the script looks hard, you can first look at the series of articles I wrote, "read the gradle script. I have nothing to explain before,DependencyI think it is necessary to explain some of them. Our helloworld project only depends on two libraries:ScalaAndActor. Akka is mainly written in Scala, So it depends on Scala. Akka is veryModularThe helloworld project only uses the actor module, so the dependencies on the actor are declared. Note thatModule nameScala version: akka-actor-_ 2.11. Therefore, 2.3.6 is the real version of the actor module.
Myactor
The core of akka is the actor model. Therefore, we need to define an actor in helloworld:
Myactor inherited fromUntypedactor(Which correspondsTypedactorBut we do not have enough knowledge to discuss the differences between the two.) untypedactor is an abstract class and must be implemented.Onreceive ()Method.
Mailbox
In the actor world, actor communicate with each other through messages. Each actor hasMailbox(Mailbox), Can receiveMessageAnd then process them one by one. But akka has helped us deal with these details. What our actor has to do is implementOnreceive ()Method, and then process a message each time. As shown in the code above, myactor processes messages by printing them to the console.
Main
The second class of helloworld is main, which is the main class of the program, as shown below:
Actorsystem
In terms of concept, akka providesActorsystemClass to abstract the actor system:
The first line of the main () method creates an actor system and is named mysystem:
ActorSystem mySystem = ActorSystem.create("mySystem");
Actorref
The entire actor system is closed, and the actor has never been in direct contact with the outside world. They give this dangerous thing to their "broker"Actorref. The second line of the main () method calls the actorsystemActorof ()Method To create an actor named myactor:
ActorRef myActor = mySystem.actorOf(Props.create(MyActor.class), "myActor");
So far, our actor system has only one actor, as shown in:
Send messages and disable the actor System
The third line of the main () method uses the actorrefTell ()Method To send a message to myactor:
myActor.tell("Hello, World!", ActorRef.noSender());
The tell () method is
Asynchronous It only sends an email to the actor's mailbox and then returns it. The first parameter of the tell () method is
Message The second parameter is
Sender In this way, the receiver actor will know who sent the message to himself. Myactor does not care who sent the message, so we passed actorref. nosender () to it (). The fourth line of the main () method calls
Shutdown () Method to notify the actor system to terminate itself:
mySystem.shutdown();
This is all the code, run the program, and we can see that the console has printed the long-awaited
Hello, world!
Conclusion
This article uses akka to create a helloworld project and introduces some basic concepts of akka. In the next article, we will use akka to create an echoserver and describe how to use akka to process the TCP protocol.
Akka Study Notes (1) -- Hello, world!