scala 2.10,akka-camel TCP套接字通信

标签 scala apache-camel akka

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

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

提前致谢!

最佳答案

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

<强>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 中已详细描述了所有内容。 。但如果您是新手,则需要多次阅读全文才能构建非常简单的客户端-服务器应用程序。我认为某种“动机例子”会更受欢迎。

关于scala 2.10,akka-camel TCP套接字通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14484096/

相关文章:

scala - Akka DistributedPubSubMediator at-least-once delivery guarantees for publishing to a topic

scala - 如何使用 SBT 0.10+ 构建 OSGi 包?

java - HashMap 与 Camel cxf :component POJO dataFormat

scala - 如何在 sbt 测试中覆盖 application.conf 中的日志级别?

scala - 使用密封特征和密封抽象类作为基类之间的区别

performance - `rdd.map(x => f(g(x))` 是否比 `rdd.map(g).map(f)` 具有更好的性能?

json - 如何将由 String 和 Seq[String] 值组成的 Scala Map 转换为 JSON?

java - Apache Camel FTP 错误

java - Apache Camel 中使用 beanref 代替 processref 的优点

java - 如何获取给定参与者系统的家庭地址