为Apache Camel配置数据源

时间:2018-06-07 03:59:39

标签: java spring-boot apache-camel

我在application.properties文件中定义了一个数据源:

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

但是,当我尝试运行我的camelroute时,我收到此错误:"在注册表中找不到bean:datasource类型:javax.sql.DataSource"。我总是使用application.properties文件在spring中配置我的数据源而没有问题。

完整错误日志:

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

我的骆驼路线:

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

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

    }});

编辑:我以编程方式添加了数据源:

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

但是,现在我遇到了一个不同的错误:

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

3 个答案:

答案 0 :(得分:0)

我想你正在使用Spring Boot。也许'datasource'不是Spring Boot给出的名字?一个选项是检查Spring Boot加载的所有bean的名称。更多信息:Print all the Spring beans that are loaded - Spring Boot

另一个早期选择是以编程方式配置DataSource并设置所需的名称。更多信息:Configure DataSource programmatically in Spring Boot

我希望它有所帮助。

对于新错误,您可以使用直接组件,例如:

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

    }});

答案 1 :(得分:0)

由于您使用的是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>

你的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

答案 2 :(得分:0)

我到达这里是因为我正在研究类似的问题,因此我们正在将基于xml的骆驼代码迁移到Springboot和Java Configuration。通过执行以下操作,我能够自动配置数据源:

添加依赖项spring-boot-starter-jdbc

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

版本取决于您使用的Springboot版本。

在application.properties文件中配置以下内容

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

按照上述步骤,Springboot将为您自动配置dataSource bean。

这是我的示例骆驼路线,试图运行存储过程:

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

“ dataSource”是Springboot创建的DataSouurce bean。

如果使用 to(“ jdbc:datasource”),则会在下面显示异常。

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

还有一项附加信息,如果忘记了添加camel-jdbc依赖项,您将在下面看到异常:

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

因此请确保您的pom.xml中具有camel-jdbc依赖项

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