简体   繁体   English

为Apache Camel配置数据源

[英]Configuring datasource for Apache Camel

I have a datasource defined inside my application.properties file as such: 我在application.properties文件中定义了一个数据源,如下所示:

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". 但是,当我尝试运行我的camelroute时,出现以下错误:“在注册表中找不到类型为javax.sql.DataSource的数据源的bean”。 I have always configured my datasource in spring by using the application.properties file without issues. 我一直在春季通过使用application.properties文件配置数据源,而没有出现问题。

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

I suppose that you're using Spring Boot. 我想您正在使用Spring Boot。 Maybe 'datasource' is not the name given by Spring Boot? 也许“数据源”不是Spring Boot给出的名称? An option is to check the names of all the beans loaded by Spring Boot. 一个选项是检查Spring Boot加载的所有bean的名称。 More info: Print all the Spring beans that are loaded - Spring Boot 更多信息: 打印所有已加载的Spring Bean-Spring Boot

Another early option is to configure your DataSource programatically and set the desired name. 另一个较早的选择是以编程方式配置DataSource并设置所需的名称。 More info: Configure DataSource programmatically in Spring Boot 更多信息: 在Spring Boot中以编程方式配置DataSource

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();

    }});

Since you are using Apache Camel you need to define a Data source bean like this 由于您使用的是Apache Camel,因此需要像这样定义一个数据源bean

<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 您的application.properties文件应如下配置

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

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. 我到达这里是因为我正在寻找类似的问题,因此我们正在将基于xml的骆驼代码迁移到Springboot和Java Configuration。 I was able to configure the datasource automatically by doing the following: 通过执行以下操作,我能够自动配置数据源:

Adding the dependency spring-boot-starter-jdbc 添加依赖项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. 版本取决于您使用的Springboot版本。

Configure the following in your application.properties file 在application.properties文件中配置以下内容

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. 按照上述步骤,Springboot将为您自动配置dataSource bean。

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. “ dataSource”是Springboot创建的DataSouurce bean。

If you use to("jdbc:datasource") you'll get the exception below. 如果使用to(“ jdbc:datasource”) ,则会在下面得到异常。

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: 仅提供一项附加信息,如果您忘记包含camel-jdbc依赖项,则会在下面看到异常:

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 因此,请确保您的pom.xml中具有camel-jdbc依赖项

<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jdbc</artifactId>
</dependency>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM