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:
- import akka.dispatch.Future;
-
- public interface MathTypedActor {
- public Future<Integer> square(int value);
- }
This is the specific implementation of TypedActor:
- import akka.actor.TypedActor;
- import akka.dispatch.Future;
-
- public class MathTypedActorImpl extends TypedActor implements MathTypedActor {
-
- @Override
- public Future<Integer> square(int value) {
- return future(value * value);
- }
-
- }
This is the main method called:
- import akka.actor.TypedActor;
- import akka.dispatch.Future;
-
- public class MathTypedActorTest {
-
- public static void main(String[] args) {
- test();
- }
-
- private static void test() {
- MathTypedActor math = TypedActor.newInstance(MathTypedActor.class, MathTypedActorImpl.class);
- Future<Integer> future = math.square(10);
- future.await();
- Integer result = future.result().get();
- System.out.println(result);
- TypedActor.stop(math);
- }
-
- }