Hello akka getting started example

Source: Internet
Author: User

Akka is a concurrent processing framework based on the actor model. Based on the event-driven concurrent processing model, each actor has its own attributes and operations, thus avoiding the need to share attributes (data) between multiple threads) instead, the lock mechanism is used for processing. This mechanism is well applied in Scala and cloure languages. It puts operations and attributes in an independent unit for processing, thus improving the concurrent processing capability.

 

The following uses the simplest helloword as the start to enter the akka world.

Function Description:

It is very easy to send a message to another actor through one actior and return the processing result. However, asynchronous processing is not synchronous, what's amazing here is that akka's internal mechanism has been further studied.

 

Helloworld class

Java code
  1. Package com. Test. akka;
  2. Import akka. Actor. Props;
  3. Import akka. Actor. untypedactor;
  4. Import akka. Actor. actorref;
  5. Public class helloworld extends untypedactor {
  6. @ Override
  7. Public void prestart (){
  8. // Create the greeter actor
  9. Final actorref greeter =
  10. Getcontext (). actorof (props. Create (greeter. Class), "greeter"); // create a greeter actor instance
  11. // Tell it to perform the greeting
  12. Greeter. Tell (greeter. msg. Greet, getself (); // send a message to greeter actor through the tell Method
  13. }
  14. @ Override
  15. Public void onreceive (Object MSG ){
  16. If (MSG = greeter. msg. Done ){
  17. // When the greeter is done, stop this actor and with it the application
  18. Getcontext (). Stop (getself ());
  19. } Else unhandled (MSG );
  20. }
  21. }

 

Greeter class

Java code
  1. Package com. Test. akka;
  2. Import akka. Actor. untypedactor;
  3. Public class greeter extends untypedactor {
  4. Public static Enum MSG {
  5. Greet, done;
  6. }
  7. @ Override
  8. Public void onreceive (Object MSG ){
  9. If (MSG = MSG. Greet ){
  10. System. Out. println ("Hello world! ");
  11. Getsender (). Tell (msg. Done, getself ());
  12. } Else unhandled (MSG );
  13. }
  14. }

Run the helloworld class. akka provides a main actor class. You can use this class to directly execute the above method. In eclipse, click Open Run dailoge in the helloworld class, and then click main. enter akka in the class option. main, and then select arguments to enter COM. test. akka. helloworld, click apply, and the running result is as follows:

Java code
  1. Hello world!
  2. [Info] [10:16:33. 003] [Main-akka.actor.default-dispatcher-5] [akka: // main/user/APP-terminator] Application supervisor has terminated, shutting down

 

According to the running results, helloworld actor correctly calls greeter actor because it outputs Hello world! From the infor log, we can see that the helloworld actor normally receives the greeter actor and stops the current actor.

Hello akka getting started example

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.