You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2023/03/06 12:52:13 UTC

[camel] branch main updated: CAMEL-19119: camel-jdbc - Add missing example to docs

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new fea84e1d25f CAMEL-19119: camel-jdbc - Add missing example to docs
fea84e1d25f is described below

commit fea84e1d25f4d00e4a9a0c0763563668b1f9a815
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon Mar 6 13:51:41 2023 +0100

    CAMEL-19119: camel-jdbc - Add missing example to docs
---
 .../camel-jdbc/src/main/docs/jdbc-component.adoc   | 99 +++++-----------------
 1 file changed, 23 insertions(+), 76 deletions(-)

diff --git a/components/camel-jdbc/src/main/docs/jdbc-component.adoc b/components/camel-jdbc/src/main/docs/jdbc-component.adoc
index 925341273a7..becb557e91a 100644
--- a/components/camel-jdbc/src/main/docs/jdbc-component.adoc
+++ b/components/camel-jdbc/src/main/docs/jdbc-component.adoc
@@ -107,95 +107,42 @@ map on the headers with the key `CamelJdbcParameters`.
 
 == Samples
 
-In the following example, we fetch the rows from the customer table.
-
+In the following example, we setup the DataSource that camel-jdbc requires.
 First we register our datasource in the Camel registry as `testdb`:
 
+[source,java]
+----
+EmbeddedDatabase db = new EmbeddedDatabaseBuilder()
+      .setType(EmbeddedDatabaseType.DERBY).addScript("sql/init.sql").build();
+
+CamelContext context = ...
+context.getRegistry().bind("testdb", db);
+----
+
 Then we configure a route that routes to the JDBC component, so the SQL
 will be executed. Note how we refer to the `testdb` datasource that was
 bound in the previous step:
 
-Or you can create a `DataSource` in Spring like this:
-
-With spring xml:
-
-[source,xml]
+[source,java]
 ----
-<?xml version="1.0" encoding="UTF-8"?>
-<beans xmlns="http://www.springframework.org/schema/beans"
-       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-       xmlns:jdbc="http://www.springframework.org/schema/jdbc"
-       xsi:schemaLocation="http://www.springframework.org/schema/beans                                                                                                                                                                                                                                                         
-                           http://www.springframework.org/schema/beans/spring-beans.xsd                                                                                                                                                                                                                                        
-                           http://camel.apache.org/schema/spring                                                                                                                                                                                                                                                               
-                           http://camel.apache.org/schema/spring/camel-spring.xsd                                                                                                                                                                                                                                              
-                           http://www.springframework.org/schema/jdbc                                                                                                                                                                                                                                                          
-                           http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd                                                                                                                                                                                                                                      
-                           http://activemq.apache.org/schema/core                                                                                                                                                                                                                                                              
-                           http://activemq.apache.org/schema/core/activemq-core.xsd">
-
-  <!--  PROPERTY PLACEHOLDERS  -->
-  <!--                                                                                                                                                                                                                                                                                                                         
-       You may use property placeholders data store configuration                                                                                                                                                                                                                                                              
-       for to do that, you can resolve this properties with srping                                                                                                                                                                                                                                                             
-  -->
-  <bean id="db" class="org.apache.camel.spring.spi.BridgePropertyPlaceholderConfigurer">
-    <property name="location" value="classpath:db.properties"/>
-  </bean>
-
-  <!-- DATA STORE CONFIGURATION -->
-
-  <!--                                                                                                                                                                                                                                                                                                                         
-       In this example the DataStore is a postgres database, you can change the jdbc connector.                                                                                                                                                                                                                                
-       Also, the property testWhileIdle and validationQuery keep connection open.                                                                                                                                                                                                                                              
-  -->                                                                                                                                                                                                                                                                                                                          
-  
-  <bean id="DataStore" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
-    <property name="driverClassName" value="org.postgresql.Driver" />
-    <property name="url" value="jdbc:postgresql://${DB_HOST}/${DB_NAME}"/>
-    <property name="username" value="${DB_USER}" />
-    <property name="password" value="${DB_PASS}" />
-    <property name="initialSize" value="4"/>
-    <property name="maxActive" value="15"/>
-    <property name="maxIdle" value="16"/>
-    <property name="minIdle" value="8"/>
-    <property name="timeBetweenEvictionRunsMillis" value="1800"/>
-    <property name="minEvictableIdleTimeMillis" value="1800"/>
-    <property name="testOnBorrow" value="true"/>
-    <property name="testWhileIdle" value="true"/>
-    <property name="testOnReturn" value="true"/>
-    <property name="validationQuery" value="SELECT 1"/>
-    <property name="maxWait"  value="1000"/>
-    <property name="removeAbandoned" value="true"/>
-    <property name="logAbandoned" value="true"/>
-    <property name="removeAbandonedTimeout" value="30000"/>
-  </bean>
-
-  <!-- You shuould write a file with an script to initialize your database.                                                                                                                                                                                                                                                    
-  If you have nothing to do with your database, then you can write just a                                                                                                                                                                                                                                                      
-  validation query like 'SELECT *; '-->
-  <jdbc:initialize-database data-source="DataStore" enabled="true">
-    <jdbc:script location="classpath:datastore-schema.sql" />
-  </jdbc:initialize-database>
-
-  <camelContext xmlns="http://camel.apache.org/schema/spring">
-
-    <!-- CAMEL ROUTE -->
-    <route id="Reporter">
-      <from uri="direct:to-datastore"/>
-      <!-- this example is done with camel-jdbc but you can make it work with camel-sql -->
-      <setBody>
-        <simple>insert into test '${body[value]}'</simple>
-      </setBody>
-      <to uri="jdbc:DataStore"/>
-    </route>
-</camelContext>
+from("direct:hello")
+    .to("jdbc:testdb");
 ----
 
 We create an endpoint, add the SQL query to the body of the IN message,
 and then send the exchange. The result of the query is returned in the
 OUT body:
 
+[source,java]
+----
+Endpoint endpoint = context.getEndpoint("direct:hello");
+Exchange exchange = endpoint.createExchange();
+// then we set the SQL on the in body
+exchange.getMessage().setBody("select * from customer order by ID");
+// now we send the exchange to the endpoint, and receives the response from Camel
+Exchange out = template.send(endpoint, exchange);
+----
+
 If you want to work on the rows one by one instead of the entire
 ResultSet at once you need to use the Splitter EIP
 such as: