3

I have a datasource defined inside my application.properties file as such:

spring.datasource.url = jdbc:mysql://localhost:3306/world?useSSL=false
spring.datasource.username = root
spring.datasource.password = password

However, when I try to run my camelroute, I get this error : "No bean could be found in the registry for: datasource of type: javax.sql.DataSource". I have always configured my datasource in spring by using the application.properties file without issues.

Full error log:

org.apache.camel.RuntimeCamelException: org.apache.camel.FailedToCreateRouteException: Failed to create route route5 at: >>> To[jdbc:datasource] <<< in route: Route(route5)[[From[bean:sqlBean?method=generateSqlQuery('ci... because of Failed to resolve endpoint: jdbc://datasource due to: No bean could be found in the registry for: datasource of type: javax.sql.DataSource
    at org.apache.camel.util.ObjectHelper.wrapRuntimeCamelException(ObjectHelper.java:1830) ~[camel-core-2.21.1.jar:2.21.1]
    at org.apache.camel.spring.SpringCamelContext.start(SpringCamelContext.java:136) ~[camel-spring-2.21.1.jar:2.21.1]
    at org.apache.camel.spring.SpringCamelContext.onApplicationEvent(SpringCamelContext.java:174) ~[camel-spring-2.21.1.jar:2.21.1]
    at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172) ~[spring-context-4.3.17.RELEASE.jar:4.3.17.RELEASE]
    at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165) ~[spring-context-4.3.17.RELEASE.jar:4.3.17.RELEASE]
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) ~[spring-context-4.3.17.RELEASE.jar:4.3.17.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:393) ~[spring-context-4.3.17.RELEASE.jar:4.3.17.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:347) ~[spring-context-4.3.17.RELEASE.jar:4.3.17.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:883) ~[spring-context-4.3.17.RELEASE.jar:4.3.17.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.finishRefresh(EmbeddedWebApplicationContext.java:144) ~[spring-boot-1.5.13.RELEASE.jar:1.5.13.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546) ~[spring-context-4.3.17.RELEASE.jar:4.3.17.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.13.RELEASE.jar:1.5.13.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.13.RELEASE.jar:1.5.13.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.13.RELEASE.jar:1.5.13.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.13.RELEASE.jar:1.5.13.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.13.RELEASE.jar:1.5.13.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.13.RELEASE.jar:1.5.13.RELEASE]
    at com.example.SpringApacheDemoApplication.main(SpringApacheDemoApplication.java:15) [classes/:na]
Caused by: org.apache.camel.FailedToCreateRouteException: Failed to create route route5 at: >>> To[jdbc:datasource] <<< in route: Route(route5)[[From[bean:sqlBean?method=generateSqlQuery('ci... because of Failed to resolve endpoint: jdbc://datasource due to: No bean could be found in the registry for: datasource of type: javax.sql.DataSource
    at org.apache.camel.model.RouteDefinition.addRoutes(RouteDefinition.java:1303) ~[camel-core-2.21.1.jar:2.21.1]
    at org.apache.camel.model.RouteDefinition.addRoutes(RouteDefinition.java:204) ~[camel-core-2.21.1.jar:2.21.1]
    at org.apache.camel.impl.DefaultCamelContext.startRoute(DefaultCamelContext.java:1145) ~[camel-core-2.21.1.jar:2.21.1]
    at org.apache.camel.impl.DefaultCamelContext.startRouteDefinitions(DefaultCamelContext.java:3731) ~[camel-core-2.21.1.jar:2.21.1]
    at org.apache.camel.impl.DefaultCamelContext.doStartCamel(DefaultCamelContext.java:3445) ~[camel-core-2.21.1.jar:2.21.1]
    at org.apache.camel.impl.DefaultCamelContext.access$000(DefaultCamelContext.java:209) ~[camel-core-2.21.1.jar:2.21.1]
    at org.apache.camel.impl.DefaultCamelContext$2.call(DefaultCamelContext.java:3253) ~[camel-core-2.21.1.jar:2.21.1]
    at org.apache.camel.impl.DefaultCamelContext$2.call(DefaultCamelContext.java:3249) ~[camel-core-2.21.1.jar:2.21.1]
    at org.apache.camel.impl.DefaultCamelContext.doWithDefinedClassLoader(DefaultCamelContext.java:3272) ~[camel-core-2.21.1.jar:2.21.1]
    at org.apache.camel.impl.DefaultCamelContext.doStart(DefaultCamelContext.java:3249) ~[camel-core-2.21.1.jar:2.21.1]
    at org.apache.camel.support.ServiceSupport.start(ServiceSupport.java:61) ~[camel-core-2.21.1.jar:2.21.1]
    at org.apache.camel.impl.DefaultCamelContext.start(DefaultCamelContext.java:3165) ~[camel-core-2.21.1.jar:2.21.1]
    at org.apache.camel.spring.SpringCamelContext.start(SpringCamelContext.java:133) ~[camel-spring-2.21.1.jar:2.21.1]
    ... 16 common frames omitted
Caused by: org.apache.camel.ResolveEndpointFailedException: Failed to resolve endpoint: jdbc://datasource due to: No bean could be found in the registry for: datasource of type: javax.sql.DataSource
    at org.apache.camel.impl.DefaultCamelContext.getEndpoint(DefaultCamelContext.java:758) ~[camel-core-2.21.1.jar:2.21.1]
    at org.apache.camel.util.CamelContextHelper.getMandatoryEndpoint(CamelContextHelper.java:80) ~[camel-core-2.21.1.jar:2.21.1]
    at org.apache.camel.model.RouteDefinition.resolveEndpoint(RouteDefinition.java:219) ~[camel-core-2.21.1.jar:2.21.1]
    at org.apache.camel.impl.DefaultRouteContext.resolveEndpoint(DefaultRouteContext.java:115) ~[camel-core-2.21.1.jar:2.21.1]
    at org.apache.camel.impl.DefaultRouteContext.resolveEndpoint(DefaultRouteContext.java:121) ~[camel-core-2.21.1.jar:2.21.1]
    at org.apache.camel.model.SendDefinition.resolveEndpoint(SendDefinition.java:62) ~[camel-core-2.21.1.jar:2.21.1]
    at org.apache.camel.model.SendDefinition.createProcessor(SendDefinition.java:56) ~[camel-core-2.21.1.jar:2.21.1]
    at org.apache.camel.model.ProcessorDefinition.makeProcessorImpl(ProcessorDefinition.java:562) ~[camel-core-2.21.1.jar:2.21.1]
    at org.apache.camel.model.ProcessorDefinition.makeProcessor(ProcessorDefinition.java:523) ~[camel-core-2.21.1.jar:2.21.1]
    at org.apache.camel.model.ProcessorDefinition.addRoutes(ProcessorDefinition.java:239) ~[camel-core-2.21.1.jar:2.21.1]
    at org.apache.camel.model.RouteDefinition.addRoutes(RouteDefinition.java:1300) ~[camel-core-2.21.1.jar:2.21.1]
    ... 28 common frames omitted
Caused by: org.apache.camel.NoSuchBeanException: No bean could be found in the registry for: datasource of type: javax.sql.DataSource
    at org.apache.camel.util.CamelContextHelper.mandatoryLookup(CamelContextHelper.java:189) ~[camel-core-2.21.1.jar:2.21.1]
    at org.apache.camel.component.jdbc.JdbcComponent.createEndpoint(JdbcComponent.java:52) ~[camel-jdbc-2.21.1.jar:2.21.1]
    at org.apache.camel.impl.DefaultComponent.createEndpoint(DefaultComponent.java:126) ~[camel-core-2.21.1.jar:2.21.1]
    at org.apache.camel.impl.DefaultCamelContext.getEndpoint(DefaultCamelContext.java:711) ~[camel-core-2.21.1.jar:2.21.1]
    ... 38 common frames omitted

My camel route:

from("bean:sqlBean?method=generateSqlQuery('tablename')")
.to("jdbc:datasource")
.process( new Processor () {

    @Override
    public void process(Exchange exchange) throws Exception {
        exchange.getOut().toString();

    }});

EDIT: I added the datasource programmatically like so:

@Bean
@Primary
public DataSource datasource() {

    System.out.println("hihihi");
    return DataSourceBuilder
        .create()
        .username("root")
        .password("password")
        .url("jdbc:mysql://localhost:3306/world?useSSL=false")
        .driverClassName("com.mysql.jdbc.Driver")
        .build();
}

But, now I encounter a different error:

Caused by: java.lang.UnsupportedOperationException: You cannot consume from a bean endpoint
    at org.apache.camel.component.bean.BeanEndpoint.createConsumer(BeanEndpoint.java:75) ~[camel-core-2.21.1.jar:2.21.1]
    at org.apache.camel.impl.EventDrivenConsumerRoute.addServices(EventDrivenConsumerRoute.java:69) ~[camel-core-2.21.1.jar:2.21.1]
    at org.apache.camel.impl.DefaultRoute.onStartingServices(DefaultRoute.java:103) ~[camel-core-2.21.1.jar:2.21.1]
    at org.apache.camel.impl.RouteService.doWarmUp(RouteService.java:172) ~[camel-core-2.21.1.jar:2.21.1]
    at org.apache.camel.impl.RouteService.warmUp(RouteService.java:145) ~[camel-core-2.21.1.jar:2.21.1]
    ... 28 common frames omitted
czolbe
  • 571
  • 5
  • 18
  • Possible duplicate of [Configure DataSource programmatically in Spring Boot](https://stackoverflow.com/questions/28821521/configure-datasource-programmatically-in-spring-boot) – Makoto Jun 07 '18 at 05:38

3 Answers3

1

I got here because I was looking at a similar problem and we are migrating our xml based camel codes to Springboot and Java Configuration. I was able to configure the datasource automatically by doing the following:

Adding the dependency spring-boot-starter-jdbc

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

The version depends on the Springboot version that you are using.

Configure the following in your application.properties file

spring.datasource.driver-class-name = <driverName>
spring.datasource.url = <url>
spring.datasource.username = <username>
spring.datasource.password = <password>

Following the steps above, Springboot will autoconfigure the dataSource bean for you.

Here is my sample camel route trying to run a stored procedure:

from("timer://mytimer?fixedRate=true&period=10s")
           .log("TESTING")
           .setBody(constant("EXEC SP_Manuals @DocumentRef = 'xyz1234' , @Year = '2019'"))
           .to("jdbc:dataSource")

"dataSource" is the DataSouurce bean that Springboot creates.

If you use to("jdbc:datasource") you'll get the exception below.

Caused by: org.apache.camel.NoSuchBeanException: No bean could be found in the registry for: datasource of type: javax.sql.DataSource

Just an additional info, if one forgets to include the camel-jdbc dependency, you'll get the exception below:

Caused by: org.apache.camel.ResolveEndpointFailedException: Failed to resolve endpoint: jdbc://dataSource due to: No component found with scheme: jdbc

So make sure you have the camel-jdbc dependency in yur pom.xml

<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jdbc</artifactId>
</dependency>
bencampbell_14
  • 587
  • 2
  • 10
  • 32
0

I suppose that you're using Spring Boot. Maybe 'datasource' is not the name given by Spring Boot? An option is to check the names of all the beans loaded by Spring Boot. More info: Print all the Spring beans that are loaded - Spring Boot

Another early option is to configure your DataSource programatically and set the desired name. More info: Configure DataSource programmatically in Spring Boot

I hope it helps.


For the new error, you could use the direct component, for example:

from("direct:start")
.to("bean:sqlBean?method=generateSqlQuery('tablename')")
.to("jdbc:datasource")
.process( new Processor () {

    @Override
    public void process(Exchange exchange) throws Exception {
        exchange.getOut().toString();

    }});
CookieSoup
  • 408
  • 5
  • 10
  • I also do not agree with the downvote. I proposed a related question to this one, the same as later Makoto suggested. – CookieSoup Jun 07 '18 at 06:20
  • I followed your suggestions, including the one to print the bean names in console, and now I can see that there is indeed a bean with the name, 'datasource' but I have ran into a different error, "cannot consume from bean endpoint". – czolbe Jun 07 '18 at 07:59
  • Thanks for the disclaimer. The error is telling you the bean endpoint cannot be defined as the input to the route, you should use, for example, the direct component in from and then call your bean endpoint. More info and examples here: http://camel.apache.org/bean.html – CookieSoup Jun 07 '18 at 08:30
0

Since you are using Apache Camel you need to define a Data source bean like this

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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">


    <bean id="sqlComponent" class="org.apache.camel.component.sql.SqlComponent">
        <property name="dataSource" ref="dataSource" />
    </bean>

</beans>

your application.properties file should be configured like below

spring.datasource.url=#use your url here 
spring.datasource.username=TEST
spring.datasource.password=TEST
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver #give your own driver-class name
spring.datasource.testWhileIdle=true
spring.datasource.validationQuery=SELECT 1
spring.jpa.show-sql=true
Shubham Chopra
  • 1,678
  • 17
  • 30
  • May I know the reason for the downvote since I am also using Apache Camel and spring boot and have configured the same way and it works for me – Shubham Chopra Jun 07 '18 at 05:39
  • Thanks no problem! try adding ``@EnableAutoConfiguration `` just below your ``@SpringBootApplication`` annotation – Shubham Chopra Jun 07 '18 at 09:07