2012-06-26 10:43:31 +02:00
|
|
|
package docs.camel
|
|
|
|
|
|
|
|
|
|
object HttpExample {
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
//#HttpExample
|
|
|
|
|
import org.apache.camel.Exchange
|
2012-07-12 15:00:11 +02:00
|
|
|
import akka.actor.{ Actor, ActorRef, Props, ActorSystem }
|
|
|
|
|
import akka.camel.{ Producer, CamelMessage, Consumer }
|
2012-06-26 10:43:31 +02:00
|
|
|
import akka.actor.Status.Failure
|
|
|
|
|
|
|
|
|
|
class HttpConsumer(producer: ActorRef) extends Consumer {
|
|
|
|
|
def endpointUri = "jetty:http://0.0.0.0:8875/"
|
|
|
|
|
|
|
|
|
|
def receive = {
|
2012-07-12 15:00:11 +02:00
|
|
|
case msg ⇒ producer forward msg
|
2012-06-26 10:43:31 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class HttpProducer(transformer: ActorRef) extends Actor with Producer {
|
|
|
|
|
def endpointUri = "jetty://http://akka.io/?bridgeEndpoint=true"
|
|
|
|
|
|
|
|
|
|
override def transformOutgoingMessage(msg: Any) = msg match {
|
2012-10-01 20:35:19 +02:00
|
|
|
case msg: CamelMessage ⇒ msg.copy(headers = msg.headers ++
|
|
|
|
|
msg.headers(Set(Exchange.HTTP_PATH)))
|
2012-06-26 10:43:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override def routeResponse(msg: Any) { transformer forward msg }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class HttpTransformer extends Actor {
|
|
|
|
|
def receive = {
|
2012-10-01 20:35:19 +02:00
|
|
|
case msg: CamelMessage ⇒
|
|
|
|
|
sender ! (msg.mapBody { body: Array[Byte] ⇒
|
|
|
|
|
new String(body).replaceAll("Akka ", "AKKA ")
|
|
|
|
|
})
|
|
|
|
|
case msg: Failure ⇒ sender ! msg
|
2012-06-26 10:43:31 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create the actors. this can be done in a Boot class so you can
|
2012-10-01 20:35:19 +02:00
|
|
|
// run the example in the MicroKernel. just add the three lines below to
|
|
|
|
|
// your boot class.
|
2012-06-26 10:43:31 +02:00
|
|
|
val system = ActorSystem("some-system")
|
|
|
|
|
val httpTransformer = system.actorOf(Props[HttpTransformer])
|
|
|
|
|
val httpProducer = system.actorOf(Props(new HttpProducer(httpTransformer)))
|
|
|
|
|
val httpConsumer = system.actorOf(Props(new HttpConsumer(httpProducer)))
|
|
|
|
|
//#HttpExample
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|