Scala之AKKA


import akka.actor.{ActorSystem, Props}

import com.typesafe.config.ConfigFactory
import com.uplooking.bigdata.p5.akka.{Header, Shutdown, Start}
 
/**
  * Created by thinkpad on 2017/3/31.
  */
object RemoteAkkaClientOps extends App {
 
  val clientActorSystem = ActorSystem("RemoteAkkaClient",
    ConfigFactory.load().getConfig("MyRemoteClientSideActor"))
 
  val clientActorRef = clientActorSystem.actorOf(Props[RemoteClientActor], "remoteClientActor")
 
  clientActorRef ! Start
  Thread.sleep(2000)
  clientActorRef ! Header("1234", 12, true)
  Thread.sleep(2000)
  clientActorRef ! Shutdown(2)
 
  clientActorSystem.shutdown()
 

}

//---------------------------------------------------------------------------------------------------------------------------------------------

/**
  * 定义客户端和服务端之间通信的协议
  */
object Start extends Serializable
object Stop extends Serializable
trait Message {
  val id: String
}
case class Shutdown(waitSecs: Int) extends Serializable
case class Heartbeat(id: String, magic:Int) extends Message with Serializable
case class Header(id: String, len: Int, encrypted: Boolean) extends Message with Serializable
case class Packet(id: String, seq: Long, content: String) extends Message with Serializable

//----------------------------------------------------------------------------------------------------------------------------------------------

/**
  * 专门用于Teacher接受的协议样例类
  */
object TeacherProtocol {
  case class Request()
  case class StudentRequest(request:String)
  case class Response(response: String)
}


/**
  * 专门用于Student接受的协议样例类
  */
object StudentProtocol {
 
  case class InitSignl()
}

 


//----------------------------------------------------------------------------------------------------------------------------

/**
  * 远程通信的服务端
  */
class RemoteServerActor extends Actor with ActorLogging{
 
  override def receive: Receive = {
    case Start => {
      log.info("----server Start----")
    }
    case Shutdown(waitSecs) => {
      log.info("系统将在" + waitSecs + "s后停止运行")
      Thread.sleep(waitSecs * 1000)
      //ActorSystem.shut,从整个ActorSystem程序上下文中获取ActorSystem对象,执行shutdown的动作
      context.system.shutdown()
    }
    case Header(id, len, encrypted) => {
      log.info("---client发送的header消息---" + (id, len, encrypted))
    }
  }
}



/**
  * 1、在类路径下面创建application.conf配置文件,指定服务端和客户端的配置信息
  * 2、定义的服务端和客户端的通信协议Message
  * 3、编写服务端、客户端
  * 4、启动整个应用程序
  *
  */
object RemoteServerAkkaOps extends App {
  /**
    * 我们在这里是要创建一个服务端的应用程序,供客户端进行连接
    *
    */
  val remoteActor = ActorSystem("remote-system",
    ConfigFactory.load().getConfig("MyRemoteServerSideActor"))
 
  val remoteActorRef = remoteActor.actorOf(Props[RemoteServerActor], "remoteActor")
 
}


//----------------------------------------------------------------------------------------------------------------------------------------------------

/**
  * 远程通讯客户端
  */
class RemoteClientActor extends Actor with ActorLogging{
  //要通过客户端去访问服务端,所以需要在客户端持有服务端的一个代理对象
  //远程的这种方式,我们需要通过netty的方式从整个应用程序上下文对象中得到
  //构建server在网络中的地址
  //netty格式:akka.tcp://actorsytemName@hostname:port/user/remoteActorRefName
  // akka.<protocol>://<actor system>@<hostname>:<port>/<actor path>
 
  val path = "akka.tcp://remote-system@192.168.100.8:2556/user/remoteActor"
  val actorSelection = context.actorSelection(path)
  override def receive: Receive = {
    case Start => {
      log.info("客户端发送给服务端的Start消息")
      actorSelection ! Start
    }
    case Shutdown(waitSecs) => {
      log.info("客户端发送给服务端的Shutdown消息")
      actorSelection !  Shutdown(waitSecs)
    }
    case Header(id, len, encrypted) => {
      log.info("客户端发送给服务端的Header消息")
      actorSelection ! Header(id, len, encrypted)
    }
  }
}

/**
  * Created by thinkpad on 2017/3/31.
  */
object RemoteAkkaClientOps extends App {
 
  val clientActorSystem = ActorSystem("RemoteAkkaClient",
    ConfigFactory.load().getConfig("MyRemoteClientSideActor"))
 
  val clientActorRef = clientActorSystem.actorOf(Props[RemoteClientActor], "remoteClientActor")
 
  clientActorRef ! Start
  Thread.sleep(2000)
  clientActorRef ! Header("1234", 12, true)
  Thread.sleep(2000)
  clientActorRef ! Shutdown(2)
 
  clientActorSystem.shutdown()
 
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值