【Akka一】Akka入门
编程技术  /  houtizong 发布于 3年前   64
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
package akka.examplesimport akka.actor.{ActorSystem, Props, Actor}case object Goodbyeclass 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 }}
package akka.examplesimport java.util.concurrent.TimeoutExceptionimport akka.actor._import akka.actor.{Props, ActorSystem, Actor}import akka.pattern.askimport akka.util.Timeoutimport scala.concurrent.duration._import scala.concurrent.Awaitimport scala.concurrent.Futurecase object Whocase object AnswerTimeoutclass 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
请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!
技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成
网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];
文章归档
文章标签
友情链接