【Akka一】Akka入门

什么是Akka

Message-Driven Runtime is the Foundation to Reactive Applications

In Akka, your business logic is driven through message-based communication patterns that are independent of physical locations. Whether your Actors live on the same thread, a different machine, or a different datacenter—the semantics are the same. You focus on the business logic while Akka handles the lower level plumbing to give your applications Reactive characteristics:

Resilience.
Resilience is a core tenet of Reactive applications, yet is often the most overlooked. With Akka, you get resilience for free as part of the model. Akka provides fault-tolerance based on supervisor hierarchies. Every Actor can create other Actors, which it will then supervise, making decisions if they should be resumed, restarted, retired or if the problem should be escalated. Instead of trying all things possible to prevent an error from happening, this approach lets you embrace the reality of unplanned errors. It means you can adopt a pragmatic ‘Let It Crash’ philosophy, with the confidence that the impacted components will be reset to a stable state and restarted upon failure.

Simpler Concurrency
Threads and non-blocking I/O are complex and error-prone to build by hand, which means they waste your time. With Akka's implementation of the Actor concurrency model, you are freed to focus on your application's business logic and let Akka think about scaling up.

Scalability and Elasticity
Scale out on multicore servers and multiple nodes using Akka makes application elasticity and true scale on demand a reality. On commodity hardware, you might run several million Actors—a huge step up from mere thousands of threads in a traditional Java application.

Scala & Java APIs
In keeping with the pragmatism of the rest of the Typesafe Reactive Platform, Akka has a APIs for both Java and Scala. This means smooth interoperability, and no need to learn or adopt Scala at all. Akka can run standalone on a JVM or deployed in your existing Java Application Server

In Akka, a Future is a data structure used to retrieve the result of some concurrent operation. This operation is usually performed by an Actor or by the Dispatcher directly. This result can be accessed synchronously (blocking) or asynchronously (non-blocking).

开发环境:

1. Intellij Idea创建Scala项目,此时默认添加了Scala Library

2. 添加Akka依赖:

http://central.maven.org/maven2/com/typesafe/akka/akka-actor_2.10/2.3.9/akka-actor_2.10-2.3.9.jar

http://central.maven.org/maven2/com/typesafe/config/1.2.1/config-1.2.1.jar

Akka Actor间异步通信

package akka.examples

import akka.actor.{ActorSystem, Props, Actor}

case object Goodbye

class HelloActor(name: String) extends Actor {
  def receive = {
    case "Hello" => println("Hello from %s".format(name))
    case Goodbye => println("Goodbye")
    case _ => println("%s, huh?".format(name))
  }
}

object HelloAkka {
  def main(args: Array[String]) {
    val system = ActorSystem("HelloSystem")
    // 如果HelloActor不带构造参数,这可以这么使用
    // val helloActor = system.actorOf(Props[HelloActor], name = "helloactor")
    // HelloActor带构造参数的
    val actor = system.actorOf(Props(new HelloActor("Jimmy")), name = "helloactor")
    actor ! "Hello"
    actor ! Goodbye
  }
}

Akka Actor间同步通信

package akka.examples

import java.util.concurrent.TimeoutException

import akka.actor._
import akka.actor.{Props, ActorSystem, Actor}
import akka.pattern.ask
import akka.util.Timeout
import scala.concurrent.duration._

import scala.concurrent.Await
import scala.concurrent.Future

case object Who

case object AnswerTimeout

class AnswerActor extends Actor {
  override def receive: Receive = {
    case Who => sender ! "Tom"
    case AnswerTimeout => {
      Thread.sleep(1000 * 10);
      sender ! "Answer time out"
    }
    case _ => sender ! "Not supported question"
  }
}

object AkkaFuture {

  def main(args: Array[String]) {
    val system = ActorSystem("QASystem")
    val answerActor = system.actorOf(Props(new AnswerActor), name = "answerActor")


    //1. 使用?同步发送请求, future等待
    implicit val timeout = Timeout(5 seconds)
    val future1 = answerActor ? Who
    val r1 = Await.result(future1, timeout.duration).asInstanceOf[String]
    println(r1)

    //2. 同步请求超时
    val future2 = answerActor ? AnswerTimeout
    try {
      val r2 = Await.result(future2, timeout.duration).asInstanceOf[String]
      println(r2)
    } catch {
      case e: TimeoutException => println(e.getMessage)
    }

    //通过ask方法发送同步等待消息
    val future3: Future[String] = ask(answerActor, "RocketQuestion").mapTo[String]
    val result3 = Await.result(future3, 5 second)
    println(result3)

    system.shutdown()
  }
}

参考://http://doc.akka.io/docs/akka/snapshot/scala/futures.html#futures-scala

猜你喜欢

转载自bit1129.iteye.com/blog/2187642
今日推荐