xhudik
xhudik

Reputation: 2442

scala 2.10, akka-camel tcp socket communication

I'm looking for some easy and short example how to connect and make interaction (two-ways) with tcp socket. In other words, how to write a scala 2.10 application (using akka-camel or netty library) to communicate with a tcp process (socket).

I found a lot of literature on Internet, but everything was old (looking for scala 2.10) and/or deprecated.

Thanks in advance!

Upvotes: 2

Views: 1110

Answers (2)

xhudik
xhudik

Reputation: 2442

hmm I was looking for something like this:

1. server:

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. Client:

 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)

I know it is everything written and well-described in http://doc.akka.io/docs/akka/2.1.0/scala/camel.html. But if you are novice you need to read it all and several times in order to build very simple client-server application. I think some kind of "motivation example" would be more than welcome.

Upvotes: 2

Claus Ibsen
Claus Ibsen

Reputation: 55540

I assume you have checked the Akka documentation? http://doc.akka.io/docs/akka/2.1.0/scala/camel.html

In Akka 2.1.0 they have improved the akka-camel module so its fully up to date (was a bit outdated before).

There is also a camel-akka video presentation, covering a real-life use case: http://www.davsclaus.com/2012/04/great-akka-and-camel-presentation-video.html

Upvotes: 1

Related Questions