刚刚开始接触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
 

akka简单示例-1的更多相关文章

  1. akka简单示例-2

    手动敲了一遍计算pi的示例:http://www.gtan.com/akka_doc/intro/getting-started-first-scala.html 有个笔误,花了半个小时定位. [To ...

  2. AKKA HTTP 简单示例

    AKKA HTTP 简单示例 依赖包: compile("com.typesafe.akka:akka-http_2.13:10.1.8") compile("com.t ...

  3. Linux下的C Socket编程 -- server端的简单示例

    Linux下的C Socket编程(三) server端的简单示例 经过前面的client端的学习,我们已经知道了如何创建socket,所以接下来就是去绑定他到具体的一个端口上面去. 绑定socket ...

  4. C# 构建XML(简单示例)

    C# 构建XML的简单示例: var pars = new Dictionary<string, string> { {"url","https://www. ...

  5. 根据juery CSS点击一个标签弹出一个遮罩层的简单示例

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  6. ACEXML解析XML文件——简单示例程序

    掌握了ACMXML库解析XML文件的方法后,下面来实现一个比较完整的程序. 定义基本结构 xml文件格式如下 <?xml version="1.0"?> <roo ...

  7. demo工程的清单文件及activity中api代码简单示例

    第一步注册一个账户,并创建一个应用.获取app ID与 app Key. 第二步下载sdk 第三步新建工程,修改清单文件,导入相关的sdk文件及调用相应的api搞定. 3.1 修改清单文件,主要是加入 ...

  8. spring-servlet.xml简单示例

    spring-servlet.xml简单示例 某个项目中的spring-servlet.xml 记下来以后研究用 <!-- springMVC简单配置 --> <?xml versi ...

  9. SignalR 简单示例

    一.什么是 SignalR ASP.NET SignalR is a library for ASP.NET developers that simplifies the process of add ...

随机推荐

  1. org.apache.struts.chain.commands.InvalidPathException: No action config found for the specified url.

    No action config found for the specified url url路径下找不到action,原因是stuts-config.xml文件配置错误. demo的项目文件如下: ...

  2. static关键字使用

    static关键字对一些公共使用的数据,比如是饭店筷子,不可能说每个人去饭店吃饭的时候自带一双筷子,饭店的筷子对于进店吃饭的对象都是公共的; 静态变量,跟其他变量不同的是,它是与类关联的,其他变量则是 ...

  3. File类学习笔记

    File类 首先,要明确的一点就是,在整个IO包中,唯一表示与文件有关的类局势File类. 它可以实现创建或删除文件等操作.下面看看它的构造方法: File(String pathname) 通过将给 ...

  4. 什么是IP地址、子网掩码、路由和网关

    什么是IP地址.子网掩码.路由和网关?经常有朋友问我,的确这些术语常常被我们看到,今天就给大伙说说这几个术语的意思: 1.IP地址: IP地址有一个32位的连接地址,由4个8位字段组成,8位字段称为8 ...

  5. packets

    packets   时间限制(普通/Java):1000MS/10000MS     运行内存限制:65536KByte 总提交: 27            测试通过: 14 描述 A factor ...

  6. [RxJS] Creation operators: empty, never, throw

    This lesson introduces operators empty(), never(), and throw(), which despite being plain and void o ...

  7. android PreferenceScreen使用笔记

    preference.xml <?xml version="1.0" encoding="utf-8"?> <PreferenceScreen ...

  8. linux命令chown修改文件所有权

      Changing User Ownership To apply appropriate permissions, the first thing to consider is ownership ...

  9. 逆拓扑排序 HDU2647Reward

    这个题如果用邻接矩阵的话,由于n比较大,会超内存,所以选用邻接表的形式.还有就是这个题有那个等级的问题,一级比一级的福利高,所以不能直接拓扑排序,而是反过来,计算出度,找出度为0的顶点,然后更新出度数 ...

  10. Remoting 的“传递的引用”理解

    WCf是集大成者,具有其他微软的很多技术,其中分布式上很多借助于Remoting,所以研究一下Remoting有助于理解WCF 提到Remoting就不得不涉及到MarshalByRefObject这 ...