This Confluence has been LDAP enabled, if you are an ASF Committer, please use your LDAP Credentials to login. Any problems file an INFRA jira ticket please.

Child pages
  • Tutorial for Camel on Google App Engine
Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 5 Next »

Camel on Google App Engine Tutorial

This tutorial will be updated whenever new features are added to the Camel Components for Google App Engine.


Goal of this tutorial is to get a non-trivial Camel application running on Google App Engine (GAE). For developing that application the Camel Components for Google App Engine will be used. The application allows the user to enter the name of a city and an email adsress in an HTML form. The form data are submitted by the user to a Camel application, running on GAE, which retrieves weather data for the given city. It then sends a weather report to the user-defined email address. The following figure gives an overview.

POSTed form data are dispatched to the Camel application via the ghttp component. Then the application queues the message with the gtask component for further background processing. This includes retrieval of weather data from the Google Weather Service, transforming the data to generate a report and sending that report by email via the gmail component.



To deploy the example application, first checkout the sources with

svn co camel-example-gae

Open the camel-example-gae/src/main/webapp/WEB-INF/application-web.xml file in an editor and replace the template application name replaceme with the name of the application that you created in the previous section.

<?xml version="1.0" encoding="utf-8"?>
<appengine-web-app xmlns="">
    <!-- use your own application name here -->
    <!-- Configure java.util.logging -->
        <property name="java.util.logging.config.file" value="WEB-INF/"/>

Open camel-example-gae/src/main/resources/context.xml file and change the sender property value to the email address of your Google App Engine account.

<beans xmlns=""

    <bean id="tutorialRouteBuilder"
        <!-- use your own GAE admin email address here -->
        <property name="sender" value="" />

Then go to the camel-example-gae directory and enter

mvn install

This will create the application war file in the target directory. Finally use the appcfg command-line tool of the App Engine SDK to deploy the application.

appcfg update target/camel-example-gae-<version>

where version needs to be replaced with the version of Camel you're using. You will be prompted for the email address and password of your Google App Engine account. After deployment the example application is ready to use.

Local development server

Please note that on the local development server, sending emails does not work and queued tasks must be executed manually from the developer console.


Go to where replaceme must be replaced with the application name you entered in appengine-web.xml. The following form should now appear.

Enter the name of a city and your email address, for example:

Then press Submit. The immediate response is

Weather report will be sent to

In the background, the current weather conditions for the user-defined city will be obtained from the Google weather service and a formatted weather report will be send by email. Submitting the form the first time initializes the application on Google App Engine which can take several seconds. Subsequent submissions are served much faster. Check your emails and you should now see a new email with subject Wheather report and content similar to this one:

Weather report for:  London, England
Current condition:   Klar
Current temperature: 12 (Celsius) 

The report is partly internationalized, depending on the language settings of your browser.

Route builder walkthrough
package org.apache.camel.example.gae;

import org.apache.camel.Exchange;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.gae.mail.GMailBinding;

public class TutorialRouteBuilder extends RouteBuilder {

    private String sender;
    public void setSender(String sender) {
        this.sender = sender;
    public void configure() throws Exception {
            .setHeader(Exchange.CONTENT_TYPE, constant("text/plain"))
            .transform(constant("Weather report will be sent to ").append(header("mailto")));
            .setHeader(Exchange.HTTP_QUERY, constant("weather=").append(header("city")))
            .process(new WeatherProcessor())        
            .setHeader(GMailBinding.GMAIL_SUBJECT, constant("Weather report"))
            .setHeader(GMailBinding.GMAIL_TO, header("mailto"))
            .to("gmail://" + sender);


Form data are received via the ghttp component. After receiving the request it is added to the default queue for background processing. Queueing messages on GAE is done with the gtask component. After queueing the request a response is generated for being displayed in the browser. The value of the mailto header is the email address the user entered in the form.

Background processing of the queued messages starts from("gtask://default"). The first step is the construction of the Google weather service URL followed by the weather service invocation using the ghttp component. For example, if the user entered London in the city field of the form the resulting URL is The resulting XML data are processed by the WeatherProcessor:
package org.apache.camel.example.gae;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathFactory;

import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.w3c.dom.Document;

public class WeatherProcessor implements Processor {

    public void process(Exchange exchange) throws Exception {
        // convert XML body to DOM tree
        Document doc = exchange.getIn().getBody(Document.class);

        XPathFactory xpfactory = XPathFactory.newInstance();
        XPath xpath = xpfactory.newXPath();

        // Extract result values via XPath
        String city = xpath.evaluate("//forecast_information/city/@data", doc);
        String cond = xpath.evaluate("//current_conditions/condition/@data", doc);
        String temp = xpath.evaluate("//current_conditions/temp_c/@data", doc);

        String msg = null;
        if (city != null && city.length() > 0) {
            msg = new StringBuffer()
                .append("\n").append("Weather report for:  ").append(city)
                .append("\n").append("Current condition:   ").append(cond)
                .append("\n").append("Current temperature: ").append(temp).append(" (Celsius)").toString();
        } else {
            // create an error message
            msg = "Error getting weather report for " + exchange.getIn().getHeader("city", String.class);


This processor extracts data from the XML result with XPath expressions and creates a simple text-based report for being sent by email. For sending the email the gmail component is used. The recipient is derived from the user-defined mailto header value.

  • No labels