What is AKKA?
Akka is a development library and runtime environment that can be used to build highly concurrent, distributed, fault-tolerant, event-driven JVM-based applications. Make it easier to build highly concurrent distributed applications. Akka encapsulates the Actor Model model. It can be understood as an asynchronous, non-blocking message delivery
The first type: send a string or a single value to AKKA
package day03
import scala.actors.Actor
/**
* Created by Administrator on 2017/9/18.
* 功能:演示AKK 1 继承Actor类 2 重写act方法
* 功能点:演示发送一个字符串
*/
// 1 继承actor类
class ActorDemo extends Actor{
// 2 重写act方法,一般有while true操作
override def act =
{
while(true)
{
// 3 在receive中写具体的操作,要用到case 判断
receive{
case name:String=>println("hello, "+name)
case money:Int=>println("how much "+money)
}
}
}
}
object ActorDemon01 {
def main(args: Array[String]): Unit = {
val helloActor=new ActorDemo // 1 创建一个继承actor的对象
helloActor.start() // 2 启动akka
helloActor ! "tom" // 3 依据具体的参数调用actor
helloActor ! 1000
}
}
Run the program in idea, and the running screenshot is as follows:
This is relatively simple, that is, the characters and values are transmitted separately, which can be understood
The second type: Send sample class to akka
package day03
import scala.actors.Actor
/**
* Created by Administrator on 2017/9/18.
* 功能:演示akka的发送样例类
*
*/
//几个样例类
case class Register(username:String,password:String)
case class Login(username:String,password:String)
// 1 继承Actor类
class ActorDemon02 extends Actor{
//2 重写act方法
override def act()
{
while(true)
{
//3 在receive中写相关的操作
receive{
case Login(username,password)=>println("Login "+username+" 密码 "+password)
case Register(username,password)=>println("Register "+username+" 密码 "+password)
}
}
}
}
object ActorDemon02 {
def main(args: Array[String]): Unit = {
val actorDemon02=new ActorDemon02 //4 创建akka对象
actorDemon02.start() // 4 启动akka
actorDemon02 ! Register("zhangsan","123") //调用akka
actorDemon02 ! Login("lisi","456")
}
}
Run the program in idea, and the screenshot of the program is as follows:
Explanation: This is a sample class transmitted to akka, similar to the first scenario
总结点: 一 Akka的开发流程
1 继承actor类 : extends Actor
2 重写act方法: override def act 一般有while true操作
3 在receive中写具体的操作,receive{ ,要用到case 判断
总结点:二 Actor的调用流程:
1 创建一个继承actor的对象: val helloActor=new ActorDemo
2 启动akka: helloActor.start()
3 依据具体的参数调用actor: helloActor ! "tom"
The third type: mutual call between akka
package day03
import scala.actors.Actor
/**
* Created by Administrator on 2017/9/18.
* 功能:演示akka akka之间发送数据
*
*/
case class Message(content:String,sender:Actor)
class zxcActor extends Actor
{
override def act: Unit =
{
while(true)
{
receive{
case Message(content,sender)=>println("zxc received "+content)
sender ! "北京欢迎你"
sender ! 1000
}
}
}
}
class chongActor(val zxc:zxcActor) extends Actor
{
override def act: Unit =
{
//this 代表调用的对象 指的是 chongActor
zxc ! Message("hello i am chong",this)
while(true)
{
receive{
case response:String=>println("chong received "+response)
case response:Int=>println("how much "+response)
}
}
}
}
object ActorDemon03 {
def main(args: Array[String]): Unit = {
val zxc=new zxcActor
val chong=new chongActor(zxc)
zxc.start()
chong.start()
}
}
Run the program in idea, and the screenshot of the program operation is as follows:
The summary points of calls between Akka:
1 The second actor's parameters must have a reference to the first actor
chongActor(val zxc:zxcActor)
2 When the next actor is called, it is placed in the act function and outside the receive
override def act: Unit =
{//this represents the calling object refers to chongActor
zxc! Message("hello i am chong",this)
Description: 1 It is placed outside of receive because it implements its own operation in receive
2 Use this to represent the caller when calling, which is more convenient