lburgazzoli edited a comment on issue #892: Defining datasource in groovy and 
using in XML routes
URL: https://github.com/apache/camel-k/issues/892#issuecomment-516997148
 
 
   > Hello !
   > 
   > Just need a quick help here. Bear with me as am not a groovy expert yet.
   > Since Camel k doesn't understand .. fragment, I tried adding a data-source 
bean via groovy script as follows
   
   This is wrong, camel-k-runtime supports what Camel does but as the runtime 
is not based on spring-boot or blueprint, you cannot define beans in the XML. 
   
   > 
   > ```groovy
   > import org.apache.commons.dbcp.BasicDataSource;
   > 
   > context {
   >     registry {
   >            mysqlDataSource = {
   >            org.apache.commons.dbcp.BasicDataSource mysqlDataSource =  new 
org.apache.commons.dbcp.BasicDataSource()
   >         mysqlDataSource.url = 'jdbc:mysql://10.213.96.182/test'
   >         mysqlDataSource.username = 'testuser'
   >            mysqlDataSource.password = '******'
   >            mysqlDataSource.driverClassName = 'com.mysql.jdbc.Driver'
   >            return mysqlDataSource
   >            }
   >     }
   > }
   > ```
   
   This define a bean named `mysqlDataSource` with type `Closure`, you need 
something like:
   
   ```groovy
   def ds =  new org.apache.commons.dbcp.BasicDataSource()
   ds.url = 'jdbc:mysql://10.213.96.182/test'
   ds.username = 'testuser'
   ds.password = '******'
   ds.driverClassName = 'com.mysql.jdbc.Driver'
   
   context {
       registry {
           mysqlDataSource = ds
       }
   }
   ```
   
   Or
   
   ```groovy
   context {
       registry {
           mysqlDataSource = [ url: '...', username: '....' ]  as  
BasicDataSource
       }
   }
   ```
   
   If you use camel-k from master, then you can do something like:
   
   ```groovy
   beans {
       dataSource(org.apache.commons.dbcp2.BasicDataSource) {
           driverClassName = "org.h2.Driver"
           url = "jdbc:h2:mem:camel"
           username = "sa"
           password = ""
       }
   }
   ```
   
   > 
   > My route looks like
   > 
   > ```
   > <?xml version="1.0" encoding="UTF-8"?>
   > <routes xmlns="http://camel.apache.org/schema/spring"; 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; xsi:schemaLocation=" 
http://camel.apache.org/schema/spring 
http://camel.apache.org/schema/spring/camel-spring.xsd";>
   >    <route streamCache="true">
   >       <from uri="jetty:http://0.0.0.0:9090/address"; />
   >       <log message="Received request to query table" />
   >       <to uri="sql:SELECT * FROM address?dataSource=mysqlDataSource" />
   >       <log message="Received records ${body}" />
   >       <convertBodyTo type="java.lang.String" />
   >    </route>
   > </routes>
   > ```
   > 
   > But looks like Camel-K doesn't detect this registry entry.
   > 
   > ```
   > 
   > [1] 2019-07-31 19:29:12.984 INFO  [main] DefaultCamelContext - Apache 
Camel 2.23.2 (CamelContext: camel-k) is shutdown in 0.013 seconds
   > [1] Exception in thread "main" 
org.apache.camel.FailedToCreateRouteException: Failed to create route route1 
at: >>> To[sql:SELECT * FROM address?dataSource=mysqlDataSource] <<< in route: 
Route(route1)[[From[jetty:http://0.0.0.0:9090/address]] -> [... because of 
Failed to resolve endpoint: 
sql://SELECT%20*%20FROM%20address?dataSource=mysqlDataSource due to: DataSource 
must be configured
   > [1]     at 
org.apache.camel.model.RouteDefinition.addRoutes(RouteDefinition.java:1352)
   > ```
   
   

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to