已经写了2个hello world的例子,包括以jar包方式运行和以微内核方式运行两种,下面写一个remote actor的例子,更贴近下现实的应用
1. 创建两个maven工程,一个是Remote端,一个Local端的,相当于Server/Client模式,maven依赖如下:
<akka.version>2.3.2</akka.version> <dependency> <groupId>com.typesafe.akka</groupId> <artifactId>akka-actor_2.10</artifactId> <version>${akka.version}</version> </dependency> <dependency> <groupId>com.typesafe.akka</groupId> <artifactId>akka-remote_2.10</artifactId> <version>${akka.version}</version> </dependency>
2. 编写Remote端代码,sender表示消息发送者,这里要记住ActorSystem的名字——HelloRemoteSystem和Actor的名字——RemoteActor,在Local端会使用到
HelloRemote.scala
object HelloRemote extends App { val system = ActorSystem("HelloRemoteSystem") val remoteActor = system.actorOf(Props[RemoteActor], name = "RemoteActor") remoteActor ! Message("The RemoteActor is alive") } class RemoteActor extends Actor { def receive = { case Message(msg) => println(s"RemoteActor received message '$msg'") sender ! Message("Hello from the RemoteActor") // 回复消息 case _ => println("RemoteActor got something unexpected.") } }
在src/main/resources中创建application.conf,这个文件要在运行的classpath下
akka { actor { provider = "akka.remote.RemoteActorRefProvider" } remote { transport = "akka.remote.netty.NettyRemoteTransport" netty.tcp { hostname = "127.0.0.1" # Remote端IP地址 port = 5150 # Remote端端口号 } } }
3. 编写Local端代码, 注意下面这行代码,调用Remote端的方式
akka.tcp://Remote端ActorSystem的名字@Remote端配置的IP:Remote端配置的端口号/user/Remote端的Actor名字
context.actorFor("akka.tcp://HelloRemoteSystem@127.0.0.1:5150/user/RemoteActor")
HelloLocal.scala
import akka.actor.ActorSystem import org.wuming.akka.hello.common.Message import org.wuming.akka.hello.common.Start import akka.actor.Actor import akka.actor.Props object HelloLocal extends App { System.setProperty("akka.remote.netty.port", "5152") implicit val system = ActorSystem("LocalSystem") val localActor = system.actorOf(Props[LocalActor], name = "LocalActor") // the local actor localActor ! Start } class LocalActor extends Actor { // create the remote actor val remote = context.actorFor("akka.tcp://HelloRemoteSystem@127.0.0.1:5150/user/RemoteActor") var counter = 0 def receive = { case Start => remote ! Message("Hello from the LocalActor") case Message(msg) => println(s"LocalActor received message: '$msg'") if (counter < 5) { sender ! Message("Hello back to you") counter += 1 } case _ => println("LocalActor got something unexpected.") } }
在src/main/resources中创建application.conf,这个文件要在运行的classpath下
akka { actor { provider = "akka.remote.RemoteActorRefProvider" } remote { transport = "akka.remote.netty.NettyRemoteTransport" netty.tcp { hostname = "127.0.0.1" # Local端IP地址 port = 0 # Local端端口号,0表示随机分配 } } }
4. Remote端和Local端共用了消息类型代码
Common.scala:
case object Start case class Message(msg: String)
5. 运行,首先运行Remote端,然后再运行Local端
RemoteActor received message 'The RemoteActor is alive'
RemoteActor received message 'Hello from the LocalActor'
RemoteActor received message 'Hello back to you'
RemoteActor received message 'Hello back to you'
RemoteActor received message 'Hello back to you'
RemoteActor received message 'Hello back to you'
RemoteActor received message 'Hello back to you'
LocalActor received message: 'Hello from the RemoteActor'
LocalActor received message: 'Hello from the RemoteActor'
LocalActor received message: 'Hello from the RemoteActor'
LocalActor received message: 'Hello from the RemoteActor'
LocalActor received message: 'Hello from the RemoteActor'
LocalActor received message: 'Hello from the RemoteActor'
相关推荐
标题中的“用Scala写的akka actor简单demo”指的是一个使用Scala编程语言编写的Akka Actor系统的基本示例。Akka是轻量级、基于actor模型的框架,它用于构建高度并发、分布式和容错的应用程序。Scala是多范式编程语言...
消息模式的Actor实现是响应式架构中的一个重要组件,而Akka框架则是Scala语言中实现这一模式的首选工具。 Akka是由Lightbend公司开发的一个开源框架,专门用于构建高度并发、分布式和容错的系统。它基于Actor模型,...
akka-actor_2.11 jar包
9. **Remote Deployment**:Akka支持远程部署,使得Actor可以跨网络进行通信,进一步增强了分布式的可能性。 10. **Sharding**:Akka的分片特性用于解决大规模Actor系统的内存问题。它将Actor分布在多个节点上,按...
- **Actor系统(Actor System)**:Actor系统的概念类似于进程或线程的概念,但它是Akka中的核心组件,用于管理Actor的生命周期。 - **Actor References, Paths and Addresses**:Actor之间的通信是通过发送消息完成...
《Akka Actor库详解——基于akka-actor-1.0-RC2.jar.zip的剖析》 Akka是一个由Lightbend公司维护的开源框架,主要用于构建高度并发、分布式和反应式的应用程序。在Java和Scala平台上,Akka因其强大的性能和易用性而...
akka-actor_2.12 jar包
尤其值得一提的是其核心组件——Akka Actor,它是实现异步、反应式编程的关键。本文将深入探讨Akka Actor 1.1.2版本,以及如何利用jar包进行项目集成。 Akka Actor系统是基于消息传递的并发模型,它将Actor作为计算...
Akka是JAVA虚拟机JVM平台上构建高并发、分布式和容错应用的工具包和...Akka用Scala语言写成,同时提供了Scala和JAVA的开发接口。 Akka处理并发的方法基于Actor模型。在Akka里,Actor之间通信的唯一机制就是消息传递。
《Akka Typed Actor详解——基于1.0-RC2.jar版本》 在Java和Scala的世界里,Akka是一个强大的工具库,它提供了构建高度可扩展、反应式应用程序的框架。Akka Typed Actor是Akka框架中的一个重要组件,它是对传统...
项目概述:邮电学院Scala大数据实时处理——基于Lambda架构的Spark源码实践 本项目为邮电学院假期师资培训的成果,主要采用Scala语言开发,围绕Lambda架构构建了一套实时数据流处理系统。项目包含35个文件,其中...