Typesafe Activator

Akka Camel Samples with Java

Akka Camel Samples with Java

Akka Team
Source
August 13, 2014
akka camel java sample

Akka Camel Samples with Java

How to get "Akka Camel Samples with Java" on your computer

There are several ways to get this template.

Option 1: Choose akka-sample-camel-java in the Typesafe Activator UI.

Already have Typesafe Activator (get it here)? Launch the UI then search for akka-sample-camel-java in the list of templates.

Option 2: Download the akka-sample-camel-java project as a zip archive

If you haven't installed Activator, you can get the code by downloading the template bundle for akka-sample-camel-java.

  1. Download the Template Bundle for "Akka Camel Samples with Java"
  2. Extract the downloaded zip file to your system
  3. The bundle includes a small bootstrap script that can start Activator. To start Typesafe Activator's UI:

    In your File Explorer, navigate into the directory that the template was extracted to, right-click on the file named "activator.bat", then select "Open", and if prompted with a warning, click to continue:

    Or from a command line:

     C:\Users\typesafe\akka-sample-camel-java> activator ui 
    This will start Typesafe Activator and open this template in your browser.

Option 3: Create a akka-sample-camel-java project from the command line

If you have Typesafe Activator, use its command line mode to create a new project from this template. Type activator new PROJECTNAME akka-sample-camel-java on the command line.

Option 4: View the template source

The creator of this template maintains it at https://github.com/akka/akka.

Option 5: Preview the tutorial below

We've included the text of this template's tutorial below, but it may work better if you view it inside Activator on your computer. Activator tutorials are often designed to be interactive.

Preview the tutorial

This tutorial contains 3 samples of Akka Camel.

  • Asynchronous routing and transformation
  • Custom Camel route
  • Quartz scheduler

Asynchronous routing and transformation

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 HttpConsumeractor 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.

Custom Camel route example

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.

  • A consumer actor receives a message from an HTTP client
  • It forwards the message to another actor that transforms the message (encloses the original message into hyphens)
  • The transformer actor forwards the transformed message to a producer actor
  • The producer actor sends the message to a custom Camel route beginning at the direct:welcome endpoint
  • A processor (transformer) in the custom Camel route prepends "Welcome" to the original message and creates a result message
  • The producer actor sends the result back to the consumer actor which returns it to the HTTP client

The 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 -

Quartz Scheduler Example

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

comments powered by Disqus