2

我正在寻找一些简单而简短的示例,如何与 tcp 套接字连接并进行交互(双向)。换句话说,如何编写一个scala 2.10 应用程序(使用akka-camelnetty库)与一个 tcp 进程(套接字)进行通信。

我在 Internet 上找到了很多文献,但一切都很旧(正在寻找 scala 2.10)和/或已弃用。

提前致谢!

4

2 回答 2

2

嗯,我正在寻找这样的东西:

1. 服务器:

import akka.actor._
import akka.camel.{ Consumer, CamelMessage }

class Ser extends Consumer {
  def endpointUri = "mina2:tcp://localhost:9002"
  def receive = {
    case message: CamelMessage => {
     //log
     println("looging, question:" + message)
     sender ! "server response to request: " + message.bodyAs[String] + ", is NO"
     }
   case _ => println("I got something else!??!!")
  }
}

object server extends App {

val system = ActorSystem("some")
val spust = system.actorOf(Props[Ser])
}

2. 客户:

 import akka.actor._
 import akka.camel._
 import akka.pattern.ask
 import scala.concurrent.duration._
 import akka.util.Timeout
 import scala.concurrent.Await

 class Producer1 extends Actor with Producer {
   def endpointUri = "mina2:tcp://localhost:9002"
  }

 object Client extends App {

 implicit val timeout = Timeout(10 seconds)
 val system2 = ActorSystem("some-system")
 val producer = system2.actorOf(Props[Producer1])
 val future = producer.ask("Hello, can I go to cinema?")

 val result = Await.result(future, timeout.duration)
 println("Is future over?="+future.isCompleted+";;result="+result)

 println("Ende!!!")
 system2.shutdown
 println("system2 ended:"+system2.isTerminated)

我知道这是http://doc.akka.io/docs/akka/2.1.0/scala/camel.html中写的所有内容并进行了详细描述。但是,如果您是新手,则需要通读几遍才能构建非常简单的客户端-服务器应用程序。我认为某种“动机示例”会非常受欢迎。

于 2013-01-26T11:36:35.207 回答
1

我假设您已经检查了 Akka 文档?http://doc.akka.io/docs/akka/2.1.0/scala/camel.html

在 Akka 2.1.0 中,他们改进了 akka-camel 模块,因此它完全是最新的(之前有点过时了)。

还有一个骆驼-akka 视频演示,涵盖了一个真实的用例:http ://www.davsclaus.com/2012/04/great-akka-and-camel-presentation-video.html

于 2013-01-24T08:06:55.613 回答