Consumer Actors section

This commit is contained in:
Piotr Gabryanczyk 2012-05-02 21:13:52 +01:00
parent b64d7b81e8
commit 5d410ce1d1
3 changed files with 87 additions and 8 deletions

View file

@ -74,8 +74,8 @@ Camel endpoints.
.. includecode:: code/akka/docs/camel/Introduction.scala .. includecode:: code/akka/docs/camel/Introduction.scala
:include: imports,Producer :include: imports,Producer
In the above example, any message sent to this actor will be added (produced) to In the above example, any message sent to this actor will be sent to
the example JMS queue. Producer actors may choose from the same set of Camel the JMS queue ``orders``. Producer actors may choose from the same set of Camel
components as Consumer actors do. components as Consumer actors do.
CamelMessage CamelMessage
@ -113,3 +113,46 @@ Maven
<artifactId>akka-camel</artifactId> <artifactId>akka-camel</artifactId>
<version>2.1-SNAPSHOT</version> <version>2.1-SNAPSHOT</version>
</dependency> </dependency>
.. _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

View file

@ -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
}
}

View file

@ -1,11 +1,11 @@
package akka.docs.camel package akka.docs.camel
object wrapper { object Introduction {
{ {
//#Consumer-mina //#Consumer-mina
import akka.camel.{ CamelMessage, Consumer } import akka.camel.{ CamelMessage, Consumer }
class MyActor extends Consumer { class MinaClient extends Consumer {
def endpointUri = "mina:tcp://localhost:6200?textline=true" def endpointUri = "mina:tcp://localhost:6200?textline=true"
def receive = { def receive = {
@ -18,14 +18,14 @@ object wrapper {
import akka.actor.{ ActorSystem, Props } import akka.actor.{ ActorSystem, Props }
val sys = ActorSystem("camel") val sys = ActorSystem("camel")
val myActor = sys.actorOf(Props[MyActor]) val mina = sys.actorOf(Props[MinaClient])
//#Consumer-mina //#Consumer-mina
} }
{ {
//#Consumer //#Consumer
import akka.camel.{ CamelMessage, Consumer } import akka.camel.{ CamelMessage, Consumer }
class MyActor extends Consumer { class JettyAdapter extends Consumer {
def endpointUri = "jetty:http://localhost:8877/example" def endpointUri = "jetty:http://localhost:8877/example"
def receive = { def receive = {
@ -39,10 +39,16 @@ object wrapper {
//#Producer //#Producer
import akka.actor.Actor import akka.actor.Actor
import akka.camel.{ Producer, Oneway } import akka.camel.{ Producer, Oneway }
import akka.actor.{ ActorSystem, Props }
class MyActor extends Actor with Producer with Oneway { class Orders extends Actor with Producer with Oneway {
def endpointUri = "jms:queue:example" def endpointUri = "jms:queue:Orders"
} }
val sys = ActorSystem("camel")
val orders = sys.actorOf(Props[Orders])
orders ! <order amount="100" currency="PLN" itemId="12345"/>
//#Producer //#Producer
} }
} }