Skip to content

Commit

Permalink
New example for using SQL component
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.apache.org/repos/asf/camel/trunk@1435060 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
davsclaus committed Jan 18, 2013
1 parent 8849c4d commit f05bf4b
Show file tree
Hide file tree
Showing 10 changed files with 574 additions and 0 deletions.
35 changes: 35 additions & 0 deletions examples/camel-example-sql/README.txt
@@ -0,0 +1,35 @@
Camel SQL Example
=================

This example shows how to exchange data using a shared database table.

The example has two Camel routes. The first route insert new data into the table,
triggered by a timer to run every 5th second.

The second route pickup the newly inserted rows from the table,
process the row(s), and mark the row(s) as processed when done;
to avoid picking up the same rows again.

You will need to compile this example first:
mvn compile

To run the example type
mvn camel:run

To stop the example hit ctrl + c

This example uses Spring to setup and configure the database,
as well the CamelContext. You can see this in the following file:
In the src/main/resources/META-INF/spring/camel-context.xml

This example is documented at
http://camel.apache.org/sql-example.html

If you hit any problems please talk to us on the Camel Forums
http://camel.apache.org/discussion-forums.html

Please help us make Apache Camel better - we appreciate any feedback you
may have. Enjoy!

------------------------
The Camel riders!
88 changes: 88 additions & 0 deletions examples/camel-example-sql/pom.xml
@@ -0,0 +1,88 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.apache.camel</groupId>
<artifactId>examples</artifactId>
<version>2.11-SNAPSHOT</version>
</parent>

<artifactId>camel-example-sql</artifactId>
<packaging>bundle</packaging>
<name>Camel :: Example :: SQL</name>
<description>An example for showing Camel using SQL component</description>

<properties>
<camel.osgi.export.pkg>org.apache.camel.example.sql.*</camel.osgi.export.pkg>
</properties>

<dependencies>

<!-- Camel -->
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-sql</artifactId>
</dependency>

<!-- jdbc -->
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
</dependency>

<!-- logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</dependency>

</dependencies>

<build>
<plugins>
<!-- Allows the routes to be run via 'mvn camel:run' -->
<plugin>
<groupId>org.apache.camel</groupId>
<artifactId>camel-maven-plugin</artifactId>
<version>${project.version}</version>
</plugin>
</plugins>
</build>

</project>
@@ -0,0 +1,58 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.example.sql;

import javax.sql.DataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;

/**
* Bean that creates the database table
*/
public class DatabaseBean {

private static final Logger LOG = LoggerFactory.getLogger(DatabaseBean.class);
private DataSource dataSource;

public DataSource getDataSource() {
return dataSource;
}

public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}

public void create() throws Exception {
JdbcTemplate jdbc = new JdbcTemplate(dataSource);

String sql = "create table orders (\n" +
" id integer primary key,\n" +
" item varchar(10),\n" +
" amount varchar(5),\n" +
" description varchar(30),\n" +
" processed boolean\n" +
")";

LOG.info("Creating table orders ...");

jdbc.execute(sql);

LOG.info("... created table orders");
}
}
@@ -0,0 +1,52 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.example.sql;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
* Bean that generates and process orders.
*/
public class OrderBean {

private int counter;
private Random ran = new Random();

/**
* Generates a new order structured as a {@link Map}
*/
public Map generateOrder() {
Map<String, Object> answer = new HashMap<String, Object>();
answer.put("id", counter++);
answer.put("item", counter % 2 == 0 ? 111 : 222);
answer.put("amount", ran.nextInt(10) + 1);
answer.put("description", counter % 2 == 0 ? "Camel in Action" : "ActiveMQ in Action");
return answer;
}

/**
* Processes the order
*
* @param data the order as a {@link Map}
* @return the transformed order
*/
public String processOrder(Map data) {
return "Processed order id " + data.get("id") + " item " + data.get("item") + " of " + data.get("amount") + " copies of " + data.get("description");
}
}

0 comments on commit f05bf4b

Please sign in to comment.