Akka, a lightweight actor framework

Source: Internet
Author: User

The name of akka is often heard when I was learning Scala. Even Scala authors have to admire its excellent implementation of actor. Recently, I wrote a server code. I need to query multiple databases in the background for the received requests. This naturally comes to the idea of using multithreading for parallel queries and then merging the results. The most important here is the execution of multi-threaded tasks and the callback of execution results. At this time, I suddenly recalled a simple "!" when using Scala. The multi-thread call is refreshing and convenient. What is the current java + akka framework?

I first looked at the official website and was surprised to find that the framework was too strong. After downloading, we can see many high-quality libraries, such as httpclient, netty, jetty, and asm. I don't know what he wants to do. As shown on the official website, akka can be used in two ways: one is called as a module and the other is called as a service.

After reading the document, I feel that akka is really outstanding! If the actor mode is implemented, the first step is to have excellent fault tolerance, and the second step is to implement the transaction function for memory objects, in addition, it can easily implement remote calls and inter-thread communication, customize routing policies, and integrate with third-party frameworks. Based on the above functions, akka has full strength as the core framework of the system. It is very easy to implement a distributed computing system through its simple multi-threaded calling method and remote calling.

Finally, let's give a small example. The code is very simple, but there are a lot of libraries used, and the positions are scattered. Let's find them by yourself.

This is the interface of TypedActor:

 
  1. import akka.dispatch.Future;  
  2.  
  3. public interface MathTypedActor {  
  4.     public Future<Integer> square(int value);  
  5. } 

 

This is the specific implementation of TypedActor:

 
  1. import akka.actor.TypedActor;  
  2. import akka.dispatch.Future;  
  3.  
  4. public class MathTypedActorImpl extends TypedActor implements MathTypedActor {  
  5.  
  6.     @Override 
  7.     public Future<Integer> square(int value) {  
  8.         return future(value * value);  
  9.     }  
  10.  
  11. } 

This is the main method called:

 
  1. import akka.actor.TypedActor;  
  2. import akka.dispatch.Future;  
  3.  
  4. public class MathTypedActorTest {  
  5.  
  6.     public static void main(String[] args) {  
  7.         test();  
  8.     }  
  9.  
  10.     private static void test() {  
  11.         MathTypedActor math = TypedActor.newInstance(MathTypedActor.class, MathTypedActorImpl.class);  
  12.         Future<Integer> future = math.square(10);  
  13.         future.await();  
  14.         Integer result = future.result().get();  
  15.         System.out.println(result);  
  16.         TypedActor.stop(math);        
  17.     }  
  18.  
  19. } 

 

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.