From 5d410ce1d1bb64d31cac123decce3072d3189bcb Mon Sep 17 00:00:00 2001 From: Piotr Gabryanczyk Date: Wed, 2 May 2012 21:13:52 +0100 Subject: [PATCH] Consumer Actors section --- akka-docs/scala/camel.rst | 47 ++++++++++++++++++- .../code/akka/docs/camel/Consumers.scala | 30 ++++++++++++ .../code/akka/docs/camel/Introduction.scala | 18 ++++--- 3 files changed, 87 insertions(+), 8 deletions(-) create mode 100644 akka-docs/scala/code/akka/docs/camel/Consumers.scala diff --git a/akka-docs/scala/camel.rst b/akka-docs/scala/camel.rst index 6a3713d46b..784ff50074 100644 --- a/akka-docs/scala/camel.rst +++ b/akka-docs/scala/camel.rst @@ -74,8 +74,8 @@ Camel endpoints. .. includecode:: code/akka/docs/camel/Introduction.scala :include: imports,Producer -In the above example, any message sent to this actor will be added (produced) to -the example JMS queue. Producer actors may choose from the same set of Camel +In the above example, any message sent to this actor will be sent to +the JMS queue ``orders``. Producer actors may choose from the same set of Camel components as Consumer actors do. CamelMessage @@ -113,3 +113,46 @@ Maven akka-camel 2.1-SNAPSHOT + +.. _camel-consumer-actors: + + +Consumer Actors +================ + +For objects to receive messages, they must mixin the `Consumer`_ +trait. For example, the following actor class (Consumer1) implements the +endpointUri method, which is declared in the Consumer trait, in order to receive +messages from the ``file:data/input/actor`` Camel endpoint. + +.. _Consumer: http://github.com/akka/akka/blob/master/akka-camel/src/main/scala/akka/camel/Consumer.scala + +.. includecode:: code/akka/docs/camel/Consumers.scala#Consumer1 + +Whenever a file is put into the data/input/actor directory, its content is +picked up by the Camel `file component`_ and sent as message to the +actor. Messages consumed by actors from Camel endpoints are of type +`CamelMessage`_. These are immutable representations of Camel messages. + +.. _file component: http://camel.apache.org/file2.html +.. _Message: http://github.com/akka/akka/blob/master/akka-camel/src/main/scala/akka/camel/CamelMessage.scala + + +Here's another example that sets the endpointUri to +``jetty:http://localhost:8877/camel/default``. It causes Camel's `Jetty +component`_ to start an embedded `Jetty`_ server, accepting HTTP connections +from localhost on port 8877. + +.. _Jetty component: http://camel.apache.org/jetty.html +.. _Jetty: http://www.eclipse.org/jetty/ + +.. includecode:: code/akka/docs/camel/Consumers.scala#Consumer2 + +After starting the actor, clients can send messages to that actor by POSTing to +``http://localhost:8877/camel/default``. The actor sends a response by using the +self.reply method (Scala). For returning a message body and headers to the HTTP +client the response type should be `Message`_. For any other response type, a +new Message object is created by akka-camel with the actor response as message +body. + +.. _Message: http://github.com/akka/akka/blob/master/akka-camel/src/main/scala/akka/camel/CamelMessage.scala diff --git a/akka-docs/scala/code/akka/docs/camel/Consumers.scala b/akka-docs/scala/code/akka/docs/camel/Consumers.scala new file mode 100644 index 0000000000..87b13aa601 --- /dev/null +++ b/akka-docs/scala/code/akka/docs/camel/Consumers.scala @@ -0,0 +1,30 @@ +package akka.docs.camel + +object Consumers { + { + //#Consumer1 + import akka.camel.{CamelMessage, Consumer} + + class Consumer1 extends Consumer { + def endpointUri = "file:data/input/actor" + + def receive = { + case msg: CamelMessage => println("received %s" format msg.bodyAs[String]) + } + } + //#Consumer1 + } + { + //#Consumer2 + import akka.camel.{CamelMessage, Consumer} + + class Consumer2 extends Consumer { + def endpointUri = "jetty:http://localhost:8877/camel/default" + + def receive = { + case msg: CamelMessage => sender ! ("Hello %s" format msg.bodyAs[String]) + } + } + //#Consumer2 + } +} \ No newline at end of file diff --git a/akka-docs/scala/code/akka/docs/camel/Introduction.scala b/akka-docs/scala/code/akka/docs/camel/Introduction.scala index 38546c9f41..4899843a27 100644 --- a/akka-docs/scala/code/akka/docs/camel/Introduction.scala +++ b/akka-docs/scala/code/akka/docs/camel/Introduction.scala @@ -1,11 +1,11 @@ package akka.docs.camel -object wrapper { +object Introduction { { //#Consumer-mina import akka.camel.{ CamelMessage, Consumer } - class MyActor extends Consumer { + class MinaClient extends Consumer { def endpointUri = "mina:tcp://localhost:6200?textline=true" def receive = { @@ -18,14 +18,14 @@ object wrapper { import akka.actor.{ ActorSystem, Props } val sys = ActorSystem("camel") - val myActor = sys.actorOf(Props[MyActor]) + val mina = sys.actorOf(Props[MinaClient]) //#Consumer-mina } { //#Consumer import akka.camel.{ CamelMessage, Consumer } - class MyActor extends Consumer { + class JettyAdapter extends Consumer { def endpointUri = "jetty:http://localhost:8877/example" def receive = { @@ -39,10 +39,16 @@ object wrapper { //#Producer import akka.actor.Actor import akka.camel.{ Producer, Oneway } + import akka.actor.{ ActorSystem, Props } - class MyActor extends Actor with Producer with Oneway { - def endpointUri = "jms:queue:example" + class Orders extends Actor with Producer with Oneway { + def endpointUri = "jms:queue:Orders" } + + val sys = ActorSystem("camel") + val orders = sys.actorOf(Props[Orders]) + + orders ! //#Producer } } \ No newline at end of file