This tutorial contains 3 samples of Akka Camel.
This example demonstrates how to implement consumer and producer actors that support Asynchronous routing with their Camel endpoints. The sample application transforms the content of the Akka homepage, http://akka.io, by replacing every occurrence of *Akka* with *AKKA*.
To run this example, go to the Run
tab, and start the application main class sample.camel.http.HttpExample
if it's not already started.
Then direct the browser to http://localhost:8875 and the
transformed Akka homepage should be displayed. Please note that this example will probably not work if you're
behind an HTTP proxy.
The following figure gives an overview how the example actors interact with
external systems and with each other. A browser sends a GET request to
http://localhost:8875 which is the published endpoint of the
HttpConsumer
actor. The HttpConsumer
actor forwards the requests to the
HttpProducer.java
actor which retrieves the Akka homepage from http://akka.io. The retrieved HTML
is then forwarded to the
HttpTransformer.java
actor which replaces all occurrences of *Akka* with *AKKA*. The transformation result is sent back the HttpConsumer
which finally returns it to the browser.
Implementing the example actor classes and wiring them together is rather easy as shown in HttpConsumer.java, HttpProducer.java and HttpTransformer.java.
The jetty endpoints of HttpConsumer and HttpProducer support asynchronous in-out message exchanges and do not allocate threads for the full duration of the exchange. This is achieved by using Jetty continuations on the consumer-side and by using Jetty's asynchronous HTTP client on the producer side. The following high-level sequence diagram illustrates that.
This section also demonstrates the combined usage of a RouteProducer and a RouteConsumer actor as well as the inclusion of a custom Camel route. The following figure gives an overview.
direct:welcome
endpointThe producer actor knows where to reply the message to because the consumer and transformer actors have forwarded the original sender reference as well. The application configuration and the route starting from direct:welcome are done in the code above.
To run this example, go to the Run
tab, and start the application main class sample.camel.route.CustomRouteExample
POST a message to http://localhost:8877/camel/welcome
.
curl -H "Content-Type: text/plain" -d "Anke" http://localhost:8877/camel/welcome
The response should be:
Welcome - Anke -
Here is an example showing how simple it is to implement a cron-style scheduler by using the Camel Quartz component in Akka.
Open MyQuartzActor.java.
The example creates a "timer" actor which fires a message every 2 seconds.
For more information about the Camel Quartz component, see here: http://camel.apache.org/quartz.html