刚刚开始接触akka,网上找了2个简单示例,并在公司运营机器上尝试,踩了一些坑,在此记录。

1. 本地hello world

 [torstan@sparkb5-i ~/akka_example/hello_world]$ cat src/helloWorld.scala
package our.examples
import akka.actor.Actor
import akka.actor.ActorSystem
import akka.actor.Props class HelloActor extends Actor {
def receive = {
case "hello" => println("hello back at you")
case _ => println("huh?")
}
} object HelloApp extends App {
override def main(args: Array[String]){
val system = ActorSystem("HelloSystem")
// default Actor constructor
val helloActor = system.actorOf(Props[HelloActor], name = "helloactor")
helloActor ! "hello"
helloActor ! "buenos dias"
}
}
 [torstan@sparkb5-i ~/akka_example/hello_world]$ cat Makefile
SRC_DIR := src
SRC := $(shell find ${SRC_DIR} -name "*.scala")
DIR=our TARGET := HelloApp.jar SCALAC := scalac
SCFLAGS := /usr/local/scala-2.10.4/lib/akka-actors.jar .PHONY: all clean all: ${TARGET} ${TARGET}: ${SRC}
${SCALAC} -cp ${SCFLAGS} $^ clean:
${RM} -r ${TARGET} ${DIR}
 [torstan@sparkb5-i ~/akka_example/hello_world]$ cat run.sh
#!/bin/bash java -cp \
.:/usr/local/scala-2.10.4/lib/scala-library.jar:/usr/local/scala-2.10.4/lib/akka-actors.jar:/usr/local/scala-2.10.4/lib/typesafe-config.jar\
our.examples.HelloApp

执行结果:

[torstan@sparkb5-i ~/akka_example/hello_world]$ ./run.sh
hello back at you
huh?

2. 简单CS示例

server端:

 [torstan@sparkb5-i ~/akka_example/remote_service/server]$ cat src/HelloRemote.scala
package remote
import akka.actor.Actor
import akka.actor.ActorSystem
import akka.actor.Props class RemoteActor extends Actor{
def receive = {
case msg:String =>
println(s"RemoteActor received message '$msg'")
sender ! "hello from RemoteActor"
}
} object HelloRemote extends App{
val system = ActorSystem("HelloRemoteSystem")
val remoteActor = system.actorOf(Props[RemoteActor], name="RemoteActor")
remoteActor ! "The remote actor is alive"
}
 [torstan@sparkb5-i ~/akka_example/remote_service/server]$ cat application.conf
akka {
loglevel = "DEBUG"
actor {
provider = "akka.remote.RemoteActorRefProvider"
}
remote {
transport = "akka.remote.netty.NettyRemoteTransport"
//log-sent-messages = on
//log-received-messages = on
netty {
hostname = "172.27.6.240"
port = 5150
}
}
}
 [torstan@sparkb5-i ~/akka_example/remote_service/server]$ cat Makefile
SRC_DIR := src
SRC := $(shell find ${SRC_DIR} -name "*.scala")
DIR=remote TARGET := HelloRemote.jar SCALAC := scalac
SCFLAGS := /usr/local/scala-2.10.4/lib/akka-actors.jar .PHONY: all clean all: ${TARGET} ${TARGET}: ${SRC}
${SCALAC} -cp ${SCFLAGS} $^ clean:
${RM} -r ${TARGET} ${DIR}
 [torstan@sparkb5-i ~/akka_example/remote_service/server]$ cat run.sh
#!/bin/bash AKKA_LIB_PATH="/usr/local/akka-2.1.4/lib/akka/" java -cp \
.:/usr/local/scala-2.10.4/lib/scala-library.jar:/usr/local/scala-2.10.4/lib/akka-actors.jar:/usr/local/scala-2.10.4/lib/typesafe-config.jar:${AKKA_LIB_PATH}/akka-remote_2.10-2.1.4.jar:${AKKA_LIB_PATH}/protobuf-java-2.4.1.jar:${AKKA_LIB_PATH}/netty-3.5.8.Final.jar \
remote.HelloRemote

client端:

 [torstan@sparkb5-i ~/akka_example/remote_service/client]$ cat src/HelloLocal.scala
package client
import akka.actor._ object HelloLocal extends App{
implicit val system = ActorSystem("LocalSystem")
val localActor = system.actorOf(Props[LocalActor], name="LocalActor")
localActor ! "START"
} class LocalActor extends Actor{
val remote = context.actorFor("akka://HelloRemoteSystem@172.27.6.240:5150/user/RemoteActor")
var counter = 0
def receive = {
case "START" =>
remote ! "HELLO from the LocalActor"
case msg:String =>
println(s"LocalActor received message: '$msg'")
if(counter < 5){
sender ! "hello back to you"
counter += 1
}
}
}
 [torstan@sparkb5-i ~/akka_example/remote_service/client]$ cat application.conf
akka {
//loglevel = "DEBUG"
actor {
provider = "akka.remote.RemoteActorRefProvider"
}
remote {
transport = "akka.remote.netty.NettyRemoteTransport"
//log-sent-messages = on
//log-received-messages = on
netty {
hostname = "127.0.0.1"
port = 0
}
}
}
 [torstan@sparkb5-i ~/akka_example/remote_service/client]$ cat Makefile
SRC_DIR := src
SRC := $(shell find ${SRC_DIR} -name "*.scala")
DIR=client TARGET := HelloLocal.jar SCALAC := scalac
SCFLAGS := /usr/local/scala-2.10.4/lib/akka-actors.jar .PHONY: all clean all: ${TARGET} ${TARGET}: ${SRC}
${SCALAC} -cp ${SCFLAGS} $^ clean:
${RM} -r ${TARGET} ${DIR}
 [torstan@sparkb5-i ~/akka_example/remote_service/client]$ cat run.sh
#!/bin/bash AKKA_LIB_PATH="/usr/local/akka-2.1.4/lib/akka/" java -cp \
.:/usr/local/scala-2.10.4/lib/scala-library.jar:/usr/local/scala-2.10.4/lib/akka-actors.jar:/usr/local/scala-2.10.4/lib/typesafe-config.jar:${AKKA_LIB_PATH}/akka-remote_2.10-2.1.4.jar:${AKKA_LIB_PATH}/protobuf-java-2.4.1.jar:${AKKA_LIB_PATH}/netty-3.5.8.Final.jar \
client.HelloLocal

执行结果:

[root@sparkb5-0 /data/torstan/akka_example/remote_service/server]# ./run.sh
[DEBUG] [12/10/2014 17:31:57.074] [main] [EventStream(akka://HelloRemoteSystem)] logger log1-Logging$DefaultLogger started
[DEBUG] [12/10/2014 17:31:57.080] [main] [EventStream(akka://HelloRemoteSystem)] Default Loggers started
[INFO] [12/10/2014 17:31:57.269] [main] [NettyRemoteTransport(akka://HelloRemoteSystem@172.27.6.240:5150)] RemoteServerStarted@akka://HelloRemoteSystem@172.27.6.240:5150
RemoteActor received message 'The remote actor is alive'
[INFO] [12/10/2014 17:32:01.768] [HelloRemoteSystem-10] [NettyRemoteTransport(akka://HelloRemoteSystem@172.27.6.240:5150)] RemoteClientStarted@akka://LocalSystem@127.0.0.1:48437
[DEBUG] [12/10/2014 17:32:01.769] [HelloRemoteSystem-10] [RemoteClient(akka://HelloRemoteSystem)] Starting remote client connection to [akka://LocalSystem@127.0.0.1:48437]
[DEBUG] [12/10/2014 17:32:01.771] [HelloRemoteSystem-10] [NettyRemoteTransport(akka://HelloRemoteSystem@172.27.6.240:5150)] RemoteServerClientConnected@akka://HelloRemoteSystem@172.27.6.240:5150: Client[akka://LocalSystem@127.0.0.1:48437]
RemoteActor received message 'HELLO from the LocalActor'
[DEBUG] [12/10/2014 17:32:01.782] [HelloRemoteSystem-akka.actor.default-dispatcher-2] [akka.serialization.Serialization(akka://HelloRemoteSystem)] Using serializer[akka.serialization.JavaSerializer] for message [java.lang.String]
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'

[torstan@sparkb5-i ~/akka_example/remote_service/client]$ ./run.sh
[INFO] [12/10/2014 17:32:01.668] [main] [NettyRemoteTransport(akka://LocalSystem@127.0.0.1:48437)] RemoteServerStarted@akka://LocalSystem@127.0.0.1:48437
[INFO] [12/10/2014 17:32:01.755] [LocalSystem-akka.actor.default-dispatcher-3] [NettyRemoteTransport(akka://LocalSystem@127.0.0.1:48437)] RemoteClientStarted@akka://HelloRemoteSystem@172.27.6.240:5150
LocalActor received message: 'hello from RemoteActor'
LocalActor received message: 'hello from RemoteActor'
LocalActor received message: 'hello from RemoteActor'
LocalActor received message: 'hello from RemoteActor'
LocalActor received message: 'hello from RemoteActor'
LocalActor received message: 'hello from RemoteActor'

遇到的坑:

1. 运行时各种各样的依赖缺失
比如:

[torstan@sparkb5-i ~/akka_example/remote_service/client]$ ./run.sh 
Exception in thread "main" java.lang.ClassNotFoundException: akka.remote.RemoteActorRefProvider
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:270)
at akka.actor.ReflectiveDynamicAccess$$anonfun$getClassFor$1.apply(DynamicAccess.scala:68)
at akka.actor.ReflectiveDynamicAccess$$anonfun$getClassFor$1.apply(DynamicAccess.scala:67)
at scala.util.Try$.apply(Try.scala:161)

google到答案是没有加载依赖文件akka-remote.jar,后来发现是没有安装akka。

2. Exception in thread "main" java.lang.NoSuchMethodException: akka.remote.RemoteActorRefProvider.<init>(java.lang.String, akka.actor.ActorSystem$Settings, akka.event.EventStream, akka.actor.Scheduler, akka.actor.DynamicAccess

google到答案:

 
12 maj 2014 kl. 22:55 skrev Liang Tang <liang...@gmail.com>:
- show quoted text -
Yes, Scala 2.10 comes with Akka 2.1.0, which is not binary compatible with Akka 2.3.2. I recommend using sbt to resolve the dependencies instead of manually constructing a command line, i.e. using the runMain task.
 
Regards,
 
Roland
https://groups.google.com/forum/#!topic/akka-user/zibfABh4cs8
 
因为安装的scala版本scala-2.10.4与akka的版本akka-2.2.4不兼容,scala-2.10.4应该与akka-2.1.4配套使用。
 
 
参考文献:
http://alvinalexander.com/scala/simple-scala-akka-actor-examples-hello-world-actors
http://alvinalexander.com/scala/simple-akka-actors-remote-example
 

最新文章

  1. python基础06 循环
  2. android模拟器停在Waiting for HOME解决方案
  3. 利用qmake生成Makefile文件
  4. Atitit 数据处理查询 中的异常标准化草案 jpa jdbc hb &#160;oql规范attilax总结
  5. Python 调用百度翻译API
  6. [原创]上海好买基金招高级Java技术经理/运维主管/高级无线客户端开发等职位(内推)
  7. iOS开发——数据持久化Swift篇&amp;通用文件存储
  8. linux下安装mysql-community后起不来
  9. oracle分配角色和表空间
  10. 流行框架(angularj基础)
  11. (转)十分钟入门pandas
  12. SQL Server CPU
  13. 传统方法过渡到ES6去优雅地实现JavaScript的继承
  14. 排查Full GC
  15. 使用SIGALARM为connect设置超时
  16. laravel数据库迁移 和 路由防攻击
  17. 解决ios兼容性问题
  18. 干货—MySQL常见的面试题+索引原理分析!
  19. 数据结构C语言版--动态顺序表的基本功能实现(二)
  20. AHK按键转载

热门文章

  1. 比较了一下基于PhoneGAP/JQ Mobile 等基于HTML5的Phone 开发框架
  2. EMV/PBOC 解析(一) 卡片文件结构
  3. USACO lamps
  4. (十)unity4.6学习Ugui中文文档-------參考-UGUI Canvas Components
  5. JBoss 系列九十九:Rest WebService jBPM 6 集成演示样例
  6. 多表关联查询(ORACLE版)
  7. Android -- Messager与Service
  8. python基础-软件目录开发规范
  9. ListControl一细节处理
  10. Topshelf